用户代码错误未处理 SqlException,但信息仍存储在数据库中 [英] SqlException was unhandled by user code error, but information still being stored on database

查看:37
本文介绍了用户代码错误未处理 SqlException,但信息仍存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用 Visual Studio 在 c# 中创建一个 asp.net web 应用程序,我需要一些有关数据库的帮助.我有一个已填写的注册表单,根据选择的单选按钮,信息存储在两个表中的一个中.我遇到的问题是当我点击应用程序上的提交按钮来存储数据时,它会抛出一条错误消息.奇怪的是,即使我收到错误消息,信息仍然存储在表中.

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:

在调整了一些东西后,我不再收到错误消息.现在,父单选按钮正在工作(将信息存储在父表中),但子单选按钮不起作用.(没有任何内容被发送到 children 表.

因此,在注释掉 'if (parentRadBtn.Checked)' 部分后,首先是 'if (child.RadBtn.Checked)' 部分,运行应用程序时会为孩子存储信息.因此,我的 if 语句似乎存在问题,并且程序未到达if (child.RadBtn.Checked)"部分.我试过把它变成一个if else"和其他各种东西,但它们都不起作用.任何人都可以发现需要更改哪些程序才能贯穿儿童部分?谢谢.

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.

表格

代码/错误信息

**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 = "";
                    }
                }
            }
        }
    }
}

}

推荐答案

在执行查询之前逐步检查您的代码,检查 userBox.Text 的值.列 childID 是唯一会导致此错误的列,并且数据库报告它为 0(或者那个 () 表示空字符串?).

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:

  1. 添加一个不允许空字符串作为合法 childID 值的约束;
  2. 命名所有约束,包括主键约束(PK_children 比你得到的更易读);

希望这会有所帮助.

这篇关于用户代码错误未处理 SqlException,但信息仍存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆