2012-08-14 87 views
2

我從ExecuteNonQuery()ExecuteNonQuery();錯誤;

"String or binary data would be truncated. The statement has been terminated." 

以下錯誤的問題是要發送的變量是更大的一個在數據庫中。 我使用下面的代碼:

 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
     //GridViewRow row = GridView1.SelectedRow; 
     GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; 
     var Username = (Label)row.FindControl("Label3"); 
     var Password = (Label)row.FindControl("Label4"); 
     var Email = (Label)row.FindControl("Label5"); 
     // var ID_Inscricao = ((Label)row.FindControl("Label1")).Text; 
     var ID_Inscricao = ((Label)row.FindControl("Label1")).Text; 


     SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["FormacaoConnectionString"].ToString()); 
     SqlCommand sqlComm = new SqlCommand(); 


     sqlComm = sqlConn.CreateCommand(); 
     sqlComm.Parameters.Add("@Username", SqlDbType.Text); 
     sqlComm.Parameters.Add("@Password", SqlDbType.Text); 
     sqlComm.Parameters.Add("@Email", SqlDbType.Text); 
     sqlComm.Parameters.Add("@ID_Inscricao", SqlDbType.Int); 

     sqlComm.Parameters["@Username"].Value = Convert.ToString(Username); 
     sqlComm.Parameters["@Password"].Value = Convert.ToString(Password); 
     sqlComm.Parameters["@Email"].Value = Convert.ToString(Email); 
     sqlComm.Parameters["@ID_Inscricao"].Value = Convert.ToInt32(ID_Inscricao); 

     string sql = "INSERT INTO Utilizadores (Username, Password, Email, ID_Inscricao) VALUES (@Username, @Password, @Email, @ID_Inscricao)"; 
     sqlComm.CommandText = sql; 
     sqlConn.Open(); 
     sqlComm.ExecuteNonQuery(); 
     sqlConn.Close(); 
    } 

回答

6

這種情況發生時您嘗試插入一些成列,它是該列太長。

例如,當您有一列VARCHAR(10)並且您嘗試插入值"ABCDEFGHIJKLMNOP"。這將導致數據被截斷併產生錯誤。

我不知道你的情況是哪一列,但它必須是Username,PasswordEmail之一。

+0

是的,你需要截斷你插入的值。 – neontapir 2012-08-14 14:25:17

+0

但不是33 bigint – 2012-08-14 14:27:41

+0

我想33是你的'ID_Inscricao'列的值嗎?問題不在該欄中。 – 2012-08-14 14:42:25

0

問題是您試圖填充的數據庫中的數據類型小於您傳遞的值之一。您需要檢查每個參數@Username,@Password和@Email,並查看哪一個參數大於數據庫數據類型。然後,您可以根據需要增加數據庫中數據類型的大小,或者在發送之前修改參數值。

+0

true我已經修改了類型,所以它與數據庫「相等」,但仍然是相同的錯誤。 – 2012-08-14 14:48:23

-1
Dim qry As String 
     qry = "inserg into Table1 values('" & TextBox1.Text & "'+'" & TextBox2.Text & "'+'" & DropDownList1.Text & "'+'" & DropDownList2.Text & "'+'" & TextBox4.Text & "'+'" & TextBox5.Text & "'+'" & RadioButtonList1.Text & "'+'" & RadioButton1.Text & "'+'" & RadioButtonList2.Text & "'+'" & CheckBoxList1.Text & "'+'" & TextBox6.Text & "'+'" & TextBox7.Text & "'+'" & TextBox8.Text & "'+'" & TextBox9.Text & "'+'" & TextBox10.Text & "'+'" & RadioButtonList3.Text & "'" 
     Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\MCALab\My Documents\net.mdb") 
     Dim cmd As New OleDbCommand(qry, cn) 
     cn.Open() 
     **cmd.ExecuteNonQuery()**this line have error please any one know mean debug it. 

     cn.Close()