Visual Basic fetching data from mysql table

Total Post:67


 1710  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. 

  1. 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.

      Modified On Mar-30-2018 01:55:49 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.