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.

share|improve this question

2 Answers

up vote 2 down vote accepted

Declare string s out of hte loop

  string s;
  while (reader.Read())
     {
         s = reader["Role"].ToString();
     }
share|improve this answer
Thank you...Can I do it with in the same connection. – ILLUMINATI7590 Jun 8 '11 at 6:03
1  
Its String variable, if you assign value and you can retrieve in the scope of variable, it does't matter connection. – Muhammad Akhtar Jun 8 '11 at 6:05
      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();
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.