Home > DeveloperSection > Forums > Visual Basic fetching data from mysql table
Alex Leblois
Alex Leblois

Total Post:67

Points:471
Posted on    June-11-2013 8:29 AM

 VB.Net VB.Net 
Ratings:


 1 Reply(s)
 916  View(s)
Rate this:
Hi Developers, 

During Fetching data from the table i get following line of error

"Invalid object name 'h'"
 
My line of code as following

 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.

Thanks in advance. 



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    June-12-2013 7:32 AM

Hi Alex,

You can not set the value of h using a SQL statement, hence you can try as following 

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.


Don't want to miss updates? Please click the below button!

Follow MindStick