Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm doing this exercise on VB2010, it's a log-in and the other is an add/edit/delete project.
I've coded the connection and been successful at it somehow, now, the thing is I can't update the database even if I get the confirmation that the record has been added.

I have no idea where the error is.
All I have for now is this block of code for the registration (add/edit/delete) so far:


Imports MySql.Data.MySqlClient
Public Class register
    Private Sub register_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        firstnametb.Focus()
        Try
            If conn.State = ConnectionState.Closed Then
                Dim connection As New MySqlConnection("Server = localhost; User Id = root; Password = root; Database = add_edit_delete")
                connection.Open()
            End If
        Catch ex As Exception
            MsgBox("Database Error!", MsgBoxStyle.Information, "System Message")
            Application.Exit()
        End Try
    End Sub
 
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clrbtn.Click
        lastnametb.Text = ""
        firstnametb.Text = ""
        middletb.Text = ""
        agetb.Text = ""
        loctb.Text = ""
        unametb.Text = ""
        pwordtb.Text = ""
        cpwordtb.Text = ""
    End Sub
    Private Sub addbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addbtn.Click
        If lastnametb.Text = "" Or firstnametb.Text = "" Or agetb.Text = "" Or loctb.Text = "" Or unametb.Text = "" Or pwordtb.Text = "" Or cpwordtb.Text = "" Then
            MsgBox("Please complete the fields!", MsgBoxStyle.Exclamation, "System Message")
        Else
            If conn.State = ConnectionState.Closed Then
                Dim connection As New MySqlConnection("Server = localhost; User Id = root; Password = root; Database = add_edit_delete")
                connection.Open()
            End If
            Try
                strSql = "INSERT INTO register(LastName, FirstName, MiddleInitial, Age, Location, Uname, Pword) VALUES('" & lastnametb.Text & "','" & firstnametb.Text & "','" & middletb.Text & "','" & agetb.Text & "','" & loctb.Text & "','" & unametb.Text & "','" & pwordtb.Text & "')"
                cmd.CommandText = strSql
                cmd.Connection = conn
                cmd.ExecuteNonQuery()
                MsgBox("Record has been saved!", MsgBoxStyle.Information, "System Message")
                conn.Close()
                Me.Dispose()
            Catch ex As Exception
                MsgBox("Unable to save record!", MsgBoxStyle.Critical, "System Message")
                conn.Close()
            End Try
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Dispose()
    End Sub
End Class


Hope someone could help me. :) THANKS! God bless!
Posted
Updated 4-Jul-11 20:47pm
v2
Comments
[no name] 5-Jul-11 2:15am    
debug the code once. and why you are defining sql connection everytime.
Dalek Dave 5-Jul-11 2:48am    
Edited for Grammar and Readability.

1 solution

This code is a mess. I know you're just learning, but how will you learn if you do things badly ? No centralisation of SQL code, a connection string in each methods, open to SQL injection attacks...

The first step is to debug and work out what your code is creating for a string of SQL. Then run that against your DB manually and see what errors the DB reports.
 
Share this answer
 
Comments
Buena Velasco 5-Jul-11 3:42am    
Thanks Mr. Graus, I'm able to solve it, took out the other connection string and then added cmd.ExecuteNonQuery. :)
<pre>
<pre lang="vb">Private Sub addbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addbtn.Click
If lastnametb.Text = "" Or firstnametb.Text = "" Or agetb.Text = "" Or loctb.Text = "" Or unametb.Text = "" Or pwordtb.Text = "" Or cpwordtb.Text = "" Then
MsgBox("Please complete the fields!", MsgBoxStyle.Exclamation, "System Message")
Else

Dim connection As New MySqlConnection("Server = localhost; User Id = root; Password = root; Database = add_edit_delete")
connection.Open()
Try

strSql = "INSERT INTO register(LastName, FirstName, MiddleInitial, Age, Location, Uname, Pword) VALUES('" & lastnametb.Text & "','" & firstnametb.Text & "','" & middletb.Text & "','" & agetb.Text & "','" & loctb.Text & "','" & unametb.Text & "','" & pwordtb.Text & "')"
cmd.CommandText = strSql
cmd.Connection = connection
cmd.ExecuteNonQuery()

MsgBox("Record has been saved!", MsgBoxStyle.Information, "System Message")
connection.Close()
Me.Dispose()
Catch ex As Exception
MsgBox("Unable to save record!", MsgBoxStyle.Critical, "System Message")
connection.Close()

End Try
End If
End Sub</pre>

</pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900