简体   繁体   中英

String or binary data would be truncated in Linq

I was working on my project and i get 'String or binary data would be truncated' error on execution of dbm.SubmitChanges();

privatevoid btn_ok_Click(object sender, EventArgs e)
{
    myDataClasses_LinqDataContext dbm = newmyDataClasses_LinqDataContext();
    myDataClasses_LinqDataContext dbp = newmyDataClasses_LinqDataContext();
    if (rdo_main.Checked == true)
    {
        string sh_shenase = txtshenase.Text;
        string name = txtname.Text;
        string family = txtfamily.Text;
        string sh_shenasname = txtshenasname.Text;
        string sh_meli = txtshmeli.Text;
        string tt_ruz = cmbroz.Text;
        string tt_mah = cmbmah.Text;
        string tt_sal = cmbsal.Text;
        string loc_tavalod = cmbshahr.Text;
        string name_pedar = txtfathername.Text;
        string vaz_tahol = cmbvaztahol.Text;
        string sh_pishtel = txtshPtamas.Text;
        string sh_tel = txtshtamas.Text;
        string mobile = txtshmobile.Text;
        string email = txtemail.Text;
        string address = txtaddres.Text;
        string mov_nazari = txtmovazafi_nazari.Text;
        string mov_amali = txtmovazafi_amali.Text;

        MemoryStream ms = newMemoryStream();
             pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
        byte[] arrpic = ms.GetBuffer();
        tbl_pro_main tbl = newtbl_pro_main()
             {
                 address = address,
                 birth_date = tt_ruz,
                 birth_loc = loc_tavalod,
                 birth_month = tt_mah,
                 birth_year = tt_sal,
                 bound_a = mov_amali,
                 bound_n = mov_nazari,
                 email = email,
                 family = family,
                 id = sh_shenasname,
                 m_status = vaz_tahol,
                 mobile_num = mobile,
                 n_cod = sh_meli,
                 name = name,
                 name_father = name_pedar,
                 phone_num = sh_tel,
                 phone_pnum = sh_pishtel,
                 pic = arrpic,
                 username = sh_shenase
             };
             dbm.tbl_pro_mains.InsertOnSubmit(tbl);
             **dbm.SubmitChanges();**// error `String or binary data would be truncated`!!!!!
        MessageBox.Show("success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    elseif (rdo_public.Checked ==true)
    {
        string sh_shenase = txtshenase.Text;
        string name = txtname.Text;
        string family = txtfamily.Text;
        string sh_shenasname = txtshenasname.Text;
        string sh_meli = txtshmeli.Text;
        string tt_ruz = cmbroz.Text;
        string tt_mah = cmbmah.Text;
        string tt_sal = cmbsal.Text;
        string loc_tavalod = cmbshahr.Text;
        string name_pedar = txtfathername.Text;
        string vaz_tahol = cmbvaztahol.Text;
        string sh_pishtel = txtshPtamas.Text;
        string sh_tel = txtshtamas.Text;
        string mobile = txtshmobile.Text;
        string email = txtemail.Text;
        string address = txtaddres.Text;
        string mov_nazari = txtmovazafi_nazari.Text;
        string mov_amali = txtmovazafi_amali.Text;

        MemoryStream ms = newMemoryStream();
         pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
        byte[] arrpic = ms.GetBuffer();

        tbl_pro_public tbl = newtbl_pro_public()
         {
             address = address,
             birth_date = tt_ruz,
             birth_loc = loc_tavalod,
             birth_month = tt_mah,
             birth_year = tt_sal,
             bound_a = mov_amali,
             bound_n = mov_nazari,
             email = email,
             family = family,
             id = sh_shenasname,
             m_status = vaz_tahol,
             mobile_num = mobile,
             n_cod = sh_meli,
             name = name,
             name_father = name_pedar,
             phone_num = sh_tel,
             phone_pnum = sh_pishtel,
             pic = arrpic,
             username = sh_shenase
         };
         dbp.tbl_pro_publics.InsertOnSubmit(tbl);
         dbp.SubmitChanges();

        MessageBox.Show("success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
        MessageBox.Show("success!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

It means that one of your fields are being overflowed by values that are too long. Check your input against your column definitions

Yes this occurs when you have mentioned a limit on the database fields like

Name field is nvarchar(50)

And you are adding something more than this limit, then obviously an error will be there.

So there are solutions,

1) Extend the size of the field in DB.

2) Limit the size of the field in the UI. (Like adding a validator something enter values in limit).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM