C# asp.net订阅表单写入数据库

C# asp.net订阅表单写入数据库,c#,asp.net,C#,Asp.net,我的任务之一是创建一个表单,当用户输入数据时,该表单将写入数据库。到目前为止,我已经取得了一些进展,但也有一些错误。这就是我目前所拥有的 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.

我的任务之一是创建一个表单,当用户输入数据时,该表单将写入数据库。到目前为止,我已经取得了一些进展,但也有一些错误。这就是我目前所拥有的

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
}
}
我还有一个在单独文件中创建的类

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
}


}
我得到的错误是

名称reg在当前上下文中不存在


任何人都可以告诉我哪里出了问题?

错误发生在
按钮1\u单击
。您需要在此方法中创建
注册
对象

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;
}
此外,您不需要此代码

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

错误发生在
按钮1\u单击
。您需要在此方法中创建
注册
对象

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;
}
此外,您不需要此代码

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

我想你忘了创建
注册
类的实例。这就是为什么它说name
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;
}

我想你忘了创建
注册
类的实例。这就是为什么它说name
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\u Registration中创建注册类对象

您需要在subscribe\u Registration中创建注册类对象

感谢您的快速响应,我补充说,现在它说“注册是一个名称空间,但像类型一样使用”,正如@Leopard建议使用
Registration.Registration reg=new Registration.Registration()
没有注意到名称空间也有相同的名称..是的,Codelahiru似乎很管用。你们真是帮了大忙,非常感谢,当我可以的时候,我一定会回来帮助别人。很高兴听到这个消息。永远帮助我。并确保将@Leopard的答案标记为正确答案。我尝试将其标记为正确答案,但我需要更多的声誉才能标记它。它说它已经被记录下来了。谢谢科德拉赫鲁!!!感谢您的快速响应,我补充说,现在它说“注册是一个名称空间,但像@Leopard建议的那样使用类型”
Registration.Registration reg=new Registration.Registration()
没有注意到名称空间也有相同的名称..是的,Codelahiru似乎很管用。你们真是帮了大忙,非常感谢,当我可以的时候,我一定会回来帮助别人。很高兴听到这个消息。永远帮助我。并确保将@Leopard的答案标记为正确答案。我尝试将其标记为正确答案,但我需要更多的声誉才能标记它。它说它已经被记录下来了。谢谢科德拉赫鲁!!!我在部分类注册中声明了对象?我在部分类注册中声明了对象?谢谢合作伙伴:)很快它告诉我“不能隐式地将类型“string”转换为“int”。因为年龄和电话号码是int”使用如下-
reg.age=convert.ToInt32(txtAge.Text)
reg.Phone=Convert.ToInt32(txtPhone.Text);
谢谢你codelahiru我真是太感谢你了,你帮我省去了很多麻烦。我的在线培训师的回复时间是几个小时,很难获得重新开始的动力。谢谢你,伙计,奏效了:)很快就告诉我了”无法将类型“string”隐式转换为“int”。因为年龄和电话号码是国际通用的,所以-
reg.age=Convert.ToInt32(txtAge.Text)
reg.Phone=Convert.ToInt32(txtPhone.Text)