开发者

Problem with usage of multiple DataReaders in ASP.NET(C#)

开发者 https://www.devze.com 2023-03-11 08:55 出处:网络
SqlConnection conn1 = new SqlConnection(\"Server=ILLUMINATI;\" + \"Database=DB;Integrated Security= true\");
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.R开发者_开发知识库ead())
{
    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.


Declare string s out of hte loop

  string s;
  while (reader.Read())
     {
         s = reader["Role"].ToString();
     }


      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();
0

精彩评论

暂无评论...
验证码 换一张
取 消