Invalid attempt to read if no data in dr

I am trying to create a login form on my ASP.NET website. There are currently some problems. I am trying to enable features such that the logged in user has a view to view only his profile. Code on the login page:

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 entering the system, the user moves to a page where a person can view his profile after entering the system. The session receives the correct value for logging in from the account and successfully transfers it to the next page, But here, on this profile page, an error occurs, and I think there is a problem somewhere in the method grid_bind()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, "an incorrect attempt to read when there is no data 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;
+3
3

DataReader.Read, :

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

DataReader.Read , , , :

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

, dr, :

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

...

while (dr.HasRows)
{
   /* If this loop is entered, it will run 
    * indefinitely until the datareader miraculously 
    * loses all its rows in a hole somewhere */
}

, ... , . , :

while (dr.Read())
{
   /* Do something with the current record */
}

dr.Read() true false , . , . , dr.Read(), true, , true , Read(), .

dr.HasRows - , , datareader ..., , , . , , - , - , :

if (dr.HasRows)
{
    while (dr.Read())
    {
        /* Display data for current row */
    }
}
else
{
    Console.WriteLine("I didn't find any relevant data.");
}
+3

datareader, . .. dr while, datareader , - , "//- ?".

0

Source: https://habr.com/ru/post/1784143/


All Articles