retrieving textbox data to sql from asp.net
i have a code to move data from asp.net (c# code) to sql. In the following code i have inserted values directly (i.e) xxx and yyy.This works too. Now i want to insert the 开发者_如何学Govalues which was entered in text boxes. What will be the code to do so?. Help me friends.
protected void Button2_Click(object sender, EventArgs e)
{
string comm="insert into login values('xxx','yyy')";
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString.ToString();
cmd = new SqlCommand(comm, con);
cmd.Connection.Open();
con.Open();
int i = cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
string comm="insert into login values(@val1,@val2)";
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString.ToString();
cmd = new SqlCommand(comm, con);
cmd.Connection.Open();
cmd.Parameters.Add("@val1",TextBox1.Text); //Replace TextBox1.Text with your first textbox
cmd.Parameters.Add("@val2",TextBox2.Text); //Replace TextBox2.Text with your second textbox
con.Open();
int i = cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
精彩评论