Results 1 to 16 of 16

Thread: Adodb to ado.net

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Question Adodb to ado.net

    Hello,

    I went through many ado.net documentation but this doesn't seem to get into my matter somehow. It'll be great if anyone can show me how to execute the following function using ado.net coding:

    Code:
    Public prs As New ADODB.Recordset
    Public crs As New ADODB.Recordset
    
    
    prs.Open("select cuid from customer order by cuid", Module1.db, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
            If prs.RecordCount > 0 Then
                prs.MoveLast()
                cuselect = prs.Fields!cuid.Value
    prs.close
    End If
    
    crs.Open("select * from customer where cuid ='" & cuselect & "'", Module1.db, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    
    Text1.Text = crs.Fields!custid.Value.ToString
    Combo1.Text = crs.Fields!Name.Value.ToString
    Text2.Text=CDate(crs.Fields!createdon.Value).ToString("dd/MMMyy,HH:mm")
    
    crs.close

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Adodb to ado.net

    Is that code just to pull the most recent record from the customers table?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: Adodb to ado.net

    Yes sir.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: Adodb to ado.net

    Also, i am using SQL server express edition.

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Adodb to ado.net

    Try this. I don't have your data store to test it against, but I think the syntax is all correct.

    Code:
            Dim myDBConnection As SqlClient.SqlConnection = Nothing
            Dim myDBCommand As SqlClient.SqlCommand = Nothing
            Dim myDBReader As SqlClient.SqlDataReader = Nothing
            Try
                myDBConnection = New SqlClient.SqlConnection("CONNECTION STRING GOES HERE")
                myDBCommand = New SqlClient.SqlCommand("SELECT * FROM customer WHERE cuid = (Select MAX(cuid) from customer)", myDBConnection)
                myDBReader = myDBCommand.ExecuteReader(CommandBehavior.CloseConnection)
    
                If myDBReader.HasRows Then
                    text1.Text = myDBReader.Item("custid").ToString
                    Combo1.Text = myDBReader.Item("Name").ToString
                    text2.text = CDate(myDBReader.Item("createdon")).ToString("dd/MMMyy,HH:mm")
                Else
                    'HANDLE CASE FOR NO RECORDS EXIST IN DB
                End If
            Catch ex As Exception
                'HANDLE EXCEPTION HERE
            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.Dispose()
            End Try

  6. #6
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Adodb to ado.net

    Kleinma beat me to it, and was much more thorough.

    Drat the lack of a VS machine right now.
    Last edited by Campion; Oct 23rd, 2009 at 10:00 AM. Reason: Kleinma beat me
    From my burrow, 2 feet under.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: Adodb to ado.net

    On catching exception it shows this error:

    Object reference not set to an instance of an object.

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Adodb to ado.net

    Well if you copy/pasted my code then make sure that

    1) custid, Name, and createdon are actually your column names, and verify casing. I don't think they are case sensitive in this scenario, but off the top of my head I can't remember

    2) make sure you replaced the CONNECTION STRING GOES HERE text with a valid connection string to your SQL database.

    3) I think I forgot to add a call to myDBConnection.Open() to open the DB connection. Place that just before the line to execute the reader.

    4) if all the above don't fix it, set a break point on the Try block, and step through to see exactly which line makes the exception occur.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: Adodb to ado.net

    I am not getting the value of ClientId in the MsgBox.

    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 * FROM ClientProfile", myDBConnection)
                ' WHERE ClientID)= (Select MAX(ClientID) from ClientProfile)", 
                myDBReader = myDBCommand.ExecuteReader(CommandBehavior.CloseConnection)
                If myDBReader.HasRows Then
    
                    MsgBox(myDBReader.Item("ClientID"), MsgBoxStyle.OkOnly)
    
                    Me.TextBox1.Text = myDBReader.Item("ClientID").ToString
                    Me.ComboBox2.Text = myDBReader.Item("machineid").ToString
                    'TextBox2.Text = CDate(myDBReader.Item("firstname")).ToString("dd/MMMyy,HH:mm")
                Else
                    'HANDLE CASE FOR NO RECORDS EXIST IN DB
                End If
            Catch ex As Exception
                'HANDLE EXCEPTION HERE
            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

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Adodb to ado.net

    Sorry, it was the fact that I didn't test the code of course

    You need a call to .read() of the datareader so it will advance to the first record (even if there is only 1 record returned by your query) so that it can read data from the record.

    Try this. Note the call to .read() before trying to get the data. This also sort of assumes you will only get 1 record back when you run your query, considering your goal is to get the most recent record.

    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")
                myDBCommand = New SqlClient.SqlCommand("SELECT * FROM ClientProfile WHERE ClientID = (Select MAX(ClientID) from ClientProfile)", myDBConnection)
    
                myDBConnection.Open()
                myDBReader = myDBCommand.ExecuteReader(CommandBehavior.CloseConnection)
    
                If myDBReader.HasRows Then
    
                    myDBReader.Read()
    
                    MsgBox(myDBReader.Item("ClientID"), MsgBoxStyle.OkOnly)
    
                    Me.TextBox1.Text = myDBReader.Item("ClientID").ToString
                    Me.ComboBox2.Text = myDBReader.Item("machineid").ToString
                    'TextBox2.Text = CDate(myDBReader.Item("firstname")).ToString("dd/MMMyy,HH:mm")
                Else
                    'HANDLE CASE FOR NO RECORDS EXIST IN DB
                End If
            Catch ex As Exception
                'HANDLE EXCEPTION HERE
            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

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: Adodb to ado.net

    wow, that worked like a pie.... thank you so much.
    just one more thing sir.
    can u also show me how to delete and update it??

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: Adodb to ado.net

    Anyone please??? i am waiting!

  13. #13
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Adodb to ado.net

    Just use UPDATE and DELETE SQL syntax, and apply the string by using Command.ExecuteNonQuery. If you know SQL, then writing the SQL string should be easy.
    From my burrow, 2 feet under.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: Adodb to ado.net

    Can you give me an example?? please!

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: Adodb to ado.net

    I tried using this but its not saving the text in textbox5

    vb Code:
    1. Using connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\ProjectMW\MachineWale\MachineWale\DatabaseMW.mdf;Integrated Security=True;User Instance=True")
    2.             Using command As New SqlCommand("INSERT INTO ClientProfile (FirstName) VALUES'" & TextBox5.Text & "'", connection)
    3.                                connection.Open()
    4.                 command.ExecuteNonQuery()
    5.             End Using
    6.         End Using

  16. #16
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Adodb to ado.net

    Well just by looking at the code, you don't have a space between VALUES and the '

    Are you getting any sort of exception thrown when you run the code?

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