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

Naitik JaniPosted Apr 10, 2014, 4:42 AM
You are inserting values from code more than Database Column Size you have defined.
for Example Address varchar(50) but you are trying to insert more than 50 characters into that.
VulpesPosted Mar 30, 2014, 10:39 AM
So, do any of the columns have lowish maximum lengths?
One other point I've noticed is that I'd expect age to be an int column but the value you're inserting is enclosed in single quotes.