字符串或二进制数据将被截断。该声明已被终止错误

时间:2017-10-17 09:12:07

标签: .net

protected void Page_Load(object sender, EventArgs e) {
    dbcon = new SqlConnection(ConfigurationManager.ConnectionStrings["modelConnectionString"].ToString());
    dbcon.Open();
    Session["connection"] = dbcon;
}

protected void create_account_button_Click(object sender, EventArgs e) {
    try {
        String username = username_textbox.Text;
        String pass = pwd_textbox.Text;
        String mob = mobile_textbox.Text;
        String email = email_textbox.Text;
        String add = address_textbox.Text;
        String ques = question_drop_down.Text;
        String ans = answer_textbox.Text;

        SqlCommand sqlcmd = new SqlCommand("insert into bankdb(username,password,email,mobile,address,secQue,secAns) values(@username,@password,@email,@mobile,@address,@question,@answer)", dbcon);
        sqlcmd.Parameters.AddWithValue("@username", username);
        sqlcmd.Parameters.AddWithValue("@password", pass);
        sqlcmd.Parameters.AddWithValue("@email", email);
        sqlcmd.Parameters.AddWithValue("@mobile", mob);
        sqlcmd.Parameters.AddWithValue("@address", add);
        sqlcmd.Parameters.AddWithValue("@question", ques);
        sqlcmd.Parameters.AddWithValue("@answer", ans);
        sqlcmd.ExecuteNonQuery();

        //update = 1;
        //Session["inserted"] = update.ToString();
        Response.Redirect("registerSucessfully.aspx");
    } catch(Exception ex) {
        Response.Write(ex.ToString());
    }
}

错误:

  

System.Data.SqlClient.SqlException(0x80131904):字符串或二进制数据将被截断。该语句已终止。在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolean breakConnection,Action 1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction)处于System.Data.SystemClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose) System.Data上的System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)中的.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean& dataReady) System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String)中的.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32 timeout,Task& task,Boolean asyncWrite)方法,TaskCompletionSource 1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource 1完成,字符串我thodName,布尔值sendToPipe,Int32超时,布尔asyncWrite)位于e:\ dot net \ projects \ BankMgtNew \ BankMgtNew \ new_register的BankMgtNew.new_register.create_account_button_Click(Object sender,EventArgs e)中的System.Data.SqlClient.SqlCommand.ExecuteNonQuery() .aspx.cs:第42行ClientConnectionId:4d3fc507-a71a-49e1-83d6-49a93ab41096

1 个答案:

答案 0 :(得分:0)

您传递的值可能大于您为表列提到的数据类型的大小,将值数据大小与列数据类型大小进行比较。