2013-11-14 61 views
1

我正在處理我的項目,並在執行dbm.SubmitChanges()時得到'字符串或二進制數據將被截斷'錯誤。將在Linq中截斷字符串或​​二進制數據

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); 
} 
+0

我注意到你的原始帖子中出現了'{'亂碼。我想我修好了他們,但看看。 – paqogomez

回答

4

這意味着你的一個字段被溢出的值太長。請檢查您輸入反對您的欄定義

+0

我檢查了所有字段的所有長度都足以存儲我的數據! –

+0

SQL服務器不同意你的看法。提出的答案是正確的。調試這可能會很痛苦,因爲錯誤可能發生在結果集上的任何行上。 –

+0

我應該對byte []字段做些什麼嗎? –

0

是當你提到一個極限在數據庫領域,如

Name field is nvarchar(50) 

發生這種情況你要添加的東西超過此限制,那麼很明顯的錯誤會在那裏。

因此,有解決方案,

1)擴展字段的大小在DB。

2)限制UI中字段的大小。 (就像添加一個驗證器,輸入限制值)。

相關問題