Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,
I have an information system which records beach volleyball Players, Teams, Tournaments, Groups, Matches... When I click on select Tournament it redirects to new page - Detail of Tournament, where are shown registrations, groups A,B,C,... And also you can registrate to tour if it is not played Tournament. Session ["Tournament"] is filled with tournament id before it redirects, but it still produce this error, does anyone know why?? Session ["Nickname"] represents logged user.

Error at:
C#
if (Session["Nickname"] == null || DateTime.Now > client.Reader.GetDateTime(1))

throw: Invalid attempt to read when no data is present.

Code:
C#
using Database.Client;
...
protected void Page_Load(object sender, EventArgs e)
    {
        Client client = new Client();
        try
        {
            
            client.Connect();
            client.Command = client.Connection.CreateCommand();
            client.Command.CommandText = "select Name, Tournament_Begining from Tournament where ID = @id";
            client.Command.Parameters.Add("@id", System.Data.SqlDbType.Int, 10, "ID").Value = Session["Tournament"];
            client.Reader = client.Command.ExecuteReader();
            while (client.Reader.Read())
            {
                lblTournamentName.Text = client.Reader.GetString(0);
            }
            if (Session["Nickname"] == null || DateTime.Now > client.Reader.GetDateTime(1))
            {
                Panel3.Visible = false;
            }
            else
            {
                Panel3.Visible = true;
                lblRegister.Visible = false;
            }
            client.Disconnect();
        }
        catch (Exception)
        {
            
            throw;
        }


Thank You for replies!

-Pepin z Hane
Posted
Updated 18-Oct-12 13:41pm
v2
Comments
R. Giskard Reventlov 18-Oct-12 19:22pm    
and the error is?
Sergey Alexandrovich Kryukov 18-Oct-12 19:26pm    
And..?
--SA
Pepin z Hane 18-Oct-12 19:38pm    
Invalid attempt to read when no data is present.
aidin Tajadod 18-Oct-12 20:28pm    
It seems you have problem with you client.reader and not session! what is that client.reader? To me it seems each time you call reader, the pointer will go forward! is this a correct assumption? if so, you may want to read once and put it in a variable and use the variable.
[no name] 18-Oct-12 21:11pm    
"DateTime.Now > client.Reader.GetDateTime(1)", you have already read everything in the reader and you are trying to read it again.

Looks to be a situation where client.Reader is empty or not populated and you are trying to read a value from within it.
 
Share this answer
 
As I think client.Reader.GetDateTime(1) is NULL. You should check the value before using it. If it is null then it'll show the Exception.
Try this:
C#
if(client.Reader != null){
    if(client.Reader.GetDateTime(1) != null)
    {
        if (Session["Nickname"] == null || DateTime.Now > client.Reader.GetDateTime(1))
        {
            Panel3.Visible = false;
        }
        else
        {
            Panel3.Visible = true;
            lblRegister.Visible = false;
        }
    }
}



--Amit
 
Share this answer
 
Oh man, I am reading out of Reader.Read() this is doing the error...

an auxiliary variable should fix it.

C#
Datetime date = new Datetime();
while (client.Reader.Read())
{
    lblTournamentName.Text = client.Reader.GetString(0);
    date = client.Reader.GetDateTime(1);
}
if (Session["Nickname"] == null || DateTime.Now > date)
{
    Panel3.Visible = false;
}
 
Share this answer
 
v3

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