0

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();
    }

Edited by __avd: Added [code] tags.

4
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by MamathaS
0

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.

0

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);
 ..
0

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

0

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..

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.