There are text boxes in my windows form called txtName,txtAdd,txtTel & txtEmail. I need to add the text in this text boxes to my database table. My table name is "Table". I used the following code to add those text to my table. The table columns are Name,Address,Tel No. & email.
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\user\documents\visual studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=True");
private void button1_Click(object sender, EventArgs e)
{
try
{
String name = txtName.Text;
String add = txtAdd.Text;
String tel = txtTel.Text;
String email = txtEmail.Text;
String SqlQuery = "insert into Table values('" + name + "','" + add + "','" + tel + "','" + email + "')";
SqlCommand cmnd = new SqlCommand(SqlQuery, con);
con.Open();
cmnd.ExecuteNonQuery();
MessageBox.Show("Saved Successfully", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("Error occured while saving", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
con.Close();
}
}
I typed some texts and pressed button1. But everytime I get the "Error occured while saving" message box. I can't understand the reason for that. I used varchar data type for all of the fields in my table. I am using visual studio 2012 express. Please help me to solve this problem.