Salman Mushtaq
Salman Mushtaq

Reputation: 341

String or binary data would be truncated. The statement has been terminated.(Linq to SQL)

I read all the topics related to title question, in all articles I found that this error occurred when we save maximum characters data in column. I apply this solution but this error not resolved. I also comment the code where I guess maximum characters will be saved but still I faced the problem. I don't know why. Below is my code. Please help.

protected void btnInsert_Click(object sender, EventArgs e)
{
    try
    {
        SiteDataContext dc = new SiteDataContext();
        User obj = new User();
        obj.Name = txtName.Text.ToString();
        obj.FatherName = txtFatherName.Text.ToString();
        obj.Password = txtPassword.Text.ToString();
        obj.DOB = txtDOB.Text.ToString();
        obj.Email = txtEmail.Text.ToString();
        //for picture
        string filename = Path.GetFileName(fuPicture.FileName);
        fuPicture.SaveAs(Server.MapPath("Pictures/" + filename));
        string address = ("Pictures/" + filename).ToString();
        obj.Picture = address;
        obj.Gender = rblGender.SelectedItem.Text.ToString();
        obj.Mobile = txtMobile.Text.ToString();
        obj.Interest = getInterest();
        dc.Users.InsertOnSubmit(obj);
        dc.SubmitChanges();
        lblMessage.Visible = true;
        lblMessage.Text = "Data Inserted Successfully.... You redirect to login page in 5 seconds";
        Response.Redirect("Login.aspx");
    }
    catch (Exception exp)
    {
        lblMessage.Visible = true;
        lblMessage.Text = exp.Message;
    }
}

Upvotes: 0

Views: 2794

Answers (2)

Hogan
Hogan

Reputation: 70523

The following will truncate all your strings to 10 characters and gender to 1 character. Try this. I expect it will work. Then look at the size of the fields in your database and know which one caused the problem.

    obj.Picture = (address).SubString(0,10);
    obj.Gender = rblGender.SelectedItem.Text.ToString().SubString(0,1);
    obj.Mobile = txtMobile.Text.ToString().SubString(0,10);
    obj.Interest = (getInterest()).SubString(0,10);

Upvotes: 0

TomTom
TomTom

Reputation: 62093

This error occurs if you try to save a string into the database that is longer than allowed by the database. That is all. Fix it.

Either limit the input length, or adjust the allowed length in the database. Does not help that you consider the database so irrelevant you do not publish the table structure.

Upvotes: 3

Related Questions