尝试无效时,没有数据出现在博士读 [英] Invalid attempt to read when no data is present in dr

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

问题描述

我想创建我的ASP.NET网站登录表单。目前,有一些问题。我试图将使得登录的用户的功能有previlige只查看他的个人资料。登录页面的代码是这样的:

  business.clsprofiles的obj =新business.clsprofiles(); 
的Int32 A = obj.logincheck(TextBox3.Text,TextBox4.Text);
如果(A == -1)
{
Label1.Text =用户名/密码不正确;
}
,否则
{
会话[鳕鱼] = A;
的Response.Redirect(profile.aspx);
}



在登录后,用户被移动到页面,在这里的人可以查看他的个人资料登录后,会话从登录页面正确登录的取得亲自价值,并成功将其传递到下一个页面。但这里有个资料页上发生了错误,我认为有问题的地方在 grid_bind()方法如下

 公共无效grid_bind()
{
business.clsprofiles的obj =新business.clsprofiles();
名单,LT; business.clsprofilesprp> objprp =新的List< business.clsprofilesprp>();
的Int32 Z = Convert.ToInt32(会话[鳕鱼]);
objprp = obj.fnd_profiles(Z); //这行代码是传递一个整数作为必需的,但不能从数据库中获取
GridView1.DataSource = objprp期望的结果;
GridView1.DataBind();
}



作为业务逻辑错误说,无效尝试读取当没有数据存在于博士

 公开名单< clsprofilesprp> fnd_profiles(的Int32 ID)
{
如果(con.State == ConnectionState.Closed)
{
con.Open();
}
的SqlCommand CMD =新的SqlCommand(fndproCON);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(@ ID,SqlDbType.Int).value的= ID;
SqlDataReader的博士= cmd.ExecuteReader();
名单,LT; clsprofilesprp> OBJ =新的List< clsprofilesprp>();
,而(dr.HasRows)
{
clsprofilesprp K =新clsprofilesprp();

k.id = Convert.ToInt32(DR [0]); //了什么在这里?

k.name =博士[1]的ToString();
k.password =博士[2]的ToString();
k.description =博士[3]的ToString();
k.created = Convert.ToDateTime(博士[4]);
k.modified = Convert.ToDateTime(博士[5]);
obj.Add(K);
}
dr.Close();
cmd.Dispose();
con.Close();
返回OBJ;
} lesprp K =新clsprofilesprp();

k.id = Convert.ToInt32(DR [0]); //了什么在这里?

k.name =博士[1]的ToString();
k.password =博士[2]的ToString();
k.description =博士[3]的ToString();
k.created = Convert.ToDateTime(博士[4]);
k.modified = Convert.ToDateTime(博士[5]);
obj.Add(K);
}
dr.Close();
cmd.Dispose();
con.Close();
返回OBJ;


解决方案

您必须调用的DataReader .Read 来获取结果:

  SqlDataReader的博士= cmd.ExecuteReader(); 
dr.Read();
// ...



DataReader.Read 返回一个布尔值,所以如果你有超过1的结果,你可以这样做:

 虽然(dr.Read ())
{
//每个记录读取的数据在这里
}

此外,你想在有没有在这部分代码访问DR数据:

  k.id = Convert.ToInt32(DR [0]); //了什么在这里? 
k.name =博士[1]的ToString();
k.password =博士[2]的ToString();
k.description =博士[3]的ToString();
k.created = Convert.ToDateTime(博士[4]);
k.modified = Convert.ToDateTime(DR [5])


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");
        }

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;

解决方案

You have to call DataReader.Read to fetch the result:

SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
// ...

DataReader.Read returns a boolean, so if you have more than 1 result, you can do:

While (dr.Read())
{
  // read data for each record here
}

Moreover, you're trying to access the dr data when there's none in this part of the code:

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])

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

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