Click here to Skip to main content
15,914,109 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");
        }

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;
Posted

I believe you've forgotten to add dr.Read() after:
C#
while(dr.HasRows)
{
  // here
 ...
}

Use dr.Read before you retrieve values.
 
Share this answer
 
v2
I do not have an environment where I can test this quicky, but could the problem be in
Int32 z = Convert.ToInt32(Session['cod']);

Maybe the return from
Session['']
is not being converted to Int32 correctly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900