C#ASP.NET注册页面不再更新数据库

时间:2016-02-04 03:53:05

标签: c# asp.net .net sql-server

我的注册页面(VS 2015上的C#ASP.NET)更新了我的SQL Server数据库中的一些条目,然后我更改了数据库,稍微更新了代码(更新了web.config字符串等),现在它只显示此错误:

  

错误: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)中的.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32 timeout,Task& task,Boolean asyncWrite,SqlDataReader ds,Boolean describeParameterEncryptionRequest) runBehavior,Boolean returnStream,S tring方法,TaskCompletionSource 1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource 1完成,String methodName,布尔sendToPipe,Int32超时,布尔asyncWrite)在System.Data.SqlClient.SqlCommand.ExecuteNonQuery()at Registration.Button1_Click(Object sender,EventArgs e)in c: \ Users \ 718358 \ Documents \ Visual Studio 2015 \ WebSites \ Registration2 \ Registration.aspx.cs:第61行ClientConnectionId:d0e00199-8bf8-435b-a2ea-03d6f5c8dc40错误号码:8152,州:13,类:16   注册

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;

        if (IsPostBack)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationDatabaseConnectionString"].ConnectionString);
            conn.Open();
            string checkuser = "SELECT count(*) FROM Customers WHERE CustUserName='" + txtCustUserName.Text + "'";

            SqlCommand com = new SqlCommand(checkuser, conn);
            int temp = Convert.ToInt32(com.ExecuteScalar().ToString());

            if (temp == 1)
            {
                Response.Write("User already exists");
            }

            conn.Close();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationDatabaseConnectionString"].ConnectionString);
            conn.Open();

            string insertQuery = "INSERT into Customers (CustUserName, CustPassword, CustFirstName, CustLastName, CustAddress, CustCity, CustProv, CustPostal, CustCountry, CustHomePhone, CustBusPhone, CustEmail) values (@custUsername ,@custPassword ,@custFirstName ,@custLastName ,@custAddress ,@custCity ,@custProv ,@custPostal, @custCountry ,@custHomePhone ,@custBusPhone ,@custEmail)";

            SqlCommand com = new SqlCommand(insertQuery, conn);
            com.Parameters.AddWithValue("@custUsername", txtCustUserName.Text);
            com.Parameters.AddWithValue("@custPassword", txtCustPassword.Text);
            com.Parameters.AddWithValue("@custFirstName", txtCustFirstName.Text);
            com.Parameters.AddWithValue("@custLastName", txtCustLastName.Text);
            com.Parameters.AddWithValue("@custAddress", txtCustAddress.Text);
            com.Parameters.AddWithValue("@custCity", txtCustCity.Text);
            com.Parameters.AddWithValue("@custProv", txtCustProv.Text);
            com.Parameters.AddWithValue("@custPostal", txtCustPostal.Text);
            com.Parameters.AddWithValue("@custCountry", txtCustCountry.Text);
            com.Parameters.AddWithValue("@custHomePhone", txtCustHomePhone.Text);
            com.Parameters.AddWithValue("@custBusPhone", txtCustBusPhone.Text);
            com.Parameters.AddWithValue("@custEmail", txtCustEmail.Text);

            com.ExecuteNonQuery();

            Response.Redirect("Manager.aspx");
            Response.Write("Registration is successful" );

            conn.Close();
        }
        catch(Exception ex)
        {
            Response.Write("Error:"+ex.ToString());
        }
    }
}

感谢。

5 个答案:

答案 0 :(得分:1)

根据错误消息,一个或多个字段获取的值超过相应列。你应该仔细检查列的长度。在不了解更多细节的情况下,无法说明更改数据库的方式或原因导致了这一点。

答案 1 :(得分:0)

将以下代码替换为现有代码: - )

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationDatabaseConnectionString"].ConnectionString);
            conn.Open();
            string insertQuery = "INSERT into Customers (CustUserName, CustPassword, CustFirstName, CustLastName, CustAddress, CustCity, CustProv, CustPostal, CustCountry, CustHomePhone, CustBusPhone, CustEmail) values (@custUsername ,@custPassword ,@custFirstName ,@custLastName ,@custAddress ,@custCity ,@custProv ,@custPostal, @custCountry ,@custHomePhone ,@custBusPhone ,@custEmail)";
            SqlCommand com = new SqlCommand(insertQuery, conn);
            com.Parameters.Add("@custUsername", txtCustUserName.Text);
            com.Parameters.Add("@custPassword", txtCustPassword.Text);
            com.Parameters.Add("@custFirstName", txtCustFirstName.Text);
            com.Parameters.Add("@custLastName", txtCustLastName.Text);
            com.Parameters.Add("@custAddress", txtCustAddress.Text);
            com.Parameters.Add("@custCity", txtCustCity.Text);
            com.Parameters.Add("@custProv", txtCustProv.Text);
            com.Parameters.Add("@custPostal", txtCustPostal.Text);
            com.Parameters.Add("@custCountry", txtCustCountry.Text);
            com.Parameters.Add("@custHomePhone", txtCustHomePhone.Text);
            com.Parameters.Add("@custBusPhone", txtCustBusPhone.Text);
            com.Parameters.Add("@custEmail", txtCustEmail.Text);

            com.CommandType = CommandType.Text;

            com.ExecuteNonQuery();
            Response.Redirect("Manager.aspx");
            Response.Write("Registration is successful" );

            conn.Close();
        }
        catch(Exception ex)
        {
            Response.Write("Error:"+ex.ToString());
        }
    }
}

您正在创建一个小缺陷,只需将AddWithValue替换为Add 并添加以下行" com.CommandType = CommandType.Text;"

答案 2 :(得分:0)

如果您仍然收到错误,则可能是以下原因:

错误 :(字符串或二进制数据将被截断)

原因

1)您在数据库字段中输入的数据属于不同类型。

2)您在数据库字段中输入的数据大小比声明的数据大。

3)您没有将传入的数据表单转换为适当的字段类型。

我的猜测 :(您直接从字段输入数据)

补救措施

1)将字符串数据转换为String(收到的最后一个输入中的concat .toString())+根据其他数据类型转换数据,如int / float / etc

2)增加数据库中的字段输入大小(例如:城市(25)到城市(MAX))

答案 3 :(得分:0)

感谢所有人。 @JohnEphraimTugado回答了这个问题。发生错误是因为字段接收的输入大于字段接受的限制。

答案 4 :(得分:0)

错误说明错误,DB中有一个字段可以容纳更多的数据,而不是它的限制(以蹩脚的语言解释)。 请检查字段,确保您执行以下任一操作:

  1. 将发送到数据库的数据字符串修剪为特定长度,低于数据库中的字符数限制。
  2. 更改数据库字段的限制,以便它可以容纳更长的时间。