Reputation: 43
I get the following error from ExecuteNonQuery()
"String or binary data would be truncated. The statement has been terminated."
The problem is the variable to be sent is bigger that the one in the DB. I am using the following code:
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();
}
Upvotes: 2
Views: 1303
Reputation: 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()
Upvotes: -1
Reputation: 32134
This happens when you try to insert something into a column that is too long for that column.
For example, when you have a column VARCHAR(10)
and you try to insert the value "ABCDEFGHIJKLMNOP"
. This would lead to the data being truncated and that generates an error.
I can't tell which column it is in your case but it has to be one of Username
, Password
or Email
.
Upvotes: 6
Reputation: 1699
The issue is the data type in the database you are trying to populate is smaller than one of the values you are passing. You need to check each of your parameters @Username, @Password and @Email and see which one is larger than the database data type. Then you can increase the size of the data type in the database if required, or trim the parameter values before sending.
Upvotes: 0