user3528873
user3528873

Reputation: 23

specified cast is not available

can someone help me in this code please

is says specified cast is not available

MysqlConn = New SqlConnection
    MysqlConn.ConnectionString =
        "Data Source=SABAHALI-SHEIKH;Initial Catalog=md_1103763;Integrated Security=True"
    Dim READER As SqlDataReader

    Try
        MysqlConn.Open()
        Dim Query As String
        Query = "SELECT [firstname] FROM md_1103763.dbo.customer where firstname='" & ComboBox1.Text & "'"
        COMMAND = New SqlCommand(Query, MysqlConn)
        READER = COMMAND.ExecuteReader
        While READER.Read
            TextBox1.Text = READER.GetInt32("customerID")
            TextBox2.Text = READER.GetString("firstname")
            TextBox3.Text = READER.GetString("surname")
            TextBox4.Text = READER.GetString("contactnumber")
            TextBox5.Text = READER.GetString("emailaddress")

        End While

        MysqlConn.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MysqlConn.Dispose()


    End Try

Upvotes: 0

Views: 153

Answers (2)

Ahmed Galal
Ahmed Galal

Reputation: 714

you Must Select all columns in table

and

Try
    MysqlConn.Open()
    Dim Query As String = Nothing
    Query = "SELECT * FROM md_1103763.dbo.customer where firstname='" + ComboBox1.Text & "'"
    COMMAND() = New SqlCommand(Query, MysqlConn)
    READER = COMMAND().ExecuteReader
    While READER.Read
        TextBox1.Text = READER("customerID").ToString()
        TextBox2.Text = READER("firstname").ToString()
        TextBox3.Text = READER("surname").ToString()
        TextBox4.Text = READER("contactnumber").ToString()
        TextBox5.Text = READER("emailaddress").ToString()
    End While


    MysqlConn.Close()
Catch ex As Exception
    MessageBox.Show(ex.Message)
Finally


    MysqlConn.Dispose()
End Try

Upvotes: 0

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

MysqlConn = New SqlConnection
MysqlConn.ConnectionString =
    "Data Source=SABAHALI-SHEIKH;Initial Catalog=md_1103763;Integrated Security=True"
Dim READER As SqlDataReader

Try
    MysqlConn.Open()
    Dim Query As String
    Query = "SELECT [customerID], [firstname], [surname], [contactnumber], [emailaddress] FROM md_1103763.dbo.customer where firstname='" & ComboBox1.Text & "'"
    COMMAND = New SqlCommand(Query, MysqlConn)
    READER = COMMAND.ExecuteReader
    While READER.Read
        TextBox1.Text = READER.GetInt32(READER.GetOrdinal("customerID"))
        TextBox2.Text = READER.GetString(READER.GetOrdinal("firstname"))
        TextBox3.Text = READER.GetString(READER.GetOrdinal("surname"))
        TextBox4.Text = READER.GetString(READER.GetOrdinal("contactnumber"))
        TextBox5.Text = READER.GetString(READER.GetOrdinal("emailaddress"))

    End While

    MysqlConn.Close()

Catch ex As Exception
    MessageBox.Show(ex.Message)
Finally
    MysqlConn.Dispose()


End Try

Upvotes: 2

Related Questions