Click here to Skip to main content
15,886,004 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have been trying to add data from a textbox into a datatable called Registration on button click. The column I am trying to add the data to is "Name". However, when I click button, it gives me error saying the text I entered in textbox1 (whatever I entered into the textbox) column does not exist. I want to maintain the same logic in the code. Any help would be appreciated.
protected void Button1_Click(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection();
       con.ConnectionString = "MY CONNECTION STRING GOES HERE";
       con.Open();
       SqlCommand cmd = new SqlCommand();
       cmd.CommandText = "insert into Registration (Name) values (" +    Convert.ToString(TextBox2.Text) + ")";
       cmd.CommandType = CommandType.Text;
       cmd.Connection = con;
       //cmd.ExecuteNonQuery();
       DataSet ds = new DataSet();
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       da.Fill(ds);
       con.Close();
   }
Posted
Updated 1-Aug-11 2:41am
v3
Comments
surender.m 30-Jul-11 0:42am    
You are trying to insert value of textbox into registration table for name. till now its fine but why you are trying to fill dataset for DML query?
ExecuteNonQuery() will do your purpose.

1 solution

You have to use Single quotes if you are appending a string datatype.
cmd.CommandText = "insert into Registration (Name) values ('" +Convert.ToString(TextBox2.Text)+ "')";
 
Share this answer
 

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