Datagridview and Database

Posted by Ruchi22 under VB.NET on 10/7/2011 | Points: 10 | Views : 2314 | Status : [Member] | Replies : 3
Hey.. I have a form consisting of datagridview and two buttons of update and load all.. I have used fillby method to obtain data from database. The query seems to be working fine, except that the data is not shown in the gridview.
As in, for the qeury, if the returned rows are 2, it will show 3 rows in the gridview, but all empty.
Here is my code.. Please help me !!



Imports System.Data
Imports System.Data.OleDb
Imports System.EventArgs
Imports System.Data.OleDb.OleDbConnection
Imports System.Data.OleDb.OleDbCommand

Public Class Form1
Inherits System.Windows.Forms.Form

Dim wrkdir As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location())
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Dim bs As New BindingSource
Dim edit As Boolean
'Dim cnn As OleDbConnection


Dim cnn As New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")

'cnn = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
'cnn.Open()

Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

If edit Then
da.Update(ds, "partno")
edit = False
End If

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'HemDatabase1DataSet.partno' table. You can move, or remove it, as needed.
Me.PartnoTableAdapter.Fill(Me.HemDatabase1DataSet.partno)

dgv1.DataSource = bs

'Dim cnn As New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
'cnn = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
'cnn.Open()

End Sub

Private Sub button2_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click

ds.Tables.Clear()

If tsText.Text <> "" Then

Dim sql As String = "SELECT partno.partnum, partno.partname, partno.partdesc, partno.partqty " & _
"FROM(partno)" & _
"WHERE (((partno.type)='" & tsText.Text & "'));"

Dim cmd As New OleDbCommand(sql, cnn)

da.SelectCommand = cmd

Dim cmdBuilder As New OleDbCommandBuilder(da)

da.Fill(ds, "partno")
bs.DataSource = ds.Tables(0)

Else

Exit Sub

End If
End Sub

Private Sub button3_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles button3.Click

ds.Tables.Clear()

Dim sql As String = "SELECT * FROM partno;"
Dim cmd As New OleDbCommand(sql, cnn)

da.SelectCommand = cmd

Dim cmdbuilder As New OleDbCommandBuilder(da)
da.Fill(ds, "partno")
bs.DataSource = ds.Tables(0)

End Sub

Private Sub FillByToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
Try
Me.PartnoTableAdapter.FillBy(Me.HemDatabase1DataSet.partno, tsText.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try

End Sub
End Class






Responses

Posted by: Blessyjees on: 10/7/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Pleasae try with
bs.Databind() after every ds.Datasource=ds.Tables(0)


Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Ruchi22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ruchi22 on: 10/7/2011 [Member] Starter | Points: 25

Up
0
Down
hello !!

b.net is not allowing that.. :(

Ruchi22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ruchi22 on: 10/7/2011 [Member] Starter | Points: 25

Up
0
Down
now that i have got through it.. just changed the line

dgv1.DataSource = bs

to

dgv1.DataSource = Me.HemDatabase1DataSet.partno

But facing another issue..

Now another issue due to this.. My update and load buttons are not serving their purposes.. update is for refreshing data in grid database.. and load all is to load data after refreshing, on query basis..
My code is as below.. Please help !!

For update :


Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click


If edit Then
da.Update(ds, "partno")
edit = False
End If

End Sub


For Load all :


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


ds.Tables.Clear()

If TypeToolStripTextBox.Text <> "" Then

Dim sql As String = "SELECT partno.partnum, partno.partname, partno.partdesc, partno.partqty " & _
"FROM(partno)" & _
"WHERE (((partno.type)='" & TypeToolStripTextBox.Text & "'));"

Dim cmd As New OleDbCommand(sql, cnn)

da.SelectCommand = cmd

Dim cmdBuilder As New OleDbCommandBuilder(da)

da.Fill(ds, "partno")
bs.DataSource = ds.Tables(0)

Else
Exit Sub

End If

End Sub

Ruchi22, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response