Hey There, its Ruan. uhh, i need help, as i did indeed hit a brick wall.
at the moment, i can connect to a database through textboxes, ill put the code at the bottom.
What i want to do/know, is, How or What is the code or manner to read a specific piece of data from the database, lets say 3rd row, 2nd column. What is the code to do that.
And then i want to put this all in a datagridview, as i dont know the code to add all the data to the datagridview.
Thank you verry much for any help...
Ruan
Public Class Form2
Dim inc As Integer
Dim MaxRows As Integer
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = C:/BudgetBase.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT * FROM tblContacts"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "BudgetBase") //// i take it i have to add
that code i dont know here.
DataGridView1.Update()
con.Close()
MaxRows = ds.Tables("BudgetBase").Rows.Count
inc = -1
End Sub
Private Sub NavigateRecords()
txtBudget.Text = ds.Tables("BudgetBase").Rows(inc).Item(1)
txtExpense.Text = ds.Tables("BudgetBase").Rows(inc).Item(2)
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
If inc <> MaxRows - 1 Then
inc = inc + 1
NavigateRecords()
Else
MsgBox("No More Rows")
End If
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
If inc > 0 Then
inc = inc - 1
NavigateRecords()
Else
MsgBox("First Record")
End If
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
If inc <> 0 Then
inc = 0
NavigateRecords()
End If
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
If inc <> MaxRows - 1 Then
inc = MaxRows - 1
NavigateRecords()
End If
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
ds.Tables("BudgetBase").Rows(inc).Item(1) = txtBudget.Text
ds.Tables("BudgetBase").Rows(inc).Item(2) = txtExpense.Text
da.Update(ds, "BudgetBase")
MsgBox("Data updated")
End Sub
Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
btnCommit.Enabled = True
btnAddNew.Enabled = False
btnUpdate.Enabled = False
btnDelete.Enabled = False
txtBudget.Clear()
txtExpense.Clear()
End Sub
Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("BudgetBase").NewRow()
dsNewRow.Item("Expense") = txtExpense.Text
dsNewRow.Item("Budget") = txtBudget.Text
ds.Tables("BudgetBase").Rows.Add(dsNewRow)
da.Update(ds, "BudgetBase")
MsgBox("New Record added to the Database")
btnCommit.Enabled = False
btnAddNew.Enabled = True
btnUpdate.Enabled = True
btnDelete.Enabled = True
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If inc <> -1 Then
If MessageBox.Show("Do you really want to Delete this Record?", _
"Delete", MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
End If
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("BudgetBase").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
NavigateRecords()
da.Update(ds, "BudgetBase")
Else
MsgBox("Cannot Delete Nothing")
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
btnCommit.Enabled = False
btnAddNew.Enabled = True
btnUpdate.Enabled = True
btnDelete.Enabled = True
inc = 0
End Sub
End Class