简体   繁体   中英

how can function called from another class in C#

I want to make one generic class for all my button click methods. My button click method works properly in same aspx.cs file but when I want to call this method from generic class. But parameter is not passing. Can someone please help me. Here is my code.

This is base class

namespace WebApplication1
{
    public partial class Singnup : System.Web.UI.Page
    {
        protected void SUpButton_Click(object sender, EventArgs e)
        {
            Webapplication2.program.Insert_RData(sender, e);
        }
    }
}

This is 2nd class where from i want to call button method

namespace Webapplication2
{
    public class program : WebApplication1.Singnup
    {
        public static void Insert_RData(object sender, EventArgs e)
        {
            SqlConnection con_Signup = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString);
            con_Signup.Open();
            SqlCommand cmd_check = new SqlCommand("Check_Existing_Email", con_Signup);
            cmd_check.CommandType = CommandType.StoredProcedure;
            cmd_check.Parameters.AddWithValue("@mail",EmailId);
            object i = cmd_check.ExecuteScalar();

            if (i != null)
            {
                lbforerror.Text = "This Email is already Registered";
                lbforerror.Visible = true;
            }

try below code

namespace WebApplication1
{
    public partial class Singnup : System.Web.UI.Page
    {
        protected void SUpButton_Click(object sender, EventArgs e)
        {
            string EmailId = "yourmailid@domain.com";
            Webapplication2.program.InsertRData(EmailId);
        }
    }
}

namespace Webapplication2
{
    public class program : WebApplication1.Singnup
    {
        public static void Insert_RData(object sender, EventArgs e)
        {
            string EmailId = "yourmailid@domain.com";
            InsertRData(EmailId);
        }

        public static void InsertRData(string EmailId)
        {
            SqlConnection con_Signup = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString);
            con_Signup.Open();
            SqlCommand cmd_check = new SqlCommand("Check_Existing_Email", con_Signup);
            cmd_check.CommandType = CommandType.StoredProcedure;
            cmd_check.Parameters.AddWithValue("@mail", EmailId);
            object i = cmd_check.ExecuteScalar();

            if (i != null)
            {
                lbforerror.Text = "This Email is already Registered";
                lbforerror.Visible = true;
            }
        }
    }
}

You are passing information from an instantiated class Singup to a static class program . The labels, etc will not be available from a static class, as it has no connection to the actual label.

If you want to separate the button click actions, AND you want them to access form elements (such as lbforerror then i suggest you use a partial class or simply use a Region within the same class to clean up the logic

#region Button Logic
//Your logic here
#endregion //button logic

The best way, is to separate the re-useable logic into a completely different class (in this case, handling the insertion) which returns the result, and is decisioned in your original class:

public class ConnectionManager
{
    public object InsertRData(string EmailId)
    {
        SqlConnection con_Signup = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString);
        con_Signup.Open();
        SqlCommand cmd_check = new SqlCommand("Check_Existing_Email", con_Signup);
        cmd_check.CommandType = CommandType.StoredProcedure;
        cmd_check.Parameters.AddWithValue("@mail", EmailId);
        object i = cmd_check.ExecuteScalar();
        return i;
    }
}

Then your original class can simply say

public partial class Singnup : System.Web.UI.Page
{
    protected void SUpButton_Click(object sender, EventArgs e)
    {
        ConnectionManager mgr = new ConnectionManager();
        object i = mgr.Insert_RData("email logic here");
        if (i != null)
        {
            lbforerror.Text = "This Email is already Registered";
            lbforerror.Visible = true;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM