This are my code, It worked perfectly except for ONE. I can't update:
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim con As SqlConnection
Dim Cmd As SqlCommand
Dim Query As String
Sub disk()
con = New SqlConnection
con.ConnectionString = "Server=Jansen\SQLEXPRESS;Database=AJLMNewProjectDatabase;Trusted_Connection=True;"
Try
con.Open()
Dim READER As SqlDataReader
Query = "UPDATE AdminESP SET Benefactor='" & CmbBenefactor.Text & "' WHERE ExternalSP='" & CmbExternalSP.Text & "'"
Cmd = New SqlCommand(Query, con)
READER = Cmd.ExecuteReader()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
End Try
con.Close()
End Sub
Sub dis()
CmbExternalSP.Items.Clear()
CmbBenefactor.Items.Clear()
con = New SqlConnection
con.ConnectionString = "Server=Jansen\SQLEXPRESS;Database=AJLMNewProjectDatabase;Trusted_Connection=True;"
Try
con.Open()
Dim READER As SqlDataReader
Query = "select * from [AJLMNewProjectDatabase].[dbo].[AdminESP]"
Cmd = New SqlCommand(Query, con)
READER = Cmd.ExecuteReader()
While READER.Read
Dim ESP = READER.GetString(1)
Dim Ben = READER.GetString(2)
CmbExternalSP.Items.Add(ESP)
CmbBenefactor.Items.Add(Ben)
End While
con.Close()
Catch ex As SqlException
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con = New SqlConnection
con.ConnectionString = "Server=Jansen\SQLEXPRESS;Database=AJLMNewProjectDatabase;Trusted_Connection=True;"
Try
con.Open()
Dim READER As SqlDataReader
Query = "select * from AdminESP"
Cmd = New SqlCommand(Query, con)
READER = Cmd.ExecuteReader()
While READER.Read
Dim ESP = READER.GetString(1)
Dim Ben = READER.GetString(2)
CmbExternalSP.Items.Add(ESP)
CmbBenefactor.Items.Add(Ben)
End While
con.Close()
Catch ex As SqlException
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try
End Sub
Private Sub CmbExternalSP_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbExternalSP.SelectedIndexChanged
CmbBenefactor.SelectedIndex = CmbExternalSP.SelectedIndex
End Sub
Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
con = New SqlConnection("Server=Jansen\SQLEXPRESS;Database=AJLMNewProjectDatabase;Trusted_Connection=True;")
con.Open()
Cmd = con.CreateCommand
Cmd.CommandText = String.Format("INSERT INTO AdminESP(ExternalSP, Benefactor) " &
"VALUES('{0}', '{1}')",
CmbExternalSP.Text,
CmbBenefactor.Text) ' To Save data to ExternalSP DataTable
Dim rowsAffected As Integer = Cmd.ExecuteNonQuery()
If rowsAffected > 0 Then
MsgBox("Program Added!")
Else
MsgBox("Failed to Add")
End If
CmbExternalSP.Text = ""
CmbBenefactor.Text = ""
con.Close()
End Sub
Private Sub BtnRefresh_Click(sender As Object, e As EventArgs) Handles BtnRefresh.Click
CmbExternalSP.DropDownStyle = ComboBoxStyle.DropDown
CmbBenefactor.DropDownStyle = ComboBoxStyle.DropDown
CmbExternalSP.Text = ""
CmbBenefactor.Text = ""
End Sub
Private Sub BtnRemove_Click(sender As Object, e As EventArgs) Handles BtnRemove.Click
con = New SqlConnection
con.ConnectionString = "Server=Jansen\SQLEXPRESS;Database=AJLMNewProjectDatabase;Trusted_Connection=True;"
Try
con.Open()
Dim READER As SqlDataReader
Query = "DELETE FROM AdminESP WHERE ExternalSP='" + CmbExternalSP.Text + "'"
Cmd = New SqlCommand(Query, con)
READER = Cmd.ExecuteReader()
If MessageBox.Show("You are about to remove a Program, Please confirm your Action.", "DELETE", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.OK Then
MsgBox("Program Removed")
End If
CmbExternalSP.Text = ""
CmbBenefactor.Text = ""
CmbExternalSP.DropDownStyle = ComboBoxStyle.DropDownList
CmbBenefactor.DropDownStyle = ComboBoxStyle.DropDownList
Call dis()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
End Try
con.Close()
End Sub
Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles BtnUpdate.Click
con = New SqlConnection
con.ConnectionString = "Server=Jansen\SQLEXPRESS;Database=AJLMNewProjectDatabase;Trusted_Connection=True;"
Try
con.Open()
Dim READER As SqlDataReader
Query = "UPDATE AdminESP SET ExternalSP='" & CmbExternalSP.Text & "' WHERE Benefactor='" & CmbBenefactor.Text & "'"
Cmd = New SqlCommand(Query, con)
READER = Cmd.ExecuteReader()
MessageBox.Show("Program Updated.")
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
End Try
con.Close()
End Sub
Private Sub BtnR_Click(sender As Object, e As EventArgs) Handles BtnR.Click
Call dis()
End Sub
Private Sub CmbBenefactor_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbBenefactor.SelectedIndexChanged
CmbExternalSP.SelectedIndex = CmbBenefactor.SelectedIndex
End Sub
End Class
Now my problem is, When I click UPDATE button, only one column which is under combobox 1 updates; the other combobox doesn't change at all.
Related
i made an listview for all poeple who scanned their ID in the system. Everything is working while I was doing the listview code however when I go back. If I log into my administrator account I found out that there is still an active connection however I traced back my code yet i have not yet found an open connection. This is my code
Imports MySql.Data.MySqlClient
Public Class admin
Private Sub admin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ConnectToDB()
Dim il = New ImageList()
sql = "select * from rfidmaintest.monitoring ORDER BY lname ASC"
cmd = New MySqlCommand(sql, cn)
dr = cmd.ExecuteReader
ListView1.Items.Clear()
While dr.Read
newLine = ListView1.Items.Add(dr("id_num"))
newLine.SubItems.Add(dr("fname"))
newLine.SubItems.Add(dr("lname"))
newLine.SubItems.Add(dr("status"))
newLine.SubItems.Add(dr("entry_record"))
newLine.SubItems.Add(dr("floor_level"))
newLine.SubItems.Add(dr("date"))
End While
cmd = Nothing
dr.Close()
cn.Close()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
dr.Close()
ConnectToDB()
Dim il = New ImageList()
sql = "select * from rfidmaintest.monitoring where floor_level = 1 ORDER BY lname ASC"
cmd = New MySqlCommand(sql, cn)
dr = cmd.ExecuteReader
ListView1.Items.Clear()
While dr.Read
newLine = ListView1.Items.Add(dr("id_num"))
newLine.SubItems.Add(dr("fname"))
newLine.SubItems.Add(dr("lname"))
newLine.SubItems.Add(dr("status"))
newLine.SubItems.Add(dr("entry_record"))
newLine.SubItems.Add(dr("floor_level"))
newLine.SubItems.Add(dr("date"))
End While
cmd = Nothing
dr.Close()
cn.Close()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
dr.Close()
ConnectToDB()
Dim il = New ImageList()
sql = "select * from rfidmaintest.monitoring where floor_level = 2 ORDER BY lname ASC"
cmd = New MySqlCommand(sql, cn)
dr = cmd.ExecuteReader
ListView1.Items.Clear()
While dr.Read
newLine = ListView1.Items.Add(dr("id_num"))
newLine.SubItems.Add(dr("fname"))
newLine.SubItems.Add(dr("lname"))
newLine.SubItems.Add(dr("status"))
newLine.SubItems.Add(dr("entry_record"))
newLine.SubItems.Add(dr("floor_level"))
newLine.SubItems.Add(dr("date"))
End While
cmd = Nothing
dr.Close()
cn.Close()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
dr.Close()
ConnectToDB()
Dim il = New ImageList()
sql = "select * from rfidmaintest.monitoring where floor_level = 3 ORDER BY lname ASC"
cmd = New MySqlCommand(sql, cn)
dr = cmd.ExecuteReader
ListView1.Items.Clear()
While dr.Read
newLine = ListView1.Items.Add(dr("id_num"))
newLine.SubItems.Add(dr("fname"))
newLine.SubItems.Add(dr("lname"))
newLine.SubItems.Add(dr("status"))
newLine.SubItems.Add(dr("entry_record"))
newLine.SubItems.Add(dr("floor_level"))
newLine.SubItems.Add(dr("date"))
End While
cmd = Nothing
dr.Close()
cn.Close()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
dr.Close()
ConnectToDB()
Dim il = New ImageList()
sql = "select * from rfidmaintest.monitoring ORDER BY lname ASC"
cmd = New MySqlCommand(sql, cn)
dr = cmd.ExecuteReader
ListView1.Items.Clear()
While dr.Read
newLine = ListView1.Items.Add(dr("id_num"))
newLine.SubItems.Add(dr("fname"))
newLine.SubItems.Add(dr("lname"))
newLine.SubItems.Add(dr("status"))
newLine.SubItems.Add(dr("entry_record"))
newLine.SubItems.Add(dr("floor_level"))
newLine.SubItems.Add(dr("date"))
End While
cmd = Nothing
dr.Close()
cn.Close()
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
dr.Close()
ConnectToDB()
Dim il = New ImageList()
sql = "select * from rfidmaintest.monitoring ORDER BY lname ASC "
cmd = New MySqlCommand(sql, cn)
dr = cmd.ExecuteReader
ListView1.Items.Clear()
While dr.Read
newLine = ListView1.Items.Add(dr("id_num"))
newLine.SubItems.Add(dr("fname"))
newLine.SubItems.Add(dr("lname"))
newLine.SubItems.Add(dr("status"))
newLine.SubItems.Add(dr("entry_record"))
newLine.SubItems.Add(dr("floor_level"))
newLine.SubItems.Add(dr("date"))
End While
cmd = Nothing
dr.Close()
cn.Close()
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
dr.Close()
ConnectToDB()
Dim listitems As ListViewItem
sql = " SELECT * FROM monitoring WHERE lname like '%" + TextBox1.Text + "%'"
cmd = New MySqlCommand(sql, cn)
cmd.Parameters.AddWithValue("#1", TextBox1.Text)
dr = cmd.ExecuteReader
ListView1.Items.Clear()
While dr.Read
listitems = ListView1.Items.Add(dr("id_num"))
listitems.SubItems.Add(dr("fname"))
listitems.SubItems.Add(dr("lname"))
listitems.SubItems.Add(dr("status"))
listitems.SubItems.Add(dr("entry_record"))
listitems.SubItems.Add(dr("floor_level"))
listitems.SubItems.Add(dr("date"))
End While
cmd = Nothing
dr.Close()
cn.Close()
End Sub
End Class
I have a form with retrieved data form ms access database. While updating using the adapter update statement, it shows Syntax error.
The following are the connection code in form load event and update button click event.
could you please let me know whats wrong in this?
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\DESKTOP\VBATESTING\ADPE Project\Project Tables\ADPE_Table.accdb"
inc = 0
Dim Con As New OleDbConnection()
Dim Cmd As New OleDbCommand
Dim SQL As String = "SELECT * FROM Heidelberg"
Con.ConnectionString = strConn
Try
Con.Open()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Cmd = New OleDbCommand(SQL, Con)
Adapter.SelectCommand = New OleDbCommand(SQL, Con)
ds = New DataSet
Adapter.Fill(ds, "testing")
MaxRows = ds.Tables("testing").Rows.Count
Label1.Text = "Total Records :" & MaxRows
NavigateRecords()
End Sub
Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
Dim cb As New OleDb.OleDbCommandBuilder(Adapter)
ds.Tables("testing").Rows(inc).Item(5) = TextBox1.Text
ds.Tables("testing").Rows(inc).Item(2) = TextBox2.Text
ds.Tables("testing").Rows(inc).Item(3) = TextBox3.Text
ds.Tables("testing").Rows(inc).Item(4) = TextBox4.Text
Try
Adapter.Update(ds, "testing")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
You have way too many commands in your Form.Load.
One
Dim Cmd As New OleDbCommand
Two
Cmd = New OleDbCommand(SQL, Con)
Three
Adapter.SelectCommand = New OleDbCommand(SQL, Con)
Every time you use the New keyword, you create another command.
If you are only dealing with a single table, you don't need a DataSet. The extra object adds extra weight and complicates the code a bit.
Private inc As Integer
Private Adapter As New OleDbDataAdapter
Private MaxRows As Integer
Private dat As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\DESKTOP\VBATESTING\ADPE Project\Project Tables\ADPE_Table.accdb"
inc = 0
Using Con As New OleDbConnection(strConn)
Using Cmd As New OleDbCommand("SELECT * FROM Heidelberg;", Con)
Adapter.SelectCommand = Cmd
Try
Adapter.Fill(dat)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Using
End Using
MaxRows = dat.Rows.Count
Label1.Text = "Total Records :" & MaxRows
NavigateRecords()
End Sub
Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
dat.Rows(inc).Item(5) = TextBox1.Text
dat.Rows(inc).Item(2) = TextBox2.Text
dat.Rows(inc).Item(3) = TextBox3.Text
dat.Rows(inc).Item(4) = TextBox4.Text
Dim cb As New OleDbCommandBuilder(Adapter)
'This will take care of any spaces that you have in column names.
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
Try
Adapter.Update(dat)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class search
Dim conn As New SqlConnection
Dim da As New SqlDataAdapter
Dim strcon As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Application\Abhi\Software\Software\Vansh.mdf;Integrated Security=True;User Instance=True"
Private Sub search_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Application\Abhi\Software\Software\Vansh.mdf;Integrated Security=True;User Instance=True"
conn = New SqlConnection(strcon)
conn.Open()
loaddata()
' txtsearch.Enabled = False
' txtsearch.Text = "Student Id is auto number"
End Sub
Public Sub loaddata()
conn = New SqlConnection(strcon)
conn.Open()
Dim str As String = "Select * From Payment "
da = New SqlDataAdapter(str, conn)
Dim ds As New DataSet
da.Fill(ds, "Payment")
DataGridView1.DataSource = ds.Tables("Payment")
da.Dispose()
conn.Close()
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
For Each txt In {txtmo, txtsearch}
txt.Clear()
Next
End Sub
Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub btnhome_Click(sender As System.Object, e As System.EventArgs) Handles btnhome.Click
Me.Hide()
Dim rd As New Home_Page
rd.Show()
End Sub
Private Sub txtsearch_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtsearch.TextChanged
Try
conn = New SqlConnection(strcon)
conn.Open()
If txtsearch.Text = " " Then
loaddata()
Else
conn = New SqlConnection(strcon)
conn.Open()
Dim Str As String = "Select Date, Name, Amount from Payment where Name like '" + txtsearch.Text + "%' "
da = New SqlDataAdapter(Str, conn)
Dim ds As New DataSet
da.Fill(ds, "Payment")
DataGridView1.DataSource = ds.Tables("Payment")
da.Dispose()
conn.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub txtmo_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtmo.TextChanged
Try
conn = New SqlConnection(strcon)
conn.Open()
If txtsearch.Text = " " Then
loaddata()
Else
conn = New SqlConnection(strcon)
conn.Open()
Dim Str As String = "Select Date, Name, Amount from Payment where Date like '" + txtmo.Text + "%' "
da = New SqlDataAdapter(Str, conn)
Dim ds As New DataSet
da.Fill(ds, "Payment")
DataGridView1.DataSource = ds.Tables("Payment")
da.Dispose()
conn.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged
Try
conn = New SqlConnection(strcon)
conn.Open()
If txtsearch.Text = " " Then
loaddata()
Else
conn = New SqlConnection(strcon)
conn.Open()
Dim Str As String = "Select Date, Name, Amount from Payment where Date like '" + DateTimePicker1.Text + "%' "
da = New SqlDataAdapter(Str, conn)
Dim ds As New DataSet
da.Fill(ds, "Payment")
DataGridView1.DataSource = ds.Tables("Payment")
da.Dispose()
conn.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub DataGridView2_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick
End Sub
End Class
My data table is loaded in 2 places, a DataGridView and a ComboBox
The ComboBox is to select a record to edit (a TextBox to enter a new value)
And the DataGridView is to see the changes (I gave up (for now) updating directly from the DataGridView)
Private Sub EditLoc_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
con.Open()
Dim sql = Nothing
sql = "SELECT Location FROM Location"
Dim cmdDataGrid As SQLiteCommand = New SQLiteCommand(sql, con)
Dim da As New SQLiteDataAdapter
da.SelectCommand = cmdDataGrid
Dim dt As New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
Dim readerDataGrid As SQLiteDataReader = cmdDataGrid.ExecuteReader()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Try ' TRY CATCH for combobox
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT Location FROM Location"
dr = cmd.ExecuteReader()
' Fill a combo box with the datareader
Do While dr.Read = True
ComboBox1.Items.Add(dr.GetString(0))
Loop
If ComboBox1.Items.Count > 0 Then
ComboBox1.SelectedIndex = 0 ' The first item has index 0 '
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
This works perfectly
Picture
The problem is when I click Save, the app hangs a while, and then I get the "Database is locked" error picture
Here is the code for the Save button:
Private Sub Savebtn_Click(sender As Object, e As EventArgs) Handles Savebtn.Click
Try
con.Open()
cmd = con.CreateCommand
cmd.CommandText = "UPDATE Location set Location = '" & TextBox1.Text & "' WHERE Location = '" & ComboBox1.Text & "'"
cmd.ExecuteNonQuery()
cmd.Dispose()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Thanks for the help
I fixed this problem by removing all "cmd.Dispose()", "da.Dispose()" and "con.Close()" from the code, leaving them ONLY in
Private Sub Closebtn_Click(sender As Object, e As EventArgs) Handles Closebtn.Click
da.Dispose()
cmd.Dispose()
con.Close()
Dim fems As New EMS
fems.Show()
Me.Close()
End Sub
On Form Load I have
Private Sub EditLoc_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.Open()
Call pull()
End Sub
And the Pull sub has all the rest...
Private Sub pull()
Try
Dim sql = Nothing
sql = "SELECT Location FROM Location"
Dim cmdDataGrid As SQLiteCommand = New SQLiteCommand(sql, con)
da.SelectCommand = cmdDataGrid
Dim dt As New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
Dim readerDataGrid As SQLiteDataReader = cmdDataGrid.ExecuteReader()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Try ' TRY CATCH for combobox
cmd.Connection = con
cmd.CommandText = "SELECT Location FROM Location"
dr = cmd.ExecuteReader()
' Fill a combo box with the datareader
Do While dr.Read = True
ComboBox1.Items.Add(dr.GetString(0))
Loop
If ComboBox1.Items.Count > 0 Then
ComboBox1.SelectedIndex = 0 ' The first item has index 0 '
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
And here is my Save button
Private Sub Savebtn_Click(sender As Object, e As EventArgs) Handles Savebtn.Click
If Not TextBox1.Text = Nothing Then
Try
cmd = con.CreateCommand
cmd.CommandText = "UPDATE Location set Location = '" & TextBox1.Text & "' WHERE Location = '" & ComboBox1.Text & "'"
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Call pull()
TextBox1.Text = Nothing
End If
End Sub
Now everything is working, no errors!
Calling pull() after saving will update the DataGridView
Thanks for the help
Imports MySql.Data.MySqlClient
Public Class manageAdminAccounts
Dim MysqlConn As MySqlConnection
Dim COMMAND As MySqlCommand
Dim dbDataSet As New DataTable
Dim SDA As New MySqlDataAdapter
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
manageAccounts.Show()
Me.Hide()
End Sub
Private Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "Server=localhost;Uid=root;Pwd=;Database=ilycean;"
Dim READER As MySqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "insert into ilycean.users(name,email,password) values ('" & txtName.Text & "', '" & txtEmail.Text & "', '" & txtPassword.Text & "')"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
MessageBox.Show("Data inserted")
txtID.ResetText()
txtName.ResetText()
txtEmail.ResetText()
txtPassword.ResetText()
txtPassword.ResetText()
RefreshData()
READER.Close()
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
COMMAND.Dispose()
End Try
End Sub
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "Server=localhost;Uid=root;Pwd=;Database=ilycean;"
Dim READER As MySqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "update ilycean.users Set name='" & txtName.Text & "', email='" & txtEmail.Text & "', password='" & txtPassword.Text & "' where id='" & txtID.Text & "' "
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
MessageBox.Show("Data Updated")
txtID.ResetText()
txtName.ResetText()
txtEmail.ResetText()
txtPassword.ResetText()
txtPassword.ResetText()
RefreshData()
READER.Close()
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
COMMAND.Dispose()
End Try
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "Server=localhost;Uid=root;Pwd=;Database=ilycean;"
Dim READER As MySqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "Delete from ilycean.users where id='" & txtID.Text & "'"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
MessageBox.Show("Data Deleted")
txtID.ResetText()
txtName.ResetText()
txtEmail.ResetText()
txtPassword.ResetText()
txtPassword.ResetText()
RefreshData()
READER.Close()
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
COMMAND.Dispose()
End Try
End Sub
Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "Server=localhost;Uid=root;Pwd=;Database=ilycean;"
Dim SDA As New MySqlDataAdapter
Dim dbDataSet As New DataTable
Dim bSource As New BindingSource
Try
MysqlConn.Open()
Dim Query As String
Query = "select * from ilycean.users"
COMMAND = New MySqlCommand(Query, MysqlConn)
SDA.SelectCommand = COMMAND
SDA.Fill(dbDataSet)
bSource.DataSource = dbDataSet
dataGridViewAdminAccounts.DataSource = bSource
SDA.Update(dbDataSet)
txtID.ResetText()
txtName.ResetText()
txtEmail.ResetText()
txtPassword.ResetText()
RefreshData()
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
End Sub
Private Sub loadTable()
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "Server=localhost;Uid=root;Pwd=;Database=ilycean;"
Dim SDA As New MySqlDataAdapter
Dim bSource As New BindingSource
Try
MysqlConn.Open()
Dim Query As String
Query = "select * from ilycean.users"
COMMAND = New MySqlCommand(Query, MysqlConn)
SDA.SelectCommand = COMMAND
SDA.Fill(dbDataSet)
bSource.DataSource = dbDataSet
dataGridViewAdminAccounts.DataSource = bSource
SDA.Update(dbDataSet)
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
End Sub
Private Sub dataGridViewAdminAccounts_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dataGridViewAdminAccounts.CellContentClick
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.dataGridViewAdminAccounts.Rows(e.RowIndex)
txtID.Text = row.Cells("id").Value.ToString
txtName.Text = row.Cells("name").Value.ToString
txtEmail.Text = row.Cells("email").Value.ToString
txtPassword.Text = row.Cells("password").Value.ToString
End If
End Sub
Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
Dim DV As New DataView(dbDataSet)
DV.RowFilter = String.Format("name Like'%{0}%'", txtSearch.Text)
dataGridViewAdminAccounts.DataSource = DV
End Sub
Private Sub manageAdminAccounts_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Dim dialog As DialogResult
dialog = MessageBox.Show("Do you really want to exit?", "Exit", MessageBoxButtons.YesNo)
If dialog = DialogResult.No Then
e.Cancel = True
Else
Application.ExitThread()
End If
End Sub
Private Sub manageAdminAccounts_Load(sender As Object, e As EventArgs) Handles MyBase.Load
loadTable()
txtID.ResetText()
txtName.ResetText()
txtEmail.ResetText()
txtPassword.ResetText()
txtPassword.ResetText()
End Sub
Public Sub RefreshData()
Dim Query As String
Query = "select * from ilycean.users"
dbDataSet.Clear()
COMMAND = New MySqlCommand(Query, MysqlConn)
SDA.SelectCommand = COMMAND
SDA.Fill(dbDataSet)
dataGridViewAdminAccounts.DataSource = dbDataSet
End Sub
End Class
Everytime I Insert, Update or Delete, the following error keeps on appearing:
There is already an open DataReader associated with this Command which must be closed first.
I already tried adding MultipleActiveResultSets=True; on my connection string but it still doesn't work.
In ExecuteReader use CommandBehavior.CloseConnection
as
MySqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
also using the using construct is good for unmanaged resources like SQL connections
Using resource As New resourceType
' Insert code to work with resource.
End Using