Results 1 to 7 of 7

Thread: [RESOLVED] Datareader doubt

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Resolved [RESOLVED] Datareader doubt

    This code is not giving the msgbox value! Is the GetValue method incorrect?


    Code:
     Dim myDBConnection As SqlClient.SqlConnection = Nothing
            Dim myDBCommand As SqlClient.SqlCommand = Nothing
            Dim myDBReader As SqlClient.SqlDataReader = Nothing
            Try
                myDBConnection = New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\ProjectMW\MachineWale\MachineWale\DatabaseMW.mdf;Integrated Security=True;User Instance=True")
                myDBConnection.Open()
                myDBCommand = New SqlClient.SqlCommand("SELECT MAX(ClientID) FROM ClientProfile", myDBConnection)
                myDBReader = myDBCommand.ExecuteReader(CommandBehavior.CloseConnection)
                If myDBReader.HasRows Then
                    myDBReader.Read()
                    MsgBox(myDBReader.GetValue("ClientID") + 1)
                    'Me.TextBox2.Text = myDBReader.Item("ClientID".ToUpper) + 1
                Else
                    Me.TextBox2.Text = "1000"
                End If
            Catch ex As Exception
                MsgBox(" Error in Connection! ")
                Exit Try
                Exit Sub
            Finally
                'CLEAN UP DB OBJECTS
                If myDBReader IsNot Nothing Then myDBReader.Close()
                If myDBCommand IsNot Nothing Then myDBCommand.Dispose()
                If myDBConnection IsNot Nothing Then myDBConnection.Close()
            End Try
    Last edited by LuxCoder; Oct 30th, 2009 at 05:04 AM. Reason: Coding

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Datareader doubt

    Have you read the documentation for the GetValue method? Obviously not, because the documentation would have told you that the parameter is type Integer, not String. If you want to get the value by column name then you need to use the Item property. Also, if the ClientID column contains Integers then, if you do get the value by index rather than name, you should call GetInt32.

    Off topic a bit, let's look at how you can improve the way you construct your code. Mainly, you should be employing Using blocks to create and destroy your disposable objects. That way you could do away with that Finally block:
    vb.net Code:
    1. Try
    2.     Using connection As New SqlConnection("...")
    3.         connection.Open()
    4.  
    5.         Using command As New SqlCommand("...", connection)
    6.             Using reader As SqlDataReader = command.ExecuteReader()
    7.                 '...
    8.             End Using
    9.         End Using
    10.     End Using
    11. Catch ex As Exception
    12.     '...
    13. End Try
    In the Catch block you really MUST actually look at the exception to see what went wrong. How many different things could go wrong in that code and you are just saying "Error in Connection!" for all of them. In many cases the connection could be fine.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: Datareader doubt

    I tried using item as ClientID is stored as integer in the DB. But still i am not getting it's value.

    vb Code:
    1. Try
    2.             Using connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\ProjectMW\MachineWale\MachineWale\DatabaseMW.mdf;Integrated Security=True;User Instance=True")
    3.                 connection.Open()
    4.                 Using command As New SqlCommand("SELECT MAX(ClientID) FROM ClientProfile", connection)
    5.                     Using reader As SqlDataReader = command.ExecuteReader()
    6.                         If reader.HasRows Then
    7.                             reader.Read()
    8.                             MsgBox((reader.Item("ClientID")) + 1)
    9.                         Else
    10.                             Me.TextBox2.Text = "1000"
    11.                         End If
    12.                     End Using
    13.                 End Using
    14.             End Using
    15.         Catch ex As Exception
    16.             MsgBox(ex.ToString, MsgBoxStyle.Exclamation)
    17.         End Try

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Datareader doubt

    I didn't actually look at your SQL code before. Your result set has no ClientID column so you can't get a value from it. You are getting the ClientID column; you're getting the MAX of the ClientID column. The results of expressions don't inherently use the names of the columns they contain because they could contain any number of columns. If you want the value to be named ClientID then you have to specify that with an alias:
    sql Code:
    1. SELECT MAX(ClientID) AS ClientID FROM ClientProfile
    I would suggest "MaxClientID" though, to be more accurate.

    That said, if you're only retrieving a single value then you shouldn't be using a DataReader at all. Check out my thread of ADO.NET code examples, which you can get to from the Database FAQ link or Codebank link in my signature, and look for the ExecuteScalar example.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: Datareader doubt

    I used msgbox(reader(1) + 1) and it worked. I still want to have a look at ExecuteScalar. Tried to find but could not. Can u gimme it's link?

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Datareader doubt

    ExecuteScalar is similar to ExecuteReader, except that it just returns a single value. In your case, I think the value is an integer (though it could be a double, I suppose), so you would do something like this (using the SQL statement and command object that you have already created in your current code):

    msgbox(command.ExecuteScalar.ToString)

    Actually, I'm a little unclear on that. Normally, I'd assign the return value to a variable like this:

    myInt = CInt(command.ExecuteScalar)

    whether or not you can use .ToString as I show, or need to use CStr(command.ExecuteScalar), is something you will find out really fast.

    ExecuteScalar returns the value in the first column of the first row of the set of rows returned by the SQL SELECT statement.
    My usual boring signature: Nothing

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Datareader doubt

    Quote Originally Posted by LuxCoder View Post
    I used msgbox(reader(1) + 1) and it worked. I still want to have a look at ExecuteScalar. Tried to find but could not. Can u gimme it's link?
    The thread is called "Retrieving and Saving Data in Databases".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width