03-21-2012, 04:10 PM
Code:
Imports MySql.Data.MySqlClient
Public Class frmMain
Public conn As MySqlConnection
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim NAME As Boolean = True
conn = New MySqlConnection("server=db4free.net;Port=3306;user id=PRIVATE;password=PRIVATE;database=PRIVATE")
Dim sqlquery As String = "SELECT * FROM reloads WHERE Name = '" & txtName.Text & "';"
Dim data As MySqlDataReader
Dim adapter As New MySqlDataAdapter
Dim command As New MySqlCommand
command.CommandText = sqlquery
command.Connection = conn
conn.Open()
adapter.SelectCommand = command
data = command.ExecuteReader
While data.Read()
If data.HasRows() = True Then
MsgBox("Name Already in use!")
NAME = False
data.Close()
Else
data.Close()
NAME = True
End If
End While
data.Close()
conn.close()
If NAME = True Then
Dim sqlquery2 As String = "INSERT INTO reloads (name) VALUES(1)"
MsgBox(sqlquery2)
Dim data2 As MySqlDataReader
Dim adapter2 As New MySqlDataAdapter
Dim command2 As New MySqlCommand
command2.CommandText = sqlquery2
command2.Connection = conn
conn.open()
adapter2.SelectCommand = command2
data2 = command2.ExecuteReader
MsgBox("Data Added")
conn.close()
Else
MsgBox("No Data Entered")
End If
End Sub
End Class
Try closing and opening your connection before you execute new pieces of code. I have inserted these segments into your code if you simply want to copy paste.