Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to write a code for a form I created called newmembers that takes string,int,string but i did not know how to write the class for it in the app_code

C#
public class newmember
 {
     public static string Register(string mname, int password, string email)
     {
         SqlConnection con = new SqlConnection(Database.ConnectionString);
         try







 }


maybe i should add that the function of the method is to take these values and add them to a table in the database called members.
Posted
Updated 23-Dec-12 8:30am
v2
Comments
Jibesh 23-Dec-12 14:26pm    
This is very basic question. you need to learn how to write a class and its members to be a good programmer. Please put some effort to learn how to wrote class and functions by self. It's not that big deal and you can do it own your own.
SQLmis 23-Dec-12 14:54pm    
ok is this code correct


protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(Database.ConnectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select isnull(max(mid),0)+1 from members",con);
int mid=(int)cmd.ExecuteScalar();
cmd.CommandText="insert into members values(@mid,@name,@password,@email)";
cmd.Parameters.Add("@mid",SqlDbType.Int).Value = mid;
cmd.Parameters.Add("@mname",SqlDbType.VarChar,50).Value=mname.txt;
cmd.Parameters.Add("@password",SqlDbType.VarChar,10).Value=password.txt;
cmd.Parameters.Add("@email",SqlDbType.VarChar,40).Value=email.txt;
cmd.ExecuteNonQuery();
HttpContext.Current.Trace.Write("Member has been added successfully with ID = " +mid);
}
catch (Exception ex)
{
HttpContext.Current.Trace.Write("error" +ex.Message);
}
finally
{
con.Close();
}
}

1 solution

Try:
C#
using (SqlConnection con = new SqlConnection(Database.ConnectionString))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myColumn1, myColumn2, myColumn3) VALUES (@NM, @PW, @EM)", con))
        {
        com.Parameters.AddWithValue("@NM", mname);
        com.Parameters.AddWithValue("@PW", password);
        com.Parameters.AddWithValue("@EM", email);
        com.ExecuteNonQuery();
        }
    }
Obviously, you need to your the appropriate table and column names! :laugh:
 
Share this answer
 
Comments
SQLmis 23-Dec-12 14:59pm    
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(Database.ConnectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select isnull(max(mid),0)+1 from members",con);
int mid=(int)cmd.ExecuteScalar();
cmd.CommandText="insert into members values(@mid,@name,@password,@email)";
cmd.Parameters.Add("@mid",SqlDbType.Int).Value = mid;
cmd.Parameters.Add("@mname",SqlDbType.VarChar,50).Value=mname.txt;
cmd.Parameters.Add("@password",SqlDbType.VarChar,10).Value=password.txt;
cmd.Parameters.Add("@email",SqlDbType.VarChar,40).Value=email.txt;
cmd.ExecuteNonQuery();
HttpContext.Current.Trace.Write("Member has been added successfully with ID = " +mid);
}
catch (Exception ex)
{
HttpContext.Current.Trace.Write("error" +ex.Message);
}
finally
{
con.Close();
}
}
SQLmis 23-Dec-12 15:02pm    
if yes why I keep getting red underline on mname.txt , password.txt, and email.txt

the error is (Error 10 The name 'mname' does not exist in the current context C:...\WebSite2\
)
SQLmis 23-Dec-12 15:32pm    
I fix all of it the only thing left is that error :

Error 3 'Button2_Click' is not a member of 'ASP.newmember_aspx'. C:\Users\dr.apple\Documents\Visual Studio 2010\WebSites\WebSite2\newmember.aspx 45


although i have the method in inside the class and that is the code:




protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(Database.ConnectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select isnull(max(mid),0)+1 from members", con);
int mid = (int)cmd.ExecuteScalar();
SqlCommand cmd1 = new SqlCommand("select isnotnull from members", con);
string mname = (string)cmd1.ExecuteScalar();
SqlCommand cmd3 = new SqlCommand("select isnotnull from members", con);
Char password = (char)cmd3.ExecuteScalar();
SqlCommand cmd4 = new SqlCommand("select isnotnull from members", con);
string email = (string)cmd4.ExecuteScalar();
cmd.CommandText = "insert into members values(@mid,@name,@password,@email)";
cmd.Parameters.Add("@mid", SqlDbType.Int).Value = mid;
cmd.Parameters.Add("@mname", SqlDbType.VarChar, 50).Value = mname;
cmd.Parameters.Add("@password", SqlDbType.VarChar, 10).Value = password;
cmd.Parameters.Add("@email", SqlDbType.VarChar, 40).Value = email;
cmd.ExecuteNonQuery();
HttpContext.Current.Trace.Write("Member has been added successfully with ID = " + mid);
}
catch (Exception ex)
{
HttpContext.Current.Trace.Write("error" + ex.Message);
}
finally
{
con.Close();
}
}



}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900