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

Recommended Answers

All 8 Replies

You have to add following two line of code for displaying data in datagridview

DataGridView1.DataSource = <name_Of_Data_set>
DataGridView1.DataMember = "<Name_Of_Table>"

For selecting 2nd column you have to change you
this

sql = "SELECT * FROM tblContacts"

to

sql = "SELECT <column_name1>,<column_name2> FROM tblContacts"

it display column_name1 and column_name2

for selecting particular row you have to define condition some thing like this

sql = "SELECT <column_name1>,<column_name2> FROM tblContacts where <column_name1>='<data>'"

if datatype of column_name1 is string then only put '<data>' otherwise <data> dont use '
where data is present in your record.

for example

sql = "SELECT emp_id,emp_name FROM emp where emp_id=101"

I hope it will help you.

Best Of Luck.

Thank you very much Praveen. I tried getting it to work today, but no luck yet, gona try again tomorrow. will let you know if i succeed =)

Thank you once again.

Ruan

Post your modified code of form2_load event so that some one help whats your problem

Thank you. ahhh, just cant get this right...
i can display my database in the datagridview by means of the wizard, so then i dont create a class.
So now if i want to read a specific row or add a specific columns totals I dont know the code to do that. coz i didnt create a dataset or ANY of my original code.

ive seen that i have to use the statements But I Canttt as i Am A IDIOT. There are billions of Code snippets on the internet on how to do something, but i have no idea how it fits together. I am thinking of killing myself.


coz this is my Problem.
1) i Can connect the database by means of the wissard. But then i cant choose
individual columns as i dont know how, or where i add the mysterious code.
2) If i add that code,then it wont work, because i take it i have to create a class to connect to the database and not just use the wissard.

Ive really given up on my project... I have no idea... I am sitting here 14hours on the internet, not getting anything right...

please help me... please..

No wait... i think i have something. Will post my Hopefully working code soon!

I think it's better to use dataset :)

I don't use wizard, because wizard not give you flexibility as code gives

Dim cnn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Public ds As New DataSet()
    Dim da As New OleDbDataAdapter()
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\BudgetBase.mdb;"                                    
        cnn.Open()
        cmd.CommandText = "SELECT emp_id,emp_name FROM emp"
        cmd.Connection = cnn
        da.SelectCommand = cmd
        da.Fill(ds, "emp")
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "emp"
cnn.close()

This code display all row from emp table and two column "emp_id" and "emp_name"

I hope it will help you

Best Of Luck

Ausm info guys, I actually modified my code with your code, and made the data-grid more flexible.
thanks alot for all of your help!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.