1

I'm trying to fetch some data from table and get error:"Invalid object name 'h'".My code is

 Dim h As Decimal
'some code
 cmd2 = New SqlCommand("insert into h select KvotaX from dbo.Utakmice where ID=@s1 ", conn)


 cmd2.Parameters.AddWithValue("@s1", myReader.GetInt32(1))
'some code
 f = cmd2.ExecuteNonQuery()

I don't know what I'm doing wrong. KvotaX from dbo.Utakmice data type is real.

3
  • 1
    Does table h exists for sure? Commented Jun 10, 2013 at 13:56
  • That's a good sign. It means you successfully connected to the database and submitted your query, and were able to handle a response from the database server. A lot of things have already gone right. Now, do you have a table in your database named "h"? Commented Jun 10, 2013 at 13:57
  • Actualy h is decimal variable.I'm trying to fetch data into variable. Declaration is first line of code. Commented Jun 10, 2013 at 13:58

2 Answers 2

2

You try to set the value of h using a sql statement. This is not correct.

Dim h As Decimal
cmd2 = New SqlCommand("select KvotaX from dbo.Utakmice where ID=@s1 ", conn)
cmd2.Parameters.AddWithValue("@s1", myReader.GetInt32(1))
h = Convert.ToDecimal(cmd2.ExecuteScalar)

You select the record corresponding to the parameter passed in and then read the return value of the ExecuteScalar method. Note that this works only if the query returns one record and only one column. Better to be safe checking the return value before assigning to the decimal

Dim o = cmd2.ExecuteScalar
if o IsNot Nothing Then
    h = Convert.ToDecimal(o)
    .....
End If

However, your code hints to the fact that you are trying to get the value for the s1 parameters reading from an open MySqlDataReader. This could not be possible because, usually, the connection used by the datareader is busy till the datareader remains open.

Sign up to request clarification or add additional context in comments.

2 Comments

It works fine. I don't have problem with MySqlDataReader connection.Maybe it's because I have MultipleActiveResultSets=true; within connection string.
Good to know, I was just searching if that key was valid also for MySql.
0

I don't think you want the insert into h if you are just trying to fetch data.

Remove that phrase and see if you get what you are expecting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.