Ihave used the following to save the users data in the the SQL table.But the data is not been save.Can anyone pls suggest what is the error?

{
        string Name = TextBox4.Text;
        String Telephone = TextBox3.Text;
        String Email = TextBox2.Text;
        String enquiry = TextBox1.Text;
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "data source=.\\SQLEXPRESS;initial catalog=IBservices;integrated security=true;";
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "INSERT INTO contactus1(Name,Telephone,Email,Enquiry) values ('" + TextBox4.Text + "," + TextBox3.Text + "," + TextBox2.Text + "," + TextBox1.Text + "')";
        cmd.Connection = conn;
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }

Recommended Answers

All 5 Replies

Getting any error message?
before doing cmd.executeNonQuery get the sql statement and try to run in sql server
so you will come to know any syntax error or somethng.

Improper SQL statement.

cmd.CommandText = "INSERT INTO contactus1(Name,Telephone,Email,Enquiry) values ('" + TextBox4.Text + "','" + TextBox3.Text + "','" + TextBox2.Text + "','" + TextBox1.Text + "')";

Use Parameters instead of hard-coded sql statement.

SqlCommand cmd = new SqlCommand();
 cmd.CommandText = "INSERT INTO contactus1 (Name,Telephone,Email,Enquiry) values (@name,@tel,@email,@enq)";
 cmd.Connection = conn;
 cmd.Parameters.AddWithValue("@name",TextBox4.Text);
 cmd.Parameters.AddWithValue("@tel",TextBox3.Text);
 cmd.Parameters.AddWithValue("@email",TextBox2.Text);
 cmd.Parameters.AddWithValue("@enq",TextBox1.Text);
 ..

Can anyone pls tell if there can be any acknowledgement in the form of message, if the data is saved in the table?

Thanq Mr.KV for the response.I did try the code tat u haved posted as well, but there are no changes.Is there any setting that I have to change in the the DB ??Im a learner, not sure if I have to change something in the table..

Check the following criterias

1. First check the UserId you are using to connect to SQL Server has got enough privileges to peform insert action.

2. Check the data length of all the columns. Check whether your data fits within the specified data limit. If not increase the datalength and try again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.