简体   繁体   English

asp.net订阅表写入数据库

[英]asp.net subscription form writing to database

Part of one of my assignments is to make a form that writes to a database when the user enters in the data. 我的一项任务是制作一个表单,当用户输入数据时将其写入数据库。 I've gotten so far but I'm getting a few errors. 到目前为止,我已经遇到了一些错误。 This is what I have so far. 到目前为止,这就是我所拥有的。

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 Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
    reg.Name = txtName.Text;
    reg.Age = txtAge.Text;
    reg.Sex = txtSex.Text;
    reg.Address = txtAddress.Text;
    reg.Email = txtEmail.Text;
    reg.Phone = txtPhone.Text;
    reg.InsertRegistration();
    phSuccess.Visible = true;
}

#region Methods
public void InsertRegistration()
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString);
    SqlCommand cmd = new SqlCommand("spRegistrationInsert", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter parameterName = new SqlParameter("@Name", SqlDbType.VarChar, 50);
    SqlParameter parameterAge = new SqlParameter("@Age", SqlDbType.VarChar, 50);
    SqlParameter parameterSex = new SqlParameter("@Sex", SqlDbType.VarChar, 50);
    SqlParameter parameterAddress = new SqlParameter("@Address", SqlDbType.VarChar, 50);
    SqlParameter parameterEmail = new SqlParameter("@Email", SqlDbType.VarChar, 100);
    SqlParameter parameterPhone = new SqlParameter("@Phone", SqlDbType.VarChar, 100);
    parameterName.Value = txtName;
    parameterAge.Value = txtAge;
    parameterEmail.Value = txtSex;
    parameterAddress.Value = txtAddress;
    parameterEmail.Value = txtEmail;
    parameterPhone.Value = txtPhone;
    cmd.Parameters.Add(parameterName);
    cmd.Parameters.Add(parameterAge);
    cmd.Parameters.Add(parameterSex);
    cmd.Parameters.Add(parameterAddress);
    cmd.Parameters.Add(parameterEmail);
    cmd.Parameters.Add(parameterPhone);
    try
    {
        conn.Open();
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        throw new Exception(ex.ToString());
    }
    finally
    {
        cmd.Dispose();
        conn.Close();
    }
}
}

#endregion


namespace Registration
{
    public partial class Registration : System.Web.UI.Page
 {
    #region Declarations
    Registration reg = new Registration();
    #endregion
}
}

I also have a class I made in a separate file 我也有一个在单独文件中制作的课程

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
{
    #region Declaration
    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; }
    #endregion
}


}

The errors I'm getting is 我得到的错误是

the name reg does not exist in current context 名称reg在当前上下文中不存在

Anyone could give me any pointers where I'm going wrong? 有人可以给我任何指示我要去哪里的地方吗?

The error is occuring in Button1_Click . 该错误发生在Button1_Click You need to create an object of Registration class in this method. 您需要使用此方法创建一个Registration classobject

protected void Button1_Click(object sender, EventArgs e)
{
    Registration.Registration reg = new Registration.Registration();    //Declare object of Registration class here
    reg.Name = txtName.Text;
    reg.Age = txtAge.Text;
    reg.Sex = txtSex.Text;
    reg.Address = txtAddress.Text;
    reg.Email = txtEmail.Text;
    reg.Phone = txtPhone.Text;
    reg.InsertRegistration();
    phSuccess.Visible = true;
}

Also you don't need this code. 您也不需要此代码。

namespace Registration
{
    public partial class Registration : System.Web.UI.Page
 {
    #region Declarations
    Registration reg = new Registration();
    #endregion
}
}

I think you forgot to create a instance of your Registration class. 我认为您忘记创建Registration类的实例。 That's why it says name reg does not appear. 这就是为什么它说名称reg不出现的原因。

protected void Button1_Click(object sender, EventArgs e)
{

    //Add this part
    Registration reg = new Registration();

    reg.Name = txtName.Text;
    reg.Age = txtAge.Text;
    reg.Sex = txtSex.Text;
    reg.Address = txtAddress.Text;
    reg.Email = txtEmail.Text;
    reg.Phone = txtPhone.Text;
    reg.InsertRegistration();
    phSuccess.Visible = true;
}

您需要在subscribe_Registration中创建一个注册类的对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM