数据表:Login
userID userName          userPwd            Dept
1 kang               Admin               信部              
2 DaLing             Ling                总经办            
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection con=new SqlConnection("server=.;database=aico;uid=sa;pwd=sa");
con.Open();
SqlCommand cmd=new SqlCommand("select * from Login where userID="+1,con);
SqlDataReader myReader=cmd.ExecuteReader();
Response.Write(myReader["userID"].ToString());
// 在此处放置用户代码以初始化页面
}
提示如下错误:
在没有任何数据时进行无效的读取尝试。 

解决方案 »

  1.   

    select * from Login where userID= 1 的时候没有数据吧?..查询分析器里试下
      

  2.   

    SqlCommand cmd=new SqlCommand("select * from Login where userID="+1,con);可以这么写?  1是int类型  你前面是string类型
    还有就是
    SqlDataReader myReader=cmd.ExecuteReader();
    后面还要写上:
    while(myReader.Read())
    {
      Response.Write(myReader["userID"].ToString());}
      

  3.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    SqlConnection con=new SqlConnection("server=.;database=aico;uid=sa;pwd=sa");
    con.Open();
    SqlCommand cmd=new SqlCommand("select * from Login where userID="+1,con);
    SqlDataReader myReader=cmd.ExecuteReader();
    if(myReader != null)
    {
    Response.Write(myReader["userID"].ToString());
    }
    // 在此处放置用户代码以初始化页面
    }
      

  4.   

    这句应该是
    SqlCommand cmd=new SqlCommand("select * from Login where userID=1",con);
      

  5.   

    哦写错了,
    应该是
    if ( r.Read())
      

  6.   

    1.在Response.Write(myReader["userID"].ToString());上面
    加上myReader.Read();
    2.用SqlDataReader 最好捕捉异常
      

  7.   

    while(myReader.Read()) //必须调用 Read 来开始访问任何数据
    {
    Response.Write(myReader["userID"].ToString());
    }
      

  8.   

    if(myReader.Read() && myReader["userID"]!=null)
    {
    Response.Write(myReader["userID"].ToString());
    }
      

  9.   

    SqlCommand cmd=new SqlCommand("select * from Login where userID="+1,con);
    这句话错了吧
    应该是SqlCommand cmd=new SqlCommand("select * from Login where userID=1,con);

      

  10.   

    SqlCommand cmd=new SqlCommand("select * from Login where userID="+1,con);可以这么写?  1是int类型  你前面是string类型
    还有就是
    SqlDataReader myReader=cmd.ExecuteReader();
    后面还要写上:
    while(myReader.Read())
    {
      Response.Write(myReader["userID"].ToString());}
    -------------------------正解
      

  11.   

    加上个判断..
    if (myReader.Read())
    {
         Response.Write(myReader["userID"].ToString());
    }
      

  12.   

    SqlDataReader myReader=cmd.ExecuteReader();//这句执行完myReader里并没有任何数据
    必须myReader.Read()才会读取一行数据,然后就可以通过myReader["userID"]获取字段数据了
    Response.Write(myReader["userID"].ToString())
      

  13.   

    读数据前,想要判断myReader.Read(),判断myreader里面有没有数据
      

  14.   

    SqlConnection con=new SqlConnection("server=.;database=aico;uid=sa;pwd=sa");
    con.Open();
    SqlCommand cmd=new SqlCommand("select * from Login where userID="+"1",con);//数据类型
    SqlDataReader myReader=cmd.ExecuteReader();
    myReader.Read();//开始读取
    Response.Write(myReader["userID"].ToString());
    myReader.Close();//关闭读取器
    con.Close();//关闭连接
      

  15.   

    while(myReader.Read())
    {
    Response.Write(myReader["userID"].ToString());}