Results 1 to 12 of 12

Thread: [RESOLVED] [2005] Noob looking for a better way.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Resolved [RESOLVED] [2005] Noob looking for a better way.

    Hey all,
    I am just recreating a vb6 function that I used to call in another app. I am very new to the whole .NET framework, but I do think there has to be a better way than what I came up with. This function queries a table for an invoice number, returns that invoice number and then increments the table.

    This is the code that I got working there's gotta be a better way:

    vbcode Code:
    1. Private Function GetInvoiceNo() As Long
    2.  
    3.         sSQL = "SELECT Order_Ctrl_Online from Order_ctrl"
    4.         da = New SqlClient.SqlDataAdapter(sSQL, conn)
    5.         da.Fill(ds, "InvoiceNo")
    6.         GetInvoiceNo = ds.Tables("InvoiceNo").Rows(0).Item(0)
    7.         ds.Tables("InvoiceNo").Rows(0).Item(0) = ds.Tables("InvoiceNo").Rows(0).Item(0) + 1
    8.         sSQL = "Update Order_Ctrl SET Order_Ctrl_Online = Order_Ctrl_Online + 1"
    9.         da = New SqlClient.SqlDataAdapter(sSQL, conn)
    10.         da.UpdateCommand = New SqlCommand(sSQL, conn)
    11.         conn.Open()
    12.         da.UpdateCommand.ExecuteNonQuery()
    13.         conn.Close()
    14.     End Function

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2005] Noob looking for a better way.

    vb Code:
    1. Dim returnValue As Long
    2.  
    3.         conn.Open()
    4.         'get old value
    5.         Dim cmd As New SqlCommand("SELECT Order_Ctrl_Online from Order_ctrl", conn)
    6.         returnValue = cmd.ExecuteScalar
    7.         'increment old value
    8.         cmd.CommandText = "Update Order_Ctrl SET Order_Ctrl_Online = Order_Ctrl_Online + 1"
    9.         cmd.ExecuteNonQuery()
    10.         conn.Close()
    11.  
    12.         Return returnValue

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [2005] Noob looking for a better way.

    That works good in this situation, but what if we complicated it and pulled a record of like 20+ fields and needed to change values in all of them?

    Thanks for your reply!

  4. #4
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Noob looking for a better way.

    then you would use a loop
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [2005] Noob looking for a better way.

    Quote Originally Posted by wild_bill
    vb Code:
    1. Dim returnValue As Long
    2.  
    3.         conn.Open()
    4.         'get old value
    5.         Dim cmd As New SqlCommand("SELECT Order_Ctrl_Online from Order_ctrl", conn)
    6.         returnValue = cmd.ExecuteScalar
    7.         'increment old value
    8.         cmd.CommandText = "Update Order_Ctrl SET Order_Ctrl_Online = Order_Ctrl_Online + 1"
    9.         cmd.ExecuteNonQuery()
    10.         conn.Close()
    11.  
    12.         Return returnValue

    Just tried this and it says errors:
    ExecuteScalar: Connection property has not been initialized.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [2005] Noob looking for a better way.

    This is the exact code I am getting the error with:

    vb Code:
    1. Private Function GetInvoiceNo() As Long
    2.         conn.Open()
    3.         Dim cmd As New SqlCommand("SELECT Order_Ctrl_Online from Order_ctrl")
    4.         GetInvoiceNo = cmd.ExecuteScalar
    5.         cmd.CommandText = "Update Order_Ctrl SET Order_Ctrl_Online = Order_Ctrl_Online + 1"
    6.         cmd.ExecuteNonQuery()
    7.         conn.Close()
    8.     End Function

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

    Re: [2005] Noob looking for a better way.

    So initialise the Connection property. You can't access a database using a Command if you don't have a Connection to connect to the database over. You can't talk to someone over the telephone if you don't have a telephone line.
    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

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [2005] Noob looking for a better way.

    I initialized it publicly at the top of the class..

  9. #9
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Noob looking for a better way.

    You need to set the command objects connection property to that connection.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

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

    Re: [2005] Noob looking for a better way.

    As Gary says. You may have created a Connection object but you have not set the Connection property of your Command. When you execute the Command, how is it to know what Connection it's supposed to use?
    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

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [2005] Noob looking for a better way.

    ahh gotcha... thanks guys!

  12. #12
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Re: [RESOLVED] [2005] Noob looking for a better way.

    Don't forget the Try Catch and make sure the connection gets closed if an error is thrown. You can also use the "Using" block in VB 2005 for the connection object so you can be sure that it releases it resources. Just thought I would mention this :-)

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