Question Add new column to mysql table won't work

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
I use vb 2010 and try to make an app to manage a mysql database.
With it I want to add/remove/edit columns in a specific table.

I use a textbox to name a new column.

But it doesn't work.

This is the code I already have (error codes are in dutch):

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.TextLength < 3 Then
            MsgBox("Ingevoerd evenement is te kort.", MsgBoxStyle.Exclamation, "Foutmelding")
        Else
            Try
                conn.ConnectionString = myConnString
                Try
                    conn.Open()
                    Try
                        Dim myquery As String = "ALTER TABLE nbs_events " & _
                                                "ADD TextBox1.Text VARCHAR(100)"
                        ExecuteSQLStmt(myquery)
                    Catch ex As Exception
                        MsgBox("Toevoegen van evenement is niet gelukt, dit is de foutmelding: " & ex.Message, MsgBoxStyle.Critical, "Foutmelding")
                    End Try
                Catch meserror As Exception
                    MsgBox("De Verbinding is niet gelukt dit is de foutmelding: " & meserror.Message, MsgBoxStyle.Critical, "Foutmelding")
                Finally
                    If conn.State <> ConnectionState.Closed Then conn.Close()
                End Try
            Catch ex As Exception
                MsgBox("Er is een fout ontstaan, dit is de foutmelding: " & ex.Message, MsgBoxStyle.Critical, "Foutmelding")
            End Try
        End If
    End Sub


Private Sub ExecuteSQLStmt(ByVal sql As String)
        ' Open the connection
        If conn.State = ConnectionState.Open Then
            conn.Close()
        End If
        myConnString = "Database=contact_database; Data Source=localhost; User Id='root'; Password=''"
        conn.ConnectionString = myConnString
        conn.Open()
        cmd = New MySqlCommand(sql, conn)
        Try
            cmd.ExecuteNonQuery()
        Catch ae As MySqlException
            MessageBox.Show(ae.Message.ToString())
        End Try
    End Sub 'ExecuteSQLStmt

Anyone an idea?

Thanks
 
Back
Top