user225269
user225269

Reputation: 10913

problem connecting with odbc mysql in vb.net

I'm trying another method to connect mysql and vb.net. I didn't have any difficulties connecting mysql and vb.net when using mysql net connector. And I also used the same codes. I just replaced the ones that needed to be replaced with odbc.

 Imports System.Data.Odbc



Public Class globalclass
    Private cn As New OdbcConnection("DSN=korosu")

    Dim cmd As Odbc.OdbcCommand

    Public name As String
    Public age As String


    Public Sub New()
        cn.Open()
        cmd = New Odbc.OdbcCommand("SELECT * FROM test")

    End Sub

    Public Sub adds()

        cmd.CommandText = "INSERT INTO test(name, age) VALUES('" + name + "','" + age + "')"
        cmd.ExecuteNonQuery()



    End Sub

What do I need to do to fix this? I always get the runtime error and its highlighting the cmd.ExecuteNonQuery. And says that the connection has not been properly initialized.Please help

Upvotes: 1

Views: 1609

Answers (2)

Koen Rijpstra
Koen Rijpstra

Reputation: 354

you forgot to set the connection for the OdbcCommand:

cn.Open()
cmd.Connection = cn
cmd.CommandText = "INSERT INTO test(name, age) VALUES('" + name + "','" + age + "')"
cmd.ExecuteNonQuery()
cn.Close()

Upvotes: 1

SteveCav
SteveCav

Reputation: 6719

You haven't specified that cmd uses cn.

Upvotes: 1

Related Questions