PF2G 0 Junior Poster in Training

Hi, DaniWeb

I'm having problems with the insert:
(This is the login code, i can't open the register form)

Imports MySql.Data.MySqlClient

Public Class Login
    Dim server As String = "Server=localhost;Database=escola_musica;Uid=root;Pwd=;"
    'Dim server As String = "Server=localhost;Database=esola_musica;Uid=root;Pwd=;"
    Dim con As New MySqlConnection
    Dim da As New MySqlDataAdapter
    Dim sql As String
    Dim cmd As New MySqlCommand(sql, con)
    Dim dr As MySqlDataReader

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'connect DB
        con.ConnectionString = server

        Try
            If con.State = ConnectionState.Closed Then
                con.Open()
            Else
                con.Close()
                MsgBox("Ligação falhada!!")
            End If
        Catch erro As Exception
            MsgBox(erro.ToString)
        End Try

        'Log in
        sql = "SELECT * FROM admin WHERE username = '" & user_box.Text & "' AND pass = '" & pass_box.Text & "'"

        With cmd
            .CommandText = sql
            .CommandType = CommandType.Text
            .Connection = con

        End With
        dr = cmd.ExecuteReader

        If dr.Read Then
            If (dr("username") <> user_box.Text) Or (dr("pass") <> pass_box.Text) Then
                MsgBox("Utilizador ou pass invalida")
                Exit Sub
            Else
                MsgBox("correcto!")
                Principal.Show()
                con.Close()
                Me.Close()
                Exit Sub
            End If
        End If


        If Trim(user_box.Text) <> "" And Trim(pass_box.Text) <> "" Then
            Principal.Show()
            Me.Close()
        Else
            MsgBox("erro! -.-' ")
        End If
    End Sub

    Private Sub reg_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles reg_button.Click
        Admin_Reg.Show()
        Me.Close()
    End Sub
End Class

but before that it didn't add.
(Register Form)

Imports MySql.Data.MySqlClient
Public Class Admin_Reg
    Dim server As String = "Server=localhost;Database=escola_musica;Uid=root;Pwd=;"
    'Dim server As String = "Server=localhost;Database=esola_musica;Uid=root;Pwd=;"
    Dim con As New MySqlConnection
    Dim sql_sel As String = "SELECT (username, pass) FROM admin WHERE username = '" & user_box.Text & "' AND pass = '" & pass_box.Text & "'"
    Dim dr As MySqlDataReader
    Dim da As New MySqlDataAdapter
    Dim cmd As New MySqlCommand
    Dim sql_ins As String = "INSERT INTO admin (username, pass)VALUES ('" & user_box.Text & "','" & pass_box.Text & "')"

    Private Sub reg_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles reg_button.Click

        'Ligação à BD
        con.ConnectionString = server

        Try
            If con.State = ConnectionState.Closed Then
                con.Open()
            Else
                con.Close()
                MsgBox("Ligação falhada!!")
            End If
        Catch erro As Exception
            MsgBox(erro.ToString)
        End Try

        Dim username As Boolean = True
        con.Open()

        'Ver se username existe
        With cmd
            .CommandText = sql_sel
            .Connection = con
        End With
        da.SelectCommand = cmd
        dr = cmd.ExecuteReader

        While dr.Read()
            If dr.HasRows() = True Then
                MsgBox("Username Already in use!")
                username = False
                dr.Close()
            Else
                dr.Close()
                username = True
            End If
        End While
        dr.Close()

        'Registar admin
        If username = True Then
            cmd.CommandText = sql_ins
            cmd.Connection = con
            da.SelectCommand = cmd
            dr = cmd.ExecuteReader
            MsgBox("You've Registered!")
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Login.Show()
        Me.Close()
    End Sub
End Class

Can Someone help me, please!!

Thank You,
PF2g

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.