2011-05-16 8 views
0

Visual Studioasp.net을 사용하여 웹 게임을 만들고 있습니다.어떻게 Visual Studio C에서 변수를 유지합니까

지금, 나는 내가 나중에 폼에서 레코드를 업데이트 할 수 있도록 2.

이 내 코드 로그인 이름과 암호를 저장하는 방법을 잘 모릅니다, 그러나 (2)을 형성하는 형태 1에서 이동하려면 양식 1 어디 사람이 연주의 사용자 이름을 얻을.

SqlCommand cmd = new SqlCommand 
    ("select UserName from UserData where UserName = @name", conn); 
      cmd.Parameters.Add(new SqlParameter("@name", TextBox2.Text.ToString())); 
      // 2. Call Execute reader to get query results 
      SqlDataReader rdr = cmd.ExecuteReader(); 

      //Label1.Text = "Welcome"; 
      while (rdr.Read()) 
      { 
       Label1.Text = "Welcome " + rdr["UserName"].ToString() + 
            ". Press the Play Button to Play"; 
       Button3.Enabled = true; 
      } 

      if (rdr.HasRows == false) 
      { 
       // Button3.Enabled = false; 
       Label1.Text = "Sorry You arent in our system. 
           Please Register Yourself Before Starting"; 
      } 

     } 
     catch (Exception ex) 
     { 
      // Label1.Text = "Error: " + ex.Message + "<br/>"; 
     } 

이제 두 가지를 만들고 결과를 업데이트하고 싶습니다. 어떻게해야합니까?

감사합니다.

답변

4

질문을 올바르게 이해하면 세션 결과를 하위 페이지 요청에서 검색 할 수 있도록 값을 저장해야합니다.

// Form 1 
while (rdr.Read())    
{     
    Page.Session["UserName"] = rdr["UserName"].ToString(); 
    Label1.Text = "Welcome " + rdr["UserName"].ToString() + ". Press the Play Button to Play";  
    Button3.Enabled = true;    
} 

// Form 2   
string username = Page.Session["UserName"].ToString(); // Retrieve in form 2 
+0

감사합니다. DId가 작동합니다. 그러나 형식 2에서는 객체 참조가 객체의 인스턴스로 설정되지 않았다고 말합니다. 이 문제를 어떻게 해결할 수 있는지 알고 있습니까? – GeeKaush

+0

기본적으로 값은 양식 1에 저장되지 않았습니다. 코드를 업데이트했습니다. 오류가 계속 발생하면 pls는 양식 2 코드를 업로드합니다. – Win

관련 문제