Retrieve data from database set by combobox to textbox - vb.net

I have a form , which have 1 combobox and 1 textbox.
and a table named tbl_dress which have columns as Dress_ID, Dress_Name, Dress_Price..
the combobox shows the Dress_Name and the code works..
Code for the combobox :-
Private Sub FillCombo()
Try
Dim fillcon As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\DressTest\DressTest\db\PriceTesting.accdb")
Dim query As String = ("SELECT Dress_Name FROM tbl_dress")
Dim da As New OleDb.OleDbDataAdapter(query, fillcon)
Dim ds As New DataSet
da.Fill(ds)
ComboBox1.ValueMember = "Dress_Name"
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.SelectedIndex = 0
Catch ex As Exception
MsgBox("ERROR : " & ex.Message.ToString)
End Try
End Sub
and this is the code when my form is loaded :-
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim con As New OleDb.OleDbConnection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\TMS Final\TMS Final\db\db_TMS.accdb"
con.Open()
FillCombo() ' Display data from tbl_order on form load
con.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
so the question is , how do i get the Dress_Price , determined by the Dress_Name chosen on the combobox.
i have tried the following code , but i have errors.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
Dim fillcon As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\DressTest\DressTest\db\PriceTesting.accdb")
fillcon.Open()
Dim query As String = ("SELECT Dress_Price FROM tbl_dress WHERE Dress_Name = ' " & ComboBox1.SelectedValue.ToString & " ' ")
Dim cmd As New OleDb.OleDbCommand(query, fillcon)
cmd.CommandText = query
TextBox1.Text = cmd.ExecuteScalar().ToString()
fillcon.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
so where did i go wrong and what should i do? new to vb.net

Try this line on execution
TextBox1.Text= Convert.ToInt32(cmd.ExecuteScalar()).ToString()

My question was whether ur getting the desired value at that point
can you put stopper near the below method and see the return value
Dim query As String = ("SELECT Dress_Price FROM tbl_dress WHERE Dress_Name = ' " & ComboBox1.SelectedValue.ToString & " ' ")
try to hard code the dress name and check. Also try Select Top 1

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
Dim fillcon As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\DressTest\DressTest\db\PriceTesting.accdb")
fillcon.Open()
Dim query As String = ("SELECT Dress_Price FROM tbl_dress WHERE Dress_Name = ' " & ComboBox1.Text & " ' ")
Dim cmd As New OleDb.OleDbCommand(query, fillcon)
cmd.CommandText = query
TextBox1.Text = cmd.ExecuteScalar().ToString()
fillcon.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub

Related

VB.NET SQL Database locked

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

Inserting combo box values?

I'm trying to get my system to insert combo box values into my access database. I always get this very long error whenever I try to click my 'add' button and I somehow get this feeling that it's because of my INSERT statement. This is my whole code for my form. Any help will be greatly appreciated! Thank you
Imports System.Data.OleDb
Public Class AdmMain
Sub fillcombo()
strsql = " select yrgr from yearandgrade"
Dim acscmd As New OleDb.OleDbCommand
acscmd.CommandText = strsql
acscmd.Connection = acsconn
acsdr = acscmd.ExecuteReader
While (acsdr.Read())
cboyr.Items.Add(acsdr("yrgr"))
End While
acscmd.Dispose()
acsdr.Close()
End Sub
Sub comb2()
strsql = " select sections from sectio"
Dim acscmd As New OleDb.OleDbCommand
acscmd.CommandText = strsql
acscmd.Connection = acsconn
acsdr = acscmd.ExecuteReader
While (acsdr.Read())
cbosec.Items.Add(acsdr("sections"))
End While
acscmd.Dispose()
acsdr.Close()
End Sub
Private Sub LinkLabel1_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
If MessageBox.Show("Are you sure you want to logout?", "Logout", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
MessageBox.Show("You have successfully logged out of VCM's Library Information System!", "Logout Confirmed")
Me.Close()
LoginUser.Show()
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Me.txtFName.Text = ""
Me.txtMName.Text = ""
Me.txtLName.Text = ""
Me.cboyr.Text = ""
Me.cbosec.Text = ""
Me.txtFName.Focus()
End Sub
Private Sub AdmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Module1.connect()
Me.fillcombo()
Me.comb2()
End Sub
Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
Dim rbdtext As String = cboyr.SelectedItem.ToString
Dim uno As String = cbosec.SelectedItem.ToString
Try
Using conn = New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Thesis\Thesis\Database1.accdb"
conn.Open()
Dim sqlquery As String = "INSERT INTO students (StudentID, FirstName,MiddleName,LastName,Yr, Section) " & _
"VALUES (#studid, #fname,#mname,#lname,#yr, #sec)"
Dim SqlCommand As New System.Data.OleDb.OleDbCommand
SqlCommand.Parameters.AddWithValue("#studid", TxtID.Text)
SqlCommand.Parameters.AddWithValue("#fname", txtFName.Text)
SqlCommand.Parameters.AddWithValue("#mname", txtMName.Text)
SqlCommand.Parameters.AddWithValue("#lname", txtLName.Text)
SqlCommand.Parameters.AddWithValue("#yr", rbdtext)
SqlCommand.Parameters.AddWithValue("#sec", uno)
SqlCommand.Connection = conn
Dim sqlRead As System.Data.OleDb.OleDbDataReader = SqlCommand.ExecuteReader()
MsgBox("One record successfully added!", "Added!")
End Using
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Heres the error the keeps showing btw!
http://i.imgur.com/DgjiWqm.png
It looks like you are never assigning a select statement to SqlCommand inside your btnAdd_Click method. Try adding SqlCommand.CommandText = sqlquery.
You need to change your query in this way
Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
Dim rbdtext As String = cboyr.SelectedItem.ToString
Dim uno As String = cbosec.SelectedItem.ToString
Try
Dim cnString = = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\Thesis\Thesis\Database1.accdb"
Dim sqlquery As String = "INSERT INTO students " & _
"(StudentID, FirstName,MiddleName,LastName,Yr, [Section]) " & _
"VALUES (#studid, #fname,#mname,#lname,#yr, #sec)"
' Use this form to initialize both connection and command to
' avoid forgetting to set the appropriate properties....
Using conn = New System.Data.OleDb.OleDbConnection(cnString)
Using cmd = New System.Data.OleDb.OleDbCommand(sqlQuery, conn)
conn.Open()
cmd.Parameters.AddWithValue("#studid", TxtID.Text)
cmd.Parameters.AddWithValue("#fname", txtFName.Text)
cmd.Parameters.AddWithValue("#mname", txtMName.Text)
cmd.Parameters.AddWithValue("#lname", txtLName.Text)
cmd.Parameters.AddWithValue("#yr", rbdtext)
cmd.Parameters.AddWithValue("#sec", uno)
Dim rowsInserted = cmd.ExecuteNonQuery()
if rowsInserted > 0 Then
MessageBox.Show("One record successfully added!", "Added!")
else
MessageBox.Show("Failure to add new record!", "Failure!")
End if
End Using
End Using
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
I have changed the name of your OleDbCommand object to avoid unnecessary confusion with the class SqlCommand used in SqlClient namespace (Not strictly necessary but nevertheless confusing when reading your code). Then I have used the OleDbCommand constructor that gets both the command text and the connection to be used by your command. This avoids to forget setting these essential properties, finally SECTION is a reserved keyword for MS-Access, thus, when used in query text, you need to encapsulate it between square brackets otherwise you get a SYNTAX ERROR

Data not updated correctly

i got 2 forms... Dress_Price is for displaying the data form database ( MS Access 2007 ) another form,Edit_Dress is to edit and update the database..
the code successfully updated the data based on the changes from the form Edit_Dress.. but there is 2 problems -
the dgvCustomerDressPrice did not refreshed after updating..
there is "space" being added to the record after updated..
before update price value
Dress_Name = "Tuxedo" Dress_Price = "150"
after update price value
Dress_Name = " Tuxedo" Dress_Price = " 250"
the "space" keeps being added up everytime i update the record... so the search function cant work properly because of the space..
code on Dress_Price :-
Private Sub Dress_Price_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\TMS Final\TMS Final\db\db_TMS.accdb"
con.Open()
dgvCustomerDressPrice()
End Sub
Private Sub dgvCustomerDressPrice()
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDb.OleDbDataAdapter
da = New OleDb.OleDbDataAdapter("SELECT * FROM tbl_dress", con)
da.Fill(dt)
dgvDressPrice.DataSource = dt.DefaultView
dgvDressPrice.SelectionMode = DataGridViewSelectionMode.FullRowSelect
con.Close()
End Sub
Private Sub btnEditDress_Click(sender As Object, e As EventArgs) Handles btnEditDress.Click
If dgvDressPrice.Rows.Count > 0 Then ' when user click a row, any query for database will based on Order_ID
If dgvDressPrice.SelectedRows.Count > 0 Then
Dim intDressID As Integer = dgvDressPrice.SelectedRows(0).Cells("Dress_ID").Value
Try
If Not con.State = ConnectionState.Open Then
con.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM tbl_dress WHERE Dress_ID =" & intDressID, con)
' the record that will be edited is based on the Customer_ID of particular row clicked
Dim dt As New DataTable
da.Fill(dt)
Edit_Dress.txtDressID.Text = intDressID
Edit_Dress.txtDressName.Text = dt.Rows(0).Item("Dress_Name")
Edit_Dress.txtDressPrice.Text = dt.Rows(0).Item("Dress_Price")
' pass the data from tbl_user into each represented field
Catch ex As Exception
MessageBox.Show("Failed to edit data ! System eror : " & ex.ToString, "Eror !", MessageBoxButtons.OK)
End Try
End If
End If
Edit_Dress.Show()
End Sub
Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
If Not con.State = ConnectionState.Open Then
con.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM tbl_dress WHERE Dress_Name like '" & txtSearch.Text & "%' ", con)
Dim dt As New DataTable
da.Fill(dt)
dgvCustomerDressPrice().DataSource = dt
con.Close()
End Sub
code on Edit_Dress :-
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
con = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\TMS Final\TMS Final\db\db_TMS.accdb")
Try
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim intDressID As Integer
intDressID = Convert.ToInt32(txtDressID.Text)
intDressID = Integer.Parse(txtDressID.Text)
Dim intDressPrice As Integer
intDressPrice = Convert.ToInt32(txtDressPrice.Text)
intDressPrice = Integer.Parse(txtDressPrice.Text)
Dim query As String = "UPDATE tbl_dress SET Dress_Name = ' " & txtDressName.Text & " ' , Dress_Price = ' " & intDressPrice & " ' WHERE Dress_ID = " & intDressID & " "
Dim cmd As New OleDb.OleDbCommand(query, con)
cmd.ExecuteNonQuery()
MessageBox.Show("Data updated !", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
Dress_Price.RefreshPriceList()
con.Close()
Me.Close()
Catch ex As Exception
MessageBox.Show("Failed to save into database ! System eror : " & ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Looking at your UPDATE method it is clear why you have a space added every time. You put it in the update string.
Dim query As String = "UPDATE tbl_dress SET Dress_Name = ' " & _
^ here
txtDressName.Text & " ' , Dress_Price = ' " & _
^here ^here
intDressPrice & " ' WHERE Dress_ID = " & intDressID & " "
^here
A part from the simple error that could be fixed removing the space, this is not the correct way to create an update command. You should use a parameterized query
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
con = New OleDb.OleDbConnection(.....)
Try
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim intDressID As Integer
intDressID = Convert.ToInt32(txtDressID.Text)
Dim intDressPrice As Integer
intDressPrice = Convert.ToInt32(txtDressPrice.Text)
Dim query As String = "UPDATE tbl_dress SET Dress_Name = ?, Dress_Price = ? " & _
"WHERE Dress_ID = ?"
Dim cmd As New OleDb.OleDbCommand(query, con)
cmd.Parameters.AddWithValue("#p1", txtDressName.Text)
cmd.Parameters.AddWithValue("#p2", intDressPrice)
cmd.Parameters.AddWithValue("#p3", intDressID)
cmd.ExecuteNonQuery()
.....

Bind Data From Microsoft Acess to Textbox

I have a database named PriceTesting ( using Microsoft Access 2007 ) that contains a table named tbl_dress with columns:
Dress_ID, Dress_Name, Dress_Price
In the form, I have created a combobox and a text box.
So far I've succeeded binding the combobox with Dress_Name data using this code :-
Private Sub FillCombo()
Try
Dim fillcon As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\DressTest\DressTest\db\PriceTesting.accdb")
Dim query As String = ("SELECT Dress_Name FROM tbl_dress")
Dim da As New OleDb.OleDbDataAdapter(query, fillcon)
Dim ds As New DataSet
da.Fill(ds)
ComboBox1.ValueMember = "Dress_Name"
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.SelectedIndex = 0
Catch ex As Exception
MsgBox("ERROR : " & ex.Message.ToString)
End Try
End Sub
and it works... everytime the form loads, the combobox contains the data from Dress_Name column.
Now I want the texbox.text = Dress_Price WHERE Dress_Name = combox.selecteditem
any idea how to do that?
what I have in mind is only this : -
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
query = " SELECT Dress_Price FROM tbl_dress WHERE Dress_Name = ' " & Combobox1.Text & " ' "
Textbox1.text = query
End Sub
TRY THIS and be sure to add Dress_Price"in your query
Private Sub FillCombo()
Try
Dim fillcon As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\DressTest\DressTest\db\PriceTesting.accdb")
Dim query As String = ("SELECT Dress_Name, Dress_Price FROM tbl_dress")
Dim da As New OleDb.OleDbDataAdapter(query, fillcon)
Dim ds As New DataSet
da.Fill(ds)
ComboBox1.ValueMember = "Dress_Name"
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.SelectedIndex = 0
texbox.DataBindings.Clear()
texbox.DataBindings.Add("Text", ds.Tables(0), "Dress_Price")
Catch ex As Exception
MsgBox("ERROR : " & ex.Message.ToString)
End Try
End Sub
OR just change the code in your idea like this and be sure to add Dress_Price"in your query
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
dim dt as DataTable = Combobox1.DataSource
dim dr as DataRow = dt.Rows(Combobox1.SelectedIndex)
Textbox1.text = iif(dr.isNull("Dress_Price"), "", dr.isNull("Dress_Price").Tostring)
End Sub
Dim fillcon As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\DressTest\DressTest\db\PriceTesting.accdb")
Dim query As String = " SELECT Dress_Price FROM tbl_dress WHERE Dress_Name = ' " & Combobox1.Text & "'"
Dim da As New OleDb.OleDbDataAdapter(query, fillcon)
Dim dt As New DataTable
da.Fill(dt)
Textbox1.text = dt.rows(0).item(0)
Search for parameters to avoid sql injection. Also try to improve the query and use the ID instead of the dress_name

How to simultaneously update an Access table and a DataGridView control?

I have a .NET application, where some of its methods updates Access database tables.
My problem is the DataGridView control that displays data, is not updating.
To do it, I have to restart the application, which is not something I desire.
Public Class frmUsers
Dim cnn3 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\AuditDB.mdb;")
Dim sql2 As String
Dim ds1 As New DataSet
Dim adptr As OleDbDataAdapter
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim Command1 As New OleDbCommand
Dim i2 As Integer
Dim sql1 As String
Dim Status As String
Try
Dim cnn3 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\AuditDB.mdb;")
cnn3.Open()
If ComboBox1.SelectedValue = "Admin" Then
Status = "Admin"
Else
Status = "Student"
End If
sql1 = "INSERT INTO Users ([ID],[PASSWORD],[LASTNAME],[FIRSTNAME],[LOGINTYPE]) VALUES('" & txtUID.Text & "','" & txtUPassword.Text & "','" & txtULastname.Text & "','" & txtUFirstName.Text & "','" & Status & "')"
Command1 = New OleDbCommand(sql1, cnn3)
i2 = Command1.ExecuteNonQuery
MessageBox.Show("Users Added Successfull")
Catch ex As Exception
End Try
End Sub
Private Sub btnback_Click(sender As Object, e As EventArgs) Handles btnback.Click
Me.Hide()
frmFaculty.Show()
End Sub
Private Sub frmUsers_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cnn4 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\AuditDB.mdb;")
sql2 = "Select * from Users"
Try
cnn4.Open()
adptr = New OleDbDataAdapter(sql2, cnn4)
adptr.Fill(ds1)
DataGridView1.DataSource = ds1.Tables(0)
Catch ex As Exception
End Try
cnn4.Close()
End Sub
End Class
You need to update it mannually.
Try this:
Dim cnn3 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\AuditDB.mdb;")
Dim sql2 As String
Dim ds1 As New DataSet
Dim adptr As OleDbDataAdapter
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim Command1 As New OleDbCommand
Dim i2 As Integer
Dim sql1 As String
Dim Status As String
Try
Dim cnn3 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\AuditDB.mdb;")
cnn3.Open()
If ComboBox1.SelectedValue = "Admin" Then
Status = "Admin"
Else
Status = "Student"
End If
sql1 = "INSERT INTO Users ([ID],[PASSWORD],[LASTNAME],[FIRSTNAME],[LOGINTYPE]) VALUES('" & txtUID.Text & "','" & txtUPassword.Text & "','" & txtULastname.Text & "','" & txtUFirstName.Text & "','" & Status & "')"
Command1 = New OleDbCommand(sql1, cnn3)
i2 = Command1.ExecuteNonQuery
MessageBox.Show("Users Added Successfull")
Refresh()
Catch ex As Exception
End Try
End Sub
Private Sub btnback_Click(sender As Object, e As EventArgs) Handles btnback.Click
Me.Hide()
frmFaculty.Show()
End Sub
Private Sub frmUsers_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Refresh()
End Sub
Private Sub Refresh()
Dim cnn4 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\AuditDB.mdb;")
sql2 = "Select * from Users"
Try
cnn4.Open()
adptr = New OleDbDataAdapter(sql2, cnn4)
adptr.Fill(ds1)
DataGridView1.DataSource = ds1.Tables(0)
Catch ex As Exception
End Try
cnn4.Close()
End Sub