Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I m not able to insert data into database.
The exception thrown is--- "String or binary data would be truncated.
The statement has been terminated."
there is no problem with connection.its connecting properly.
I m still getting the error.plz help me out

public SqlConnection connect()
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=IMS;Integrated Security=True");
            con.Open();
            return con;
        }

        public void Closeconnect(SqlConnection con)
        {
            con.Close();
        }

private void btnSave_Click(object sender, EventArgs e)
        {
            string Query = "Insert into tblEnquiry values (@mem_no,@date,@name,@add,@gen,@city,@pin,@home,@mobile,@email,@dob,@exec,@interest,@contact,@name1,@tel1,@name2,@tel2)";
            SqlCommand cmdSave = new SqlCommand(Query, connect());
            cmdSave.Parameters.Add(new SqlParameter("@mem_no", txtMemNo.Text));
            cmdSave.Parameters.Add(new SqlParameter("@date", dtpEnquiryDate.Value));
            cmdSave.Parameters.Add(new SqlParameter("@name", txtName.Text));
            cmdSave.Parameters.Add(new SqlParameter("@add", txtAddress.Text));
            cmdSave.Parameters.Add(new SqlParameter("@gen", cbGender.Text));
            cmdSave.Parameters.Add(new SqlParameter("@city", txtCity.Text));
            cmdSave.Parameters.Add(new SqlParameter("@pin", txtPin.Text));
            cmdSave.Parameters.Add(new SqlParameter("@home", txtTelHome.Text));
            cmdSave.Parameters.Add(new SqlParameter("@mobile", txtTelMobile.Text));
            cmdSave.Parameters.Add(new SqlParameter("@email", txtEmailID.Text));
            cmdSave.Parameters.Add(new SqlParameter("@dob", dtpDOB.Value));
            cmdSave.Parameters.Add(new SqlParameter("@exec", cbExecutive.Text));
            cmdSave.Parameters.Add(new SqlParameter("@interest", cblInterest.Text));
            cmdSave.Parameters.Add(new SqlParameter("@contact", cblContact.Text));
            cmdSave.Parameters.Add(new SqlParameter("@name1", txtName1.Text));
            cmdSave.Parameters.Add(new SqlParameter("@tel1", txtTel1.Text));
            cmdSave.Parameters.Add(new SqlParameter("@name2", txtName2.Text));
            cmdSave.Parameters.Add(new SqlParameter("@tel2", txtTel2.Text));
            cmdSave.ExecuteNonQuery();
            MessageBox.Show("Record Saved Successfully");
            Closeconnect(connect());
        }


I also need code to insert image into database along with the above data.
Posted
Updated 5-Apr-10 9:39am
v3

The message is telling you what the problem actually is. You have a string value that's far too big to go into a field in your database. I notice that you haven't actually specified any datatypes/sizes in your parameter, so I can't even begin to give you an indication as to which one it might be.

BTW: You should really wrap the SqlCommand in a using statement to dispose of it. As you are getting an exception in your code, the Closeconnect method is not being reached, which means your connection isn't being closed. Also, you are calling connect() twice. The first when you create your command, the second when you call Closeconnect. Change your Closeconnect statement to Closeconnect(cmdSave.Connection); and wrap con.Close in the Closeconnect method with a check to see if it's open or not:
if (con.Connection == ConnectionState.Open)
{
  con.Close();
}
 
Share this answer
 
Not sure. Maybe your date field has the wrong type (say date) whereas the DateTimePicker.Value is a DateTime, which needs probably twice as much space. You might want to make sure the field is a DateTime.

:)
 
Share this answer
 
Is it throwing an exception?
If so, what is the exception message, and where is it being thrown?

If that your actual connection string?
If so, shouldn't it be "(localhost)/SQLEPRESS" or similar, rather than "." for the data source?

"It don't work" is not particularly useful as a problem description!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900