|
-
May 31st, 2007, 06:41 AM
#1
Thread Starter
Addicted Member
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).
-
May 31st, 2007, 06:52 AM
#2
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.
-
May 31st, 2007, 07:09 AM
#3
Thread Starter
Addicted Member
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.
-
May 31st, 2007, 07:30 AM
#4
Re: Execute Scalar Problem+Sourabh Das
It might have something to do with the combobox.Text. Try this instead:
2 Code:
Dim cmdvenstr As String = "Select vendor_code from tbl_vendor where vendor_name = @Vendor"
Dim cmdven As New SqlCommand(cmdvenstr, conn)
cmdven.Parameters.AddWithValue("@Vendor", Me.cmdvendor.selectedItem.Tostring.trim)
-
May 31st, 2007, 07:40 AM
#5
Thread Starter
Addicted Member
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
-
May 31st, 2007, 07:48 AM
#6
Thread Starter
Addicted Member
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..
-
May 31st, 2007, 07:48 AM
#7
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:
"Select vendor_code from tbl_vendor where vendor_name='" & cmbVendor.SelectedItem.ToString. & "'"
-
May 31st, 2007, 07:53 AM
#8
Thread Starter
Addicted Member
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...
-
May 31st, 2007, 07:54 AM
#9
Thread Starter
Addicted Member
Re: Execute Scalar Problem+Sourabh Das
it should have been
Code:
cmdven.ExecuteScalar()
-
May 31st, 2007, 07:59 AM
#10
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|