1
SqlConnection conn1 = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");

SqlCommand COMM = new SqlCommand("select Role from Login where User_Name='admin'", conn1);
conn1.Open();

SqlDataReader reader = COMM.ExecuteReader();
while (reader.Read())
{
    string s = reader["Role"].ToString();
}
reader.Close();
conn1.Close();

SqlConnection conn = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");
SqlCommand comm = new SqlCommand("Select * from FileUpload where UploadedBy='"+NAME+"'",conn);

try
{
    conn.Open();

    SqlDataReader rdr = comm.ExecuteReader();
    if (s.Equals("admin"))
    {
        GridView1.DataSource = rdr;
        GridView1.DataBind();
    }

    if(s.Equals("teacher"))
    {
         GridView2.DataSource = rdr;
         GridView2.DataBind();
    }

    rdr.Close();
    //reader.Close();
}
catch
{
    conn.Close();
}

I'm getting error in the below connection saying s does not exist in the current context. How to use the multiple datareaders please help me.

2 Answers 2

2

Declare string s out of hte loop

  string s;
  while (reader.Read())
     {
         s = reader["Role"].ToString();
     }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you...Can I do it with in the same connection.
Its String variable, if you assign value and you can retrieve in the scope of variable, it does't matter connection.
1
      SqlConnection conn1 = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");
      SqlCommand COMM = new SqlCommand("select Role from Login where User_Name='admin'", conn1);
      conn1.Open();
      SqlDataReader reader = COMM.ExecuteReader();
      string s = String.Empty;
      while (reader.Read())
          s = reader["Role"].ToString();

Comments

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.