SQL错误,无法注册用户,Asp.net使用C#

时间:2016-11-18 05:35:54

标签: c# asp.net

我一直在努力创建一个简单的登录&用于学习/测试目的的注册表。我想,最初这个表单正在运行,但我现在注意到我收到了一个SQL错误。我仔细检查了所有标签,一切似乎都匹配了。有没有人注意到代码的任何明显错误?

  

sql错误:System.Data.OleDb.OleDbException(0x80040E14):INSERT   INTO语句包含以下未知字段名称:   'txtUsername'。确保您已正确输入名称,然后尝试   再次操作。在   System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult   hr)at   System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS   dbParams,Object& executeResult)at   System.Data.OleDb.OleDbCommand.ExecuteCommandText(对象&安培;   executeResult)at   System.Data.OleDb.OleDbCommand.ExecuteCommand(的CommandBehavior   行为,对象& executeResult)at   System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(的CommandBehavior   行为,String方法)at   System.Data.OleDb.OleDbCommand.ExecuteNonQuery()at   ASP.register_aspx.ProcessRegistration(Object sender,EventArgs e)in   e:\ server \ user \ register.aspx:第60行

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<%@ Page Language="C#" Debug="true" %>

<script runat="server">
    void Page_Load()
    {

    }

    void ProcessRegistration(object sender, EventArgs e)
    {
        string connstr
           = ConfigurationManager.ConnectionStrings["northwind"].ConnectionString;
        string strSQL
           = "SELECT * from  customers ";

        OleDbConnection conn = new OleDbConnection(connstr);
        OleDbCommand cmd = new OleDbCommand(strSQL, conn);
        OleDbDataReader rdr;

        try
        {
            conn.Open();
            //OleDbDataAdapter aa = new OleDbDataAdapter(strSQL, conn);
            //DataSet ds = new DataSet();
            //aa.Fill(ds);
            //rdr = cmd.ExecuteReader();
            //if (rdr.HasRows)
            //{
            //    lblNameInUse.Text = "Sorry that name is taken.";
            //    lblResult.Text = "";
            //    rdr.Close();
            //    return;
            //}
        }
        catch (Exception ex)
        {
            lblResult.Text = "error in connecting " + ex.ToString();
            return;
        }
        //  rdr.Close();
        string strDate = DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss tt");


        strSQL = "Insert into Table1 (txtUsername,txtPassword,FirstName,LastName) Values ('" + txtUsername.Text + "','" + txtPassword.Text + "','" + FirstName.Text + "','" + LastName.Text + "')";

</script>

    <form runat="server">
        <h3>Registration Form</h3>
        <p>All fields required.</p>
        Your user name:<br />
        <asp:TextBox ID="txtUsername" runat="server" />
        <asp:Label ID="lblNameInUse" runat="server" /><br />
        <asp:RequiredFieldValidator runat="server"
            ID="okUserName" ControlToValidate="txtUsername"
            ErrorMessage="Please type a user name."
            Style="color: Red" /><br />
        Email Address:
        <asp:TextBox ID="txtEmail" runat="server" />
       <%-- <asp:RegularExpressionValidator
            ID="ValidEmail"
            ControlToValidate="txtEmail"
            Style="color: Red"
            ValidationExpression="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
            ErrorMessage="Please enter a valid e-mail address. "
            runat="server" />--%><br>
        Password:
        <asp:TextBox ID="txtPassword"
            TextMode="Password" runat="server" /><br>
        Confirm:
        <asp:TextBox ID="PasswordConfirm"
            TextMode="Password" runat="server" />
        <asp:CompareValidator
            ID="ComparePasswords"
            ControlToValidate="PasswordConfirm"
            ControlToCompare="txtPassword"
            Style="color: Red;"
            ErrorMessage="Passwords did not match.  Please check and try again."
            runat="server" />

        <br>
        <br>
        First Name:
        <asp:TextBox ID="FirstName" runat="server" />
      <%--  <asp:RegularExpressionValidator
            ID="ValidFN"
            ControlToValidate="FirstName"
            Style="color: Red"
            ValidationExpression="\w+"
            ErrorMessage="Please enter your first name."
            runat="server" />--%><br>
        Last Name:
        <asp:TextBox ID="LastName" runat="server" />
      <%--  <asp:RegularExpressionValidator
            ID="ValidLN"
            ControlToValidate="LastName"
            Style="color: Red"
            ValidationExpression="\w+"
            ErrorMessage="Please enter your last name."
            runat="server" />--%><br>


        <asp:Button runat="server" Text="Submit" OnClick="ProcessRegistration" /><br>

        <p>
            <asp:Label ID="lblResult" runat="server" />
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我实际上错误地标记了数据库中的列。