GeeKaush
GeeKaush

Reputation: 33

How do you keep variables in Visual Studio C sharp

I am creating a web game using Visual Studio and asp.net.

Now, I want to navigate from form 1 to form 2. However, I dont know how to store the login name and password so that I can update records later from form 2.

This is my code in form 1 where i get the username of the person playing.

 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/>";
        }

Now, I want to go to form two and update his results. How can I do that.

Thank you.

Upvotes: 0

Views: 327

Answers (1)

Win
Win

Reputation: 62260

If I understand your question correctly, you need to store the value in session state so that you can retrieve in subsequence page request.

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

Upvotes: 4

Related Questions