Results 1 to 10 of 10

Thread: Execute Scalar Problem+Sourabh Das

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    220

    Execute Scalar Problem+Sourabh Das

    Hi All,
    Please see the code below:

    Code:
                        Dim vencode As String
                        conn.Open()
                        Dim cmdvenstr As String = "Select vendor_code from tbl_vendor where vendor_name='" & cmbVendor.Text.Trim() & "'"
                        Dim cmdven As New SqlCommand(cmdvenstr, conn)
                        vencode = cmd.ExecuteScalar()
                        MessageBox.Show(vencode, "IG", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
    on the messagebox everything is coming other than the value of vencode. where am I going wrong. In the database vendor_code datatype is nvarchar(50).

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Execute Scalar Problem+Sourabh Das

    Have you tried filling a datatable to see if there's definitely data being returned? The code looks fine to me. And I take it there are no errors, it's just showing a blank messagebox where the text should be. You also need to remember to close the connection before displaying the messagebox, although I don't know if that could cause an issue.


    Just as an additional comment (Although this wont be the reason for the problem ) ExecuteScalar returns an object so you might be better declaring vencode as an Object and then using the .ToString when displaying the result in a messagebox.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    220

    Re: Execute Scalar Problem+Sourabh Das

    I did try by converting the vencode as object but still not working, neither its returning any values, but the same query is working fine in the sql server.

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Execute Scalar Problem+Sourabh Das

    It might have something to do with the combobox.Text. Try this instead:

    2 Code:
    1. Dim cmdvenstr As String = "Select vendor_code from tbl_vendor where vendor_name = @Vendor"
    2. Dim cmdven As New SqlCommand(cmdvenstr, conn)
    3. cmdven.Parameters.AddWithValue("@Vendor", Me.cmdvendor.selectedItem.Tostring.trim)
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    220

    Re: Execute Scalar Problem+Sourabh Das

    Hi this code is giving an blue under line in the thrid line as "AddWithValue" is not a member SqlparameterCollection class

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    220

    Re: Execute Scalar Problem+Sourabh Das

    Hey Stimbo..,
    I was a stupid..
    read my first thread carefully, you will understand what is the mistake..
    even you didnt notice, if you are unable to figure out let me know.. its a very very very silly mistake..

  7. #7
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Execute Scalar Problem+Sourabh Das

    I don't why it's doing that, I've used the code loads of times. It should work. When I decalre a new SQL command, the parameters and add with value both appear when typing it.

    Well, forget about the parameters for the time being, what happens if you replace the combobox.Text in your sql string with the .selecteditem text instead? Something like this, or whatever the correct format is.

    vb Code:
    1. "Select vendor_code from tbl_vendor where vendor_name='" & cmbVendor.SelectedItem.ToString. & "'"
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    220

    Re: Execute Scalar Problem+Sourabh Das

    My dear sweet friend,
    Code:
    Dim vencode As String
                        conn.Open()
                        Dim cmdvenstr As String = "Select vendor_code from tbl_vendor where vendor_name='" & cmbVendor.Text.Trim() & "'"
                        Dim cmdven As New SqlCommand(cmdvenstr, conn)
                        vencode = cmd.ExecuteScalar()
                        MessageBox.Show(vencode, "IG", MessageBoxButtons.OK, MessageBoxIcon.Information
    just see I have marked the difference in bold. we both are good programmers.. no great programmers...

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    220

    Re: Execute Scalar Problem+Sourabh Das

    it should have been
    Code:
    cmdven.ExecuteScalar()

  10. #10
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Execute Scalar Problem+Sourabh Das

    Doh! Posted the last one before seeing your follow up.

    Well, my code should work fine now - that's what's important!
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

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