10 Replies - 2345 Views - Last Post: 21 October 2014 - 07:29 AM Rate Topic: -----

#1 Gregorys05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-October 14

No data present error when there is data

Posted 20 October 2014 - 01:12 AM

Hi all,

I am having an issue with the below error saying that there is no data present for the second read item (bold and underlined).
But I can see in the data that both fields have data in them. any ideas why the approver2 variable is not being populated?

Thanks in advance.

Error
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll

Additional information: Invalid attempt to read when no data is present.




C#
            string approver = null;
            string approver2 = null;


            con.Open();

                string Approve = "Select Approver"+
                                 ",Approver2"+
                                 " From dbo.Tbl_Overview"+
                                 " Where RequestID = '" + STAR.Globals.Detail + STAR.Globals.DetailNum + "'";

                     using (SqlCommand command = new SqlCommand(Approve, con))
                     {
                         SqlDataReader reader = command.ExecuteReader();
                         if (reader.HasRows)
                         { 
                             while (reader.Read())
                                 approver = reader["Approver"] as string;
                                 [u][b]approver2 = reader["Approver2"] as string;[/b][/u]
                            
                         }
                     }

            con.Close();




SQL
Select Approver,Approver2 From dbo.Tbl_Overview Where RequestID = 'CR4'



Results
Approver	Approver2
User.1	        User2



Is This A Good Question/Topic? 0
  • +

Replies To: No data present error when there is data

#2 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6403
  • View blog
  • Posts: 25,878
  • Joined: 12-December 12

Re: No data present error when there is data

Posted 20 October 2014 - 02:30 AM

You need brackets following the while, otherwise only the first following statement is executed:
             while (reader.Read()) {
                 approver = reader["Approver"] as string;
                 approver2 = reader["Approver2"] as string;
            }

It is preferable that you always include brackets to avoid such concerns.

This post has been edited by andrewsw: 20 October 2014 - 09:19 AM

Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: No data present error when there is data

Posted 20 October 2014 - 06:41 AM

Braces, not bracket.

{} - Curly braces
[] - Square brackets
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6403
  • View blog
  • Posts: 25,878
  • Joined: 12-December 12

Re: No data present error when there is data

Posted 20 October 2014 - 09:14 AM

{ } curly brackets or braces
[ ] square brackets
( ) round brackets or parens/parentheses

Everything is a bracket.

This post has been edited by andrewsw: 20 October 2014 - 09:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6403
  • View blog
  • Posts: 25,878
  • Joined: 12-December 12

Re: No data present error when there is data

Posted 20 October 2014 - 09:21 AM

Quote

Brackets are tall punctuation marks used in matched pairs within text, to set apart or interject other text. Used unqualified, brackets refer to different types of brackets in different parts of the world and in different contexts.


http://en.wikipedia....ntheses_.28_.29
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: No data present error when there is data

Posted 20 October 2014 - 09:23 AM

It's somewhat dependent on where you're from. Regardless, the language around these particular punctuation characters is unclear; it's best for all involved to just specify the character when you name it. Your example made it clear which marks you meant.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: No data present error when there is data

Posted 20 October 2014 - 09:46 AM

Yeah, technically, in general language terms. But then again technically "gay" means 'happy'. But that's not the real-world use of the term today.
Technically you can say 'angle brackets' to mean < and >... but who really does that in the world?
You *can* say 'bracket' to refer to any of these: [ { ( < - Because bracket it the generic term encompassing all paired deliminators of this type.
But as we all know just because you can do a thing doesn't mean you should do a thing, especially when it comes to programing.

This is a hot button topic with many language purests and you can see it here with specific respect to programming:
http://english.stack...quiggly-bracket

Looking over several debates on this on lots of sites I choose to stick firmly to my use above, as do many other developers. Also when you look at most textbooks you see this same use, mostly because that is what is defined by the Chicago Manual of Style, used by many if not most publishers.

So I think this is one of those things where you can be technically right yet be real-world wrong with one's peers.

Attached Image

This post has been edited by tlhIn`toq: 20 October 2014 - 09:46 AM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6403
  • View blog
  • Posts: 25,878
  • Joined: 12-December 12

Re: No data present error when there is data

Posted 20 October 2014 - 10:01 AM

Quote

Technically you can say 'angle brackets' to mean < and >... but who really does that in the world?

I do, frequently (angled brackets), and it is common usage in the UK.

This post has been edited by andrewsw: 20 October 2014 - 10:01 AM

Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: No data present error when there is data

Posted 20 October 2014 - 10:12 AM

Well... We all do certain things. I violate naming conventions in code so as to make my life easier with global find/replace.

I think as long as someone is specific they can communicate.
If you say "curly bracket" people will hear - {
If you say "square bracket" people will hear - [
You can even say "round bracket" and some people will hear - (

But if you just say "bracket" without a shape specification, I hear - [
... just say "brace" and I hear - {


If you really want to frak up the average Joe on the street then say "square parenthesis" to mean [ - It's linguistically legal but will screw people up.
Or say "left Guillemet" and see what they think you're talking about.

So, to me, I will always mean and expect and argue for this use:

( - Parenthesis
[ - Bracket
{ - Brace
< - Less than
 - Left chevron

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5921
  • View blog
  • Posts: 20,243
  • Joined: 05-May 12

Re: No data present error when there is data

Posted 21 October 2014 - 05:36 AM

And to further derail this thread, there are folks who think that "/" are backslashes... :)
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: No data present error when there is data

Posted 21 October 2014 - 07:29 AM

Those people are just mistaken :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1