2016-08-26 3 views
0

내 과제 중 하나는 사용자가 데이터를 입력 할 때 데이터베이스에 쓰는 양식을 만드는 것입니다. 나는 지금까지 얻었지만 몇 가지 오류가 발생합니다. 이것은 내가 지금까지 가지고있는 것이다.asp.net 가입 양식 데이터베이스에 쓰기

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 
} 


} 

내가 갖는 오류는 이름 등록이 현재 컨텍스트

을 존재하지 않는

나는 또한 내가 별도의 파일로 만든 클래스가

입니다

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

누구나 내가 잘못 가고있는 부분을 알려줄 수 있습니까?

답변

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

빠른 응답을 주셔서 감사합니다. 지금은 '등록은 네임 스페이스이지만 형식처럼 사용됩니다'라고 덧붙였습니다. – user6725507

+0

@Leopard는 'Registration.Registration reg = new Registration.Registration();' 네임 스페이스에 대해서도 같은 이름이 있습니다 .. – SilentCoder

+0

Codelahiru를 사용하는 것 같았습니다. 너희들은 정말 도움이된다. 나는 다른 사람들을 돕기 위해 돌아올 수있을 때 그렇게 많이 감사한다. – user6725507

2

Button1_Click에 오류가 발생했습니다. 이 방법으로 objectRegistrationclass으로 만들어야합니다.

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 
} 
} 
+0

감사 메이트 'int'. 나이와 전화 번호가 int 값 이니까 – user6725507

+0

이렇게 사용하십시오 -'reg.Age = Convert.ToInt32 (txtAge.Text); 그리고'reg.Phone = Convert.ToInt32 (txtPhone.Text); ' – SilentCoder

+0

고맙습니다. 코델라 히루 나는 두통을 많이 덜어 줘서 고맙다고 생각하지 않아. 온라인 트레이너의 반응 시간은 어때? rs와 그것으로 돌아 오기 위하여 동기 부여를 얻는 그것의 단단한. – user6725507

0

당신은 객체를 생성 할 필요가 클래스 subscribe_Registration 에 등록

: 자사에 "할 수 implicity하지 변환 타입 '문자열'나를 tellling 그냥 빨리 일을했다
+0

부분 클래스 등록에서 객체를 선언 했습니까? – user6725507