没有数据时无效尝试读取。 [英] Invalid attempt to read when no data is present.

查看:65
本文介绍了没有数据时无效尝试读取。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿朋友......我的代码中出现了这个错误..我的sqldatareader没有从数据库中读取任何行...虽然我在表格中插入了很多数据。



这里是代码:

  public   partial   class 欢迎:System.Web.UI.Page 
{

public string username;

public string fname;
public string lname;


受保护 void Page_Load( object sender,EventArgs e)
{
bindTable2();
}

受保护 void bindTable2()
{

string sqlSel = select * from Table2,其中username =' + username + ';

SqlConnection con = new SqlConnection();
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings [ ApplicationServices]。ConnectionString ;
con.Open();

SqlCommand cmd = new SqlCommand(sqlSel,con);
SqlDataReader sdr = cmd.ExecuteReader();


sdr.Read();


username = sdr [ username]。ToString ();

fname = sdr [ fname]。ToString();

........... etc
sdr.close();

}

}



如果我正在使用:

  if (sdr.HasRows)
{
while (sdr.Read())
{

// 此处所有数据
}
sdr.Close();
}
else
{
Response.Write( 我没有找到任何相关数据);

}
}



然后它自动将我的代码带到else语句。它不是从表2中读取任何数据。

不知道为什么....请帮助我。

解决方案

其中你在初始化用户名变量吗?如果它是空白的,你的sql查询可能不会返回任何行。


也许查询没有发回数据,因为变量username还没有值。



如果查询返回所需数据,请检查数据库管理员。


string sqlSel =select * from Table2 where username ='+ username +';你有断点来查看这段代码中用户名的值吗?



我认为每次加载页面时用户名都没有值

Hey Friends... I'm getting this error in my code.. that my sqldatareader not reading any row from database.. though i have inserted many data in my table.

here is the Code:

public partial class Welcome : System.Web.UI.Page
{

    public string username; 

    public string fname;
    public string lname;


    protected void Page_Load(object sender, EventArgs e)
    {
        bindTable2();
    }

    protected void bindTable2()
    {

        string sqlSel = "select * from Table2 where username ='" + username + "' ";

        SqlConnection con = new SqlConnection();
        con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
        con.Open();

        SqlCommand cmd = new SqlCommand(sqlSel, con);
        SqlDataReader sdr = cmd.ExecuteReader();


        sdr.Read();
            

        username = sdr["username"].ToString();

                fname = sdr["fname"].ToString();

                ........... etc
                sdr.close();
          
     }
        
}


And if i'm using :

if (sdr.HasRows)
        {
            while (sdr.Read())
            {

                //All Data here
            }
            sdr.Close();
        }
        else
        {
           Response.Write("I didn't find any relevant data");
          
        }
     }


Then it automatically taking my code to else statement. It's not reading any data from Table2.
don't know why.... Please help me out in it.

解决方案

Where are you initialising te username variable? if its blank your sql query might not return any rows.


Maybe the query isn't sending data back because the variable username has no value yet.

Check in your DB manager if that query returns the desired data.


string sqlSel = "select * from Table2 where username ='" + username + "' "; have you make a breakpoint to see the username's value in this code ?

I think username hasn't got a value everytime the page is loaded


这篇关于没有数据时无效尝试读取。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆