Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
 
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 23-Dec-12 9:23am
SQLmis314
Edited 23-Dec-12 9:30am
v2
Comments
jibesh at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try:
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 | :laugh:
  Permalink  
Comments
SQLmis at 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 at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 382
1 DamithSL 275
2 OriginalGriff 245
3 Zoltán Zörgő 149
4 Dave Kreskowiak 110
0 OriginalGriff 7,575
1 DamithSL 5,529
2 Sergey Alexandrovich Kryukov 5,279
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 23 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100