0

G_ID is an integer column in Groups table. I want the maximum value of it. When I trace below code I receive the error I've mentioned in code. Reader.HasRows equals true during debugging. So why it says "no data is present"

SqlConnection sqlc= new SqlConnection("data source=. ; database=LDatabase; integrated security=true");
SqlCommand cmd= new SqlCommand("select MAX(G_ID) as MAXID from Groups", sqlc);

sqlc.Open();
SqlDataReader Reader= cmd.ExecuteReader();
int MaxID = 0;
        if (Reader.HasRows)
        {
            MaxID = Convert.ToInt32(Reader["MAXID"].ToString());// Here I receive this error:  System.InvalidOperationException: Invalid attempt to read when no data is present.
            MaxID += 1;
        }
5
  • You need to call Reader.Read() before you can access the columns ... Commented Dec 5, 2013 at 21:23
  • 1
    Better use ExecuteScalar if you retrieving a single value Commented Dec 5, 2013 at 21:24
  • @marc_s I did it now but still the same error Commented Dec 5, 2013 at 21:26
  • @YuriyGalanter it's integer in the table Commented Dec 5, 2013 at 21:26
  • @Behnaz doesn't matter. any value can be retreived. Commented Dec 5, 2013 at 21:35

2 Answers 2

3

Before accessing a DataReader you need to call the method Read to position the reader on the first record

SqlConnection sqlc= new SqlConnection("data source=. ; database=LDatabase; integrated security=true");
SqlCommand cmd= new SqlCommand("select MAX(G_ID) as MAXID from Groups", sqlc);

sqlc.Open();
SqlDataReader Reader= cmd.ExecuteReader();
int MaxID = 0;
if (Reader.Read())
{
    MaxID = Convert.ToInt32(Reader["MAXID"].ToString());
    MaxID += 1;
}

By the way, your query returns just one row and one column from the DB so a better approach is to use the ExecuteScalar method

SqlCommand cmd= new SqlCommand("select MAX(G_ID) as MAXID from Groups", sqlc);
sqlc.Open();
object result = cmd.ExecuteScalar();
if (result != null)
{
    MaxID = Convert.ToInt32(result) + 1;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, I am not sure why I have started thinking in VB lately
When I execute above code(ExecuteScalar()) I receive this error: System.InvalidOperationException: ExecuteScalar: CommandText property has not been initialized
That's strange, the CommandText is the SELECT.... in the command constructor. Are you sure that you are executing the same SqlCommand shown above?
I also tried this: sqlc.Open(); Int32 result = (Int32)cmd.ExecuteScalar(); if (result != 0) { result += 1; } but still I get the same error.
If you try the DataReader version do you get something instead?
|
0

You are checking if the reader has rows but you are not reading them. Do this instead (note I'm also wrapping things to make sure they gets disposed properly):

SqlConnection sqlc= new SqlConnection("data source=. ; database=LDatabase; integrated security=true");
SqlCommand cmd = new SqlCommand("select MAX(G_ID) as MAXID from Groups", sqlc);

sqlc.Open();
try {
    using (SqlDataReader reader = cmd.ExecuteReader()) {
        int MaxID = 0;

        while (reader.Read()) {
            MaxID = Convert.ToInt32(Reader["MAXID"].ToString());
            MaxID += 1;
        }
    }
}
finally {
    sqlc.Close();
}

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.