当DR中没有数据时,尝试读取无效 [英] Invalid attempt to read when there is not data in DR

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

问题描述


我试图在我的ASP.NET网站上创建一个登录表单.目前,存在一些问题.我正在尝试合并功能,以便已登录的用户具有仅查看其个人资料的特权.登录页面上的代码是这样的:

Hi,
I am trying to create a login form on my ASP.NET website. Currently, there''s some problem. I am trying to incorporate the functionality such that the logged in user has the previlige to view only his profile. The code on the login page is this:

business.clsprofiles obj = new business.clsprofiles();
        Int32 a = obj.logincheck(TextBox3.Text, TextBox4.Text);
        if (a == -1)
        {
            Label1.Text = "Username/Password incorrect";
        }
        else
        {
            Session["cod"]= a;
            Response.Redirect("profile.aspx");
        }


登录后,用户将移动到该人登录后即可查看其个人资料的页面.会话正在从登录页面正确获取已登录人员的值,并将其成功传递到下一页.但是在此个人资料页面上的这里会发生错误,我认为下面的grid_bind()方法中的某处存在问题


After logging in, the user is moved to the page where the person can view his profile once logged in. Session is obtaining the value correctly of the logged in person from the login-page and successfully passing it on to the next page. But here on this profile page an error occurs and I think there is problem somewhere in the grid_bind() method below

public void grid_bind()
{
    business.clsprofiles obj = new business.clsprofiles();
    List<business.clsprofilesprp> objprp = new List<business.clsprofilesprp>();
    Int32 z = Convert.ToInt32(Session["cod"]);
    objprp = obj.fnd_profiles(z); //This line of code is passing an integer as required but does not get the desired result from the database
    GridView1.DataSource = objprp;
    GridView1.DataBind();
}



正如业务逻辑中的错误所言,当博士中不存在任何数据时,无效的读取尝试"



As the error in business logic says, " invalid attempt to read when no data is present in dr"

public List<clsprofilesprp> fnd_profiles(Int32 id)
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand cmd = new SqlCommand("fndpro", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            SqlDataReader dr = cmd.ExecuteReader();
            List<clsprofilesprp> obj = new List<clsprofilesprp>();
            while(dr.HasRows)
            {
                clsprofilesprp k = new clsprofilesprp();

                k.id = Convert.ToInt32(dr[0]);//Something wrong here?

                k.name = dr[1].ToString();
                k.password = dr[2].ToString();
                k.description = dr[3].ToString();
                k.created = Convert.ToDateTime(dr[4]);
                k.modified = Convert.ToDateTime(dr[5]);
                obj.Add(k);
            }
            dr.Close();
            cmd.Dispose();
            con.Close();
            return obj;
        }lesprp k = new clsprofilesprp();

        k.id = Convert.ToInt32(dr[0]);//Something wrong here?

                k.name = dr[1].ToString();
                k.password = dr[2].ToString();
                k.description = dr[3].ToString();
                k.created = Convert.ToDateTime(dr[4]);
                k.modified = Convert.ToDateTime(dr[5]);
                obj.Add(k);
            }
            dr.Close();
            cmd.Dispose();
            con.Close();
            return obj;

推荐答案

我相信您已经忘记在以下位置添加dr.Read():
I believe you''ve forgotten to add dr.Read() after:
while(dr.HasRows)
{
  // here
 ...
}


检索值之前,请使用dr.Read .


Use dr.Read before you retrieve values.


我没有可以快速测试此环境的环境,但是问题可能出在
I do not have an environment where I can test this quicky, but could the problem be in
Int32 z = Convert.ToInt32(Session[''cod'']);


也许

Session['''']

的返回值未正确转换为Int32.

is not being converted to Int32 correctly.


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

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