I keep getting the following error in the browser 'Invalid attempt to read when no data is present.'

Answered I keep getting the following error in the browser 'Invalid attempt to read when no data is present.'

  • Wednesday, September 19, 2012 12:35 PM
     
     

    Hi,

    I'm new to .NET and trying to create a web application. I'm trying to query my database and read the results (only one row) into an SQLDataReader, then display them in a table on a web page. But when I do I keep getting the error below.

    Invalid attempt to read when no data is present.

    here's my code

            //draws a row of data in  the table
            public void drawDataRow(int itemID, int quantity)
            {
                int cost = 0;
                SqlDataReader rd = readFromDB(itemID);

                TableRow tr = new TableRow();

                TableCell tc1 = new TableCell();
                TableCell tc2 = new TableCell();
                TableCell tc3 = new TableCell();
                TableCell tc4 = new TableCell();
                TableCell tc5 = new TableCell();



                    tc1.Controls.Add(new LiteralControl("" + rd.GetInt32(0)));
                    tc2.Controls.Add(new LiteralControl("" + rd.GetString(1)));
                    tc3.Controls.Add(new LiteralControl("" + quantity));
                    tc4.Controls.Add(new LiteralControl("" + rd.GetInt32(2)));

                    cost = rd.GetInt32(2) * quantity;//calculate cost

                    bill += cost;//increment total bill

                    tc1.Controls.Add(new LiteralControl("" + cost));

                //add cells to row
                tr.Cells.Add(tc1);
                tr.Cells.Add(tc2);
                tr.Cells.Add(tc3);
                tr.Cells.Add(tc4);
                tr.Cells.Add(tc5);

                //add row to table
                Contents.Rows.Add(tr);

            }//end drawDataRow

    the above method calls the one below

           public SqlDataReader readFromDB(int itemID )
            {
                SqlConnection connect = new SqlConnection();
                
                    //specify connection string
                    connect.ConnectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

                    
                    SqlCommand comm = new SqlCommand("get_row", connect);
                    connect.Open();//open the connection

                    //notify SQLCommand object that it will be using a procedure
                    comm.CommandType = CommandType.StoredProcedure;
                    comm.Parameters.Add( new SqlParameter("@itemID", itemID));//pass a parameter to the procedure
                    SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);


                    return reader;

            }//end connectToDB

    Any help is welcome, I'm at my wits end.

    Thanx



    • Edited by Kalombo C Wednesday, September 19, 2012 2:06 PM
    •  

All Replies

  • Wednesday, September 19, 2012 3:37 PM
     
     Answered Has Code

    I don't use the DataReader at all, but I've "played" with it in the past. I believe that you're missing the call to rd.Read(). I think that you need to put all your code in a while (rd.Read()) loop like this:

    SqlDataReader rd = readFromDB(itemID);
    while (rd.Read())
    {
                TableRow tr = new TableRow();
                TableCell tc1 = new TableCell();
                TableCell tc2 = new TableCell();
                TableCell tc3 = new TableCell();
                TableCell tc4 = new TableCell();
                TableCell tc5 = new TableCell();
                    tc1.Controls.Add(new LiteralControl("" + rd.GetInt32(0)));
                    tc2.Controls.Add(new LiteralControl("" + rd.GetString(1)));
                    tc3.Controls.Add(new LiteralControl("" + quantity));
                    tc4.Controls.Add(new LiteralControl("" + rd.GetInt32(2)));
                    cost = rd.GetInt32(2) * quantity;//calculate cost
                    bill += cost;//increment total bill
                    tc1.Controls.Add(new LiteralControl("" + cost));
                //add cells to row
                tr.Cells.Add(tc1);
                tr.Cells.Add(tc2);
                tr.Cells.Add(tc3);
                tr.Cells.Add(tc4);
                tr.Cells.Add(tc5);
                //add row to table
                Contents.Rows.Add(tr);
    }


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

  • Thursday, September 20, 2012 8:36 AM
     
     
    It worked!!!!!!!!!  Thanx Geek Goddess :-))))))
  • Friday, September 21, 2012 4:35 AM
     
     

    You're welcome, Kalombo!!!! Glad I could help!!   Could you come back and mark my post as the answer?


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com