storing data from textbox to sql table

Aug 7 2009 12:35 PM
hi guys
i dont know if it's the right place
i just want to take value from two textbox & store it into sql database
i am using sql server 2005 & visual studio 2005
i have used a label for checking the data is stoed or not ...but not working
this is the code for the buton event

 SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\db.mdf;Integrated Security=True;User Instance=True";

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into t1 (name,age) values (@name,@age)";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
SqlParameter name_param = new SqlParameter("@name", SqlDbType.VarChar, 50);
name_param.Value = textBox1.Text;
cmd.Parameters.Add(name_param);

SqlParameter age_param = new SqlParameter("@age", SqlDbType.VarChar, 10);
age_param.Value = textBox2.Text;
cmd.Parameters.Add(age_param);
con.Open();
int result = cmd.ExecuteNonQuery();
if (result != 0)
{
lb1.Visible = true;
lb1.Text = "added";
}
else
{
lb1.Visible = true;
lb1.Text = "error";
}


con.Close();


can someone pls give some simple explanation of how to solve my problem
thnks

Answers (10)