我的一项任务的一部分是在用户输入数据时创建一个写入数据库的表单。我到目前为止已经收到了一些错误。这是我到目前为止所做的。
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在当前上下文中不存在
任何人都可以给我任何指示我出错的地方?
答案 0 :(得分:2)
Button1_Click
发生错误。您需要在此方法中创建object
Registration
class
。
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 :(得分:0)
我想您忘了创建Registration
课程的实例。这就是为什么名称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;
}
答案 2 :(得分:0)
您需要在 subscribe_Registration
中创建注册类的对象