I'm having some problems when I try to insert data to my database(mysql). It seems my connection isn't working, but I doubt it because I can retrieve data from it yet no luck on inserting. I've also tried various things and I still can't come up with a solution nor find the cause of error.

Can somebody help me out? Your concern is very much appreciated!

Here's my Registration.aspx page

Imports MySql.Data.MySqlClient
Imports System.Web.Security
Imports System.Security.Cryptography
Imports System.Web.Configuration
Imports System.Configuration
Partial Class Register
    Inherits System.Web.UI.Page
    Dim ctc As New Class1
    Dim conn As MySqlConnection
    Dim connstr As String = ConfigurationManager.ConnectionStrings("dbconn").ConnectionString
    Dim cmd As MySqlCommand
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

            conn = New MySqlConnection(connstr)

            cmd = New MySqlCommand("INSERT INTO tbl_uinfo (user_id, fname, lname, email, pword, paypal) values (null,@fname,@lname,@email,@pword,@paypal)")
            cmd.Parameters.Add("@fname", MySqlDbType.VarChar, 45).Value = firstname.Text
            cmd.Parameters.Add("@lname", MySqlDbType.VarChar, 45).Value = lastname.Text
            cmd.Parameters.Add("@email", MySqlDbType.VarChar, 45).Value = email.Text
            cmd.Parameters.Add("@pword", MySqlDbType.VarChar, 250).Value = password2.Text
            cmd.Parameters.Add("@paypal", MySqlDbType.VarChar, 45).Value = paypal.Text


        Catch ex As Exception
            MsgBox("Registration failed." & ex.Message)
        End Try

    End Sub
End Class

Here's my ConnectionString (web.config)

        <add name="dbconn" connectionString="server=localhost;database=db_aspweb;user id=root; pwd=root"/>

My Database

CREATE TABLE `tbl_uinfo` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `fname` varchar(45) DEFAULT NULL,
  `lname` varchar(45) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `pword` varchar(250) DEFAULT NULL,
  `paypal` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `email` (`email`),
  UNIQUE KEY `paypal` (`paypal`)

The error I am getting so far

"Registration Failed. Connection must be valid and open."

Related posts

Recent Viewed