Visual basic

Hi guys,

I'm doing some query to get a parameter from SQL.

 Using cmd As New SqlCommand("select * from" + " " + tablen + " " + "where id=@rowid", Basewebpage.Passport.Connection)
            cmd.Parameters.AddWithValue("@rowid", rowid)
            Dim getRow = Convert.ToInt32(cmd.Parameters(0).Value)

        End Using

Open in new window


I have one issue here:
I can't get the value- Dim getRow = Convert.ToInt32(cmd.Parameters(0).Value)
LVL 1
Moti Mashiah.NET DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
You aren't using a stored procedure, so there isn't a "return value".  There is a result of your query, and so to get that, you need to do:

Dim getRow = Convert.ToInt32(cmd.ExecuteScalar());

Open in new window


https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.110).aspx
Moti Mashiah.NET DeveloperAuthor Commented:
Thank you for your answer.
can you just tell me how to pass the let's say column 0 only?

can I do
Dim getRow = Convert.ToInt32(cmd.ExecuteScalar(0))
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
as the documentation provided by Megan already mentioned:

The first column of the first row in the result set, or a null reference (Nothing in Visual Basic) if the result set is empty. Returns a maximum of 2033 characters.

referring to cmd.ExecuteScalar() should be good enough to return the first column's value of first row for your result set.

if you want to read other columns, you probably need to use a DataReader instead.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Moti Mashiah.NET DeveloperAuthor Commented:
Basically U gave me a good solution but still it doesn't return a value just a boolean it there is data or not and this is good enough foe me.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.