penmas penmas - 1 month ago 12
ASP.NET (C#) Question

Asp.net Form Inserting Data into Wrong Fields in Database

I have an Asp.net form I am using to insert user data into a .mdb file using c#. Data is successfully entered, however it is not entering the person's e-mail address. Instead the persons Name (first entry) is being placed into the E-mail field in the Database and not putting anything in the field for the Name. How do I enter the e-mail into the e-mail column (pEmail) and the persons name (pName) into the name column in the database?

Form Code:

<form id="form1" runat="server">
<div style="height: 138px">

Enter Name:<asp:TextBox ID="TextBox1" runat="server" style="margin-left: 12px"></asp:TextBox>

<asp:RequiredFieldValidator
id="reqName"
ControlToValidate="TextBox1"
Style="color:Red"
ErrorMessage="Please enter your name!"
runat="server" />

<br />
Enter Email:&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

<asp:RegularExpressionValidator
id="ValidEmail"
ControlToValidate="TextBox2"
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="Invalid Email Entry"
runat="server" />

<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>

</div>
</form>


c# code for database entry:

using System.Data.OleDb;
using System.Configuration;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["northwind"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "insert into[Table1](pName)values(@nm)";
cmd.Parameters.AddWithValue("@nm", TextBox1.Text);
cmd.CommandText = "insert into[Table1](pEmail)values(@nm)";
cmd.Parameters.AddWithValue("@nm", TextBox2.Text);
cmd.Connection = con;
int a = cmd.ExecuteNonQuery();
if (a>0)
{
Label1.Text = "Inserted Sucessfully!";
}
}
}


And the database only has two columns for entry, one called pName and the second called pEmail.

Answer

Here is the C# code using one insert SQL query.

using System.Data.OleDb;
using System.Configuration;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        OleDbConnection con = new OleDbConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["northwind"].ToString();
        con.Open();
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = "insert into[Table1](pName, pEmail)values(@nm, @em)";
        cmd.Parameters.AddWithValue("@nm", TextBox1.Text);
        cmd.Parameters.AddWithValue("@em", TextBox2.Text);
        cmd.Connection = con;
        int a = cmd.ExecuteNonQuery();
        if (a>0)
        {
            Label1.Text = "Inserted Sucessfully!";
        }
    }
}

Because you are overwriting your insert query that inserts the name with the insert query that is inserting the email.

However, you should insert both values with the same query since they are part of the same record.

Comments