I have been working on creating a simple log-in & registration form for learning/testing purposes. Initially this form was working, I thought, but I noticed now that I am getting an SQL error. I double checked all the labels and everything seems to be matching up. Does anyone notice anything glaringly wrong with the code?
sql error: System.Data.OleDb.OleDbException (0x80040E14): The INSERT INTO statement contains the following unknown field name: 'txtUsername'. Make sure you have typed the name correctly, and try the operation again. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at ASP.register_aspx.ProcessRegistration(Object sender, EventArgs e) in e:\server\user\register.aspx:line 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>
I actually mislabeled the columns in the database.
User contributions licensed under CC BY-SA 3.0