Cannot insert values into database

114 views Asked by At

I am having problems getting my VB.net application to insert values into my MySQL. I can connect to the db, but cannot insert values. I have downloaded the DLL and have attached it as a reference. I have also taken the query written and plugged it directly into the db and it works. I have tried multiple ways of coding it from the examples I have found on this website, but I still cannot get it to work. This is the code I am trying to get to work:

Try

MysqlConn = New MySqlConnection("server=localhost;userid=User;password=******;database=Targets")

MysqlConn.Open()

            Dim sqlCommand As New MySqlCommand

            sqlCommand.CommandType = CommandType.Text

            sqlCommand.CommandText = "insert into Victims(UserName, Machine, IP) values('" + strUser + "','" + strMachine + "','" + strIPAddress + "')"

            Dim strRD As MySqlDataReader

            strRD = sqlCommand.ExecuteReader()

            MysqlConn.Close()

            MessageBox.Show("connected")

  Catch ex As MySqlException
            MessageBox.Show("error")

I have declared the variables used in the query statement earlier in the code. Thanks for any help you could offer.

2

There are 2 answers

1
rusty On BEST ANSWER

Public Function InsertVal() As Boolean

    Dim iReturn As Boolean
    Using MysqlConn As New MySqlConnection(connectionString)
        Using sqlCommand As New MySqlCommand()
            With sqlCommand
                .CommandText = "INSERT INTO Victims (UserName, Machine, IP) VALUES(@User,@Machine,@IP)"
                .Connection = MysqlConn
                .CommandType = CommandType.Text
                .Parameters.AddWithValue("@User", lblDisplayUser.Text)
                .Parameters.AddWithValue("@Machine", lblDisplayMachine.Text)
                .Parameters.AddWithValue("@IP", lblDisplayIP.Text)

            End With
            Try
                MysqlConn.Open()
                sqlCommand.ExecuteNonQuery()
                iReturn = True
            Catch ex As MySqlException
                MsgBox(ex.Message.ToString)
                iReturn = False
            Finally
                MysqlConn.Close()
            End Try
        End Using
    End Using

    Return InsertVal

This is the solution that worked for me. I had to change it to a function and work off the detailed error messages. Thanks for everybody's help.

2
Dovah On

You can try this instead..

Try

        MysqlConn = New MySqlConnection("server=localhost;userid=User;password=******;database=Targets")

        MysqlConn.Open()

        Dim query as String

        query = "INSERT INTO Victims(UserName, Machine, IP) VALUES('" + strUser + "','" + strMachine + "','" + strIPAddress + "')"

        Dim cmd As MySqlCommand
        cmd = New MySqlCommand(query,MysqlConn)
        cmd.ExecuteNonQuery()
        MysqlConn.Close()
        MessageBox.Show("connected")

Catch ex As MySqlException MessageBox.Show("error") End Try