3

new problem out there. While reading data from database and when it runs out of records, it simply says "Invalid attempt to read when no data is present", so the question is how to prevent that?

Here's my code:

{
    Label3.Text = Request.QueryString["Username"];

}

SqlConnection con = new SqlConnection("Data Source=JEVGENIJ-PC;Initial Catalog=ViaFitness;Integrated Security=True");
static SqlDataReader dr;
SqlCommand cmd;

protected void Button1_Click(object sender, EventArgs e)
{

    con.Open();

    SqlCommand cmd = new SqlCommand("select * from Summary where UserName='"+Label3.Text+ "'", con);

    dr = cmd.ExecuteReader();
    dr.Read();
    Label3.Text = dr[2].ToString();
    Label1.Text = dr[1].ToString();
    Label2.Text = dr[0].ToString();

}
 protected void Button2_Click(object sender, EventArgs e)
{
    dr.Read();
    Label3.Text = dr[2].ToString();
    Label1.Text = dr[1].ToString();
    Label2.Text = dr[0].ToString();
    con.Close();
}

}

0

4 Answers 4

2

You should make sure you have data in your reader:

protected void Button1_Click(object sender, EventArgs e)
{

    con.Open();

    SqlCommand cmd = new SqlCommand("select * from Summary where UserName='"+Label3.Text+ "'", con);

    dr = cmd.ExecuteReader();
    if (dr.Read())
    {
        Label3.Text = dr[2].ToString();
        Label1.Text = dr[1].ToString();
        Label2.Text = dr[0].ToString();
    }

}
 protected void Button2_Click(object sender, EventArgs e)
{
    if (dr.Read())
    {
        Label3.Text = dr[2].ToString();
        Label1.Text = dr[1].ToString();
        Label2.Text = dr[0].ToString();
    }
    con.Close();
}

Check out the MSDN-page for more info: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read.aspx

Sign up to request clarification or add additional context in comments.

Comments

2

call dr.read before every fetch. check if it is true , which means data is present

MSDN SqlDataReader.Read Method - Advances the SqlDataReader to the next record.

SqlDataReader reader = command.ExecuteReader();

// Call Read before accessing data. 
while (reader.Read())
{
        ReadSingleRow((IDataRecord)reader);
}

// Call Close when done reading.
reader.Close();

Comments

1

Datareader Read() method returns bool value. If it returns true, it succsesfully read next record, if it returns false - it is not read record because there is no more. So you should test result of Read method and then try to access to its values.

Comments

0

Read() jumps to the next record. Is there nothing you cant' acces this. Try this for only one record

If(dr.Read()) {
    Label3.Text = dr[2].ToString();
    Label1.Text = dr[1].ToString();
    Label2.Text = dr[0].ToString();
}

or this for all

While(dr.Read())) {
    ' do something
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.