0

I posted this a few days ago How to know a row's value before its inserted in gridview? and i got this answer

SqlCommand cmdEvent = new SqlCommand("SELECT COUNT(date) FROM patients WHERE date= '2012/02/23'", yourSqlConnection);
object myCount;
if (yourSqlConnection.State == ConnectionState.Closed){ yourSqlConnection.Open(); }
myCount = cmdEvent.ExecuteScalar();
if (yourSqlConnection.State == ConnectionState.Open){ yourSqlConnection.Close(); }

if (myCount != null)
{
  if ((int)myCount >= 10)
  {
    // Logic here e.g myLabel.Text = "You have reached your maximum of 10 visits!";
    return;
  }
}

But now i dont need one row's value before its inserted, instead, i need to know the values of 2 rows and i have no idea of how to do it. Im trying to make a login and i need the user's id to make the session unique but i need also to validate if that query returns true and i did it as follow:

comando = new SqlCommand("SELECT user_name FROM login WHERE user_name=@user AND pass=@pass", conexion);
comando.Parameters.AddWithValue("@pass", Upass);
comando.Parameters.AddWithValue("@user", user);
object check_coincidence = comando.ExecuteScalar();

if ((string)check_coincidence == user)
{
     Session["USER"] = check_coincidence;
     Response.Redirect("someURL")
}

But i do not know how to gat the user's ID to make the session unique, i mean, take that value as a new session.

1 Answer 1

1

To get more results from the query, you will have to use the SqlReader.

comando = new SqlCommand("SELECT user_name, user_id FROM login WHERE user_name=@user AND pass=@pass", conexion);
comando.Parameters.AddWithValue("@pass", Upass);
comando.Parameters.AddWithValue("@user", user);

 SqlDataReader reader = commando.ExecuteReader();
        while (reader.Read())
        {
            var userName = Convert.ToString(reader["user_name"]);
            var userId = Convert.ToInt32(reader["user_id"]); 
        }

I would advice to follow some tutorials about this, it will help to understand the Sql-classes. Check this one for example: http://csharp-station.com/Tutorials/AdoDotNet/Lesson03.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

THANX MEN!! I REALLY APRECIATE!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.