问题
So i'm using Visual Studio to create a asp.net web app in c# and I need some help regarding databases. I have a registration form that is filled out and depending on which radio button is selected, the information is stored in one of two tables. The issue I am having is when I hit the submit button on my app to store the data, it throws up an error message. The curious part is, the information still stores in the table even though I get the error message.
Can anyone help explain? I've attached pictures showing my code/error message and my tables. Thanks!
EDIT - This is my latest issue, posted from below:
After tweaking with some stuff, I am not getting the error message anymore. Now, the parent radio button is working (storing the info in parent table), but the child radio button is not. (nothing is being sent to the children table.
So after commenting out the 'if (parentRadBtn.Checked)' section so that the 'if (child.RadBtn.Checked)' section is first, information gets stored for children when running the app. So it seems there is an issue with my if statements and the program not reaching the 'if (child.RadBtn.Checked)' part. I have tried making it an 'if else' and various other things but none of them work. Can anyone spot what needs to be changed for the program to run through the children part? Thanks.
Tables
Code/error message
**Updated code**
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;
namespace Coursework
{
public partial class Registration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
successLabel.Text = ("Your submission is now being stored in our database");
}
}
protected void parentRadBtn_CheckedChanged(object sender, EventArgs e)
{
if (parentRadBtn.Checked)
{
firstNameBox.Text = string.Empty;
surnameBox.Text = string.Empty;
dayDobList.Text = string.Empty;
monthDobList.Text = string.Empty;
yearDobList.Text = string.Empty;
genderList.Text = string.Empty;
postcodeBox.Text = string.Empty;
teleBox.Text = string.Empty;
emailBox.Text = string.Empty;
userBox.Text = string.Empty;
passwordBox.Text = string.Empty;
genderList.Enabled = false;
dayDobList.Enabled = false;
monthDobList.Enabled = false;
yearDobList.Enabled = false;
surnameBox.Enabled = true;
postcodeBox.Enabled = true;
teleBox.Enabled = true;
emailBox.Enabled = true;
successLabel.Text = ("");
}
}
protected void passwordBox_TextChanged(object sender, EventArgs e)
{
}
protected void childRadBtn_CheckedChanged(object sender, EventArgs e)
{
else if (childRadBtn.Checked)
{
firstNameBox.Text = string.Empty;
surnameBox.Text = string.Empty;
dayDobList.Text = string.Empty;
monthDobList.Text = string.Empty;
yearDobList.Text = string.Empty;
genderList.Text = string.Empty;
postcodeBox.Text = string.Empty;
teleBox.Text = string.Empty;
emailBox.Text = string.Empty;
userBox.Text = string.Empty;
passwordBox.Text = string.Empty;
genderList.Enabled = true;
dayDobList.Enabled = true;
monthDobList.Enabled = true;
yearDobList.Enabled = true;
surnameBox.Enabled = false;
postcodeBox.Enabled = false;
teleBox.Enabled = false;
emailBox.Enabled = false;
successLabel.Text = ("");
}
}
protected void submitBtn_Click(object sender, EventArgs e)
{
SqlConnection connect = new SqlConnection("Data Source=THEBEAST;Initial Catalog=newregDB;Integrated Security=True;Pooling=False");
{
if (parentRadBtn.Checked)
{
SqlCommand pa = new SqlCommand("INSERT INTO parent(parentID, firstname, surname, postcode, telephone, email, password) VALUES (@parentID, @firstname, @surname, @postcode, @telephone, @email, @password)", connect);
pa.Parameters.AddWithValue("@parentID", userBox.Text);
pa.Parameters.AddWithValue("@firstname", firstNameBox.Text);
pa.Parameters.AddWithValue("@surname", surnameBox.Text);
pa.Parameters.AddWithValue("@postcode", postcodeBox.Text);
pa.Parameters.AddWithValue("@telephone", teleBox.Text);
pa.Parameters.AddWithValue("@email", emailBox.Text);
pa.Parameters.AddWithValue("@password", passwordBox.Text);
connect.Open();
pa.ExecuteNonQuery();
connect.Close();
if (IsPostBack)
{
userBox.Text = "";
firstNameBox.Text = "";
surnameBox.Text = "";
postcodeBox.Text = "";
teleBox.Text = "";
emailBox.Text = "";
passwordBox.Text = "";
}
else if (childRadBtn.Checked)
{
SqlCommand ca = new SqlCommand("INSERT INTO children(childID, firstname, dob, gender, password) VALUES (@childID, @firstname, @dob, @gender, @password)", connect);
ca.Parameters.AddWithValue("@childID", userBox.Text);
ca.Parameters.AddWithValue("@firstname", firstNameBox.Text);
ca.Parameters.AddWithValue("@dob", dayDobList.Text + monthDobList.Text + yearDobList.Text);
ca.Parameters.AddWithValue("@gender", genderList.Text);
ca.Parameters.AddWithValue("@password", passwordBox.Text);
connect.Open();
ca.ExecuteNonQuery();
connect.Close();
if (IsPostBack)
{
userBox.Text = "";
firstNameBox.Text = "";
dayDobList.Text = "";
monthDobList.Text = "";
yearDobList.Text = "";
genderList.Text = "";
passwordBox.Text = "";
}
}
}
}
}
}
}
回答1:
Step through your code checking the value of userBox.Text prior to executing the query. Column childID is the only one that can cause this error, and the database is reporting that it is 0 (or is that () meaning empty string?).
You can also try manually deleting the record each time you run your code - you should no longer get the error.
Additional changes you might want to consider:
- Add a constraint disallowing empty string as a legal childID value;
- Name all your constraints, including the primary key constraint (PK_children is more readable than what you are getting);
Hope this helps.
来源:https://stackoverflow.com/questions/41043510/sqlexception-was-unhandled-by-user-code-error-but-information-still-being-store