c# &异常处理ASP.. NET在尝试将数据保存到数据库时

本文关键字:数据 保存 数据库 异常处理 ASP NET | 更新日期: 2023-09-27 18:04:45

只是试图将用户数据输入从表单保存到数据库,但一直抛出异常。我在编码半新,并感谢您能提供的任何帮助。这是我的代码。

c#

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;
using System.Configuration;
using System.Data;
 public partial class subscribe_Registration : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
    Registration.Registration reg = new Registration.Registration();
    reg.Name = txtName.Text;
    reg.Age = Int32.Parse(txtAge.Text);
    reg.Sex = txtSex.Text;
    reg.Address = txtAddress.Text;
    reg.Email = txtEmail.Text;
    reg.Phone = Int32.Parse(txtPhone.Text);
    this.InsertRegistration(reg);
    phSuccess.Visible = true;
}
public void InsertRegistration(Registration.Registration reg)
{
    try
    {
        using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString)) 
        {
            using (var cmd = new SqlCommand("dbo.Procedure", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                var parameterName = new SqlParameter("@Name", SqlDbType.VarChar, 50);
                var parameterAge = new SqlParameter("@Age", SqlDbType.VarChar, 50);
                var parameterSex = new SqlParameter("@Sex", SqlDbType.VarChar, 50);
                var parameterAddress = new SqlParameter("@Address", SqlDbType.VarChar, 50);
                var parameterEmail = new SqlParameter("@Email", SqlDbType.VarChar, 100);
                var parameterPhone = new SqlParameter("@Phone", SqlDbType.VarChar, 100);
                cmd.Parameters.Add(parameterName);
                cmd.Parameters.Add(parameterAge);
                cmd.Parameters.Add(parameterSex);
                cmd.Parameters.Add(parameterAddress);
                cmd.Parameters.Add(parameterEmail);
                cmd.Parameters.Add(parameterPhone);
                cmd.Parameters.Add(parameterName);
                cmd.Parameters.Add(parameterAge);
                cmd.Parameters.Add(parameterSex);
                cmd.Parameters.Add(parameterAddress);
                cmd.Parameters.Add(parameterEmail);
                cmd.Parameters.Add(parameterPhone);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
}

我的班级也是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace Registration
{
public class Registration
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Sex { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
    public int Phone { get; set; }
}    
}

My database procedure

CREATE PROCEDURE [dbo].[Procedure]
@Name VARCHAR(50),
@Age VARCHAR(50),
@Sex VARCHAR(50),
@Address VARCHAR(50),
@Email VARCHAR(100),
@Phone VARCHAR(100)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO Users(Name, Age, Sex, [Address], Email, Phone)
VALUES(@Name, @Age, @Sex, @Address, @Email, @Phone)
END
GO

每当我在表单的字段中输入数据时,它就会出现

异常
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=App_Web_5rrjgvhs
StackTrace:
   at subscribe_Registration.InsertRegistration(Registration reg) in c:'Users'*mycomputer*'Documents'Visual Studio 2015'WebSites'WebSite1'subscribe'Registration.aspx.cs:line 68
   at subscribe_Registration.Button1_Click(Object sender, EventArgs e) in c:'Users'*mycomputer*'Documents'Visual Studio 2015'WebSites'WebSite1'subscribe'Registration.aspx.cs:line 25
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:

请注意,我是一个相当新的编程,所以我还在学习。

我做错了什么吗?有人能解释一下为什么会这样吗?

c# &异常处理ASP.. NET在尝试将数据保存到数据库时

您没有为参数提供值。如下代码所示,定义参数后,下一行设置参数的.Value属性。

public void InsertRegistration(Registration.Registration reg)
{
    try
    {
        using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString))
        {
            using (var cmd = new SqlCommand("dbo.Procedure", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                var parameterName = new SqlParameter("@Name", SqlDbType.VarChar, 50);
                parameterName.Value = reg.Name;
                var parameterAge = new SqlParameter("@Age", SqlDbType.VarChar, 50);
                parameterAge.Value = reg.Age;
                var parameterSex = new SqlParameter("@Sex", SqlDbType.VarChar, 50);
                parameterSex.Value = reg.Sex;
                var parameterAddress = new SqlParameter("@Address", SqlDbType.VarChar, 50);
                parameterAddress.Value = reg.Address;
                var parameterEmail = new SqlParameter("@Email", SqlDbType.VarChar, 100);
                parameterEmail.Value = reg.Email;
                var parameterPhone = new SqlParameter("@Phone", SqlDbType.VarChar, 100);
                parameterPhone.Value = reg.Phone;
                cmd.Parameters.Add(parameterName);
                cmd.Parameters.Add(parameterAge);
                cmd.Parameters.Add(parameterSex);
                cmd.Parameters.Add(parameterAddress);
                cmd.Parameters.Add(parameterEmail);
                cmd.Parameters.Add(parameterPhone);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

同样,您添加了两次参数。我已经删除了命令的第二组添加参数