Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
I am making a web site in ASP.Net using VB
And i am new in asp.net
I'd like to know how to insert in mysql database
Thanks
Happy New Year Smile | :)

{Edit - OP code from comment]
Imports MySql.Data.MySqlClient
Imports System.Data

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim sqladapter As MySqlDataAdapter
    Dim sqlcommand As MySqlCommand
    Dim constr As String = "Database=teste;" & _
   "Data Source=localhost;" & _
   "User Id=root;"
    Dim con As New MySqlConnection(constr)
    Dim sqlQuery As String
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim cmd1 As New MySqlCommand("SELECT username FROM dados WHERE username='" & username.Text & "'", con)
        con.Close()
        con.Open()
        Dim reader As MySqlDataReader = cmd1.ExecuteReader
        If reader.HasRows = False Then
            Console.WriteLine("INSERT INTO dados (username, password) VALUES ('" & username.Text & "', '" & password.Text & "'")
            username.Text = ""
            password.Text = ""

        End If
        con.Close()
    End Sub

I have tried this code but when i click to regist nothing happens
Posted
Updated 1-Jan-14 7:57am
v2
Comments
ZurdoDev 1-Jan-14 11:26am    
I already answered this. Why are you reposting?
Member 10494891 1-Jan-14 11:29am    
I have deleted the other one by mistake
Sorry :/
Can you help me?
ZurdoDev 1-Jan-14 11:34am    
Sure. The problem is there are many ways to do this. I would suggest creating yourself a helper class (perhaps even search google for one) and use the SqlConnection and SqlCommand classes. There's more to it than that but that should get you started.
Member 10494891 1-Jan-14 11:44am    
Imports MySql.Data.MySqlClient
Imports System.Data

Partial Class _Default
Inherits System.Web.UI.Page
Dim sqladapter As MySqlDataAdapter
Dim sqlcommand As MySqlCommand
Dim constr As String = "Database=teste;" & _
"Data Source=localhost;" & _
"User Id=root;"
Dim con As New MySqlConnection(constr)
Dim sqlQuery As String
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cmd1 As New MySqlCommand("SELECT username FROM dados WHERE username='" & username.Text & "'", con)
con.Close()
con.Open()
Dim reader As MySqlDataReader = cmd1.ExecuteReader
If reader.HasRows = False Then
Console.WriteLine("INSERT INTO dados (username, password) VALUES ('" & username.Text & "', '" & password.Text & "'")
username.Text = ""
password.Text = ""

End If
con.Close()
End Sub
I have tried this code but when i click to regist nothing happens
JoCodes 1-Jan-14 13:20pm    
Getting any error?

For insert record try something as below code sample inside the button click event.

VB
Dim retVal As Integer
Dim sConnectionString As String = "server=(local);database=dbName;user=ASPNET" //replace your connection string here
Dim conn As New SqlConnection(sConnectionString)
        Dim sSQL As String
        sSQL = "INSERT INTO TableName(ColumnName1, ColumnName2)" & _
           " VALUES (@parameter1, @parameter2)"
        Dim cmd As New SqlCommand(sSQL, conn)   
        
        cmd.Parameters.Add(New SqlParameter("@firstname", "SomeValue"))
        cmd.Parameters.Add(New SqlParameter("@lastname", "SomeValue"))
        conn.Open()
        Try
            retVal = cmd.ExecuteNonQuery()
            
        Catch ex As System.Exception
           //Error Code 
        Finally
            conn.Close()
        End Try
    End Sub
 
Share this answer
 
v2
Comments
Member 10494891 1-Jan-14 14:27pm    
do i need to do anything in the html code?
JoCodes 2-Jan-14 1:16am    
Yes, if you intend to key in the inputs to insert through a webpage.
The issue is after you check to see if the user exists in your database, instead of actually executing the SQL to insert the user into your database you are just writing the Sql string to the console. You need to execute the sql instead.
 
Share this answer
 

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