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

时间:2016-08-31 02:20:21

标签: c# asp.net sql-server

只是尝试将用户数据输入从表单保存到数据库,但不断抛出异常。我是编码的新手,非常感谢您提供的任何帮助。这是我的代码。

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; }
}    
}

我的数据库程序

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:

请注意我在编程方面相当新,所以我还在学习。

我做错了吗?有人可以解释为什么会这样吗?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您没有为参数提供值。请参阅下面的代码,在定义参数后,参数的.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;
    }
}

此外,您要添加两次参数。我已经删除了第二组添加参数到命令。