Afzal Mozan

Afzal Mozan

  • NA
  • 122
  • 15.3k

c#.net

Mar 30 2014 10:14 AM
Dear sir,
i am BCA student from saurashtra university,
sir when i use following code in c# .Net connected architecture for insert data in database
note: database is service based database and data provider is sqlclient.
the main point is that when i am insert the data in database using this code one error occur at run time
 
private void insertin_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Afzal Mozan\Desktop\ADO.Net connectivity\ADO.Net connectivity\Database1.mdf;Integrated Security=True;User Instance=True");
string str;
if (malein.Checked == true)
{
str = malein.Text;
}
else { str = femalein.Text; }
con.Open();
SqlCommand cmd = new SqlCommand("insert into student(fname,lname,gender,age,address)values('"+fnamein+"','"+lnamein+"','"+str+"','"+agein+"','"+addin+"')", con);
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("Data Inserted successfully");
}
else
{
MessageBox.Show("Data not Inserted ");
}
}
catch (Exception e)
{
MessageBox.Show("Inner Error");
}
}
 The error is:
{"String or binary data would be truncated.\r\nThe statement has been terminated."} 

Answers (2)