VISUAL BASIC FETCHING DATA FROM MYSQL TABLE

Alex Leblois

Total Post:67

Points:471
Posted by  Alex Leblois
VB.Net 
 1142  View(s)
Ratings:
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. 

  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: Visual Basic fetching data from mysql table

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!