Datagridview and databse

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 3010 Views - Last Post: 08 October 2011 - 02:59 AM Rate Topic: -----

#16 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Datagridview and databse

Posted 07 October 2011 - 04:44 AM

So now only update is not working? Have you tested my code for update which I posted.?
Was This Post Helpful? 0
  • +
  • -

#17 Ruchi22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 23-September 11

Re: Datagridview and databse

Posted 07 October 2011 - 06:12 AM

Yes.. I have tested the code u posted.. I had mentioned it in the last snippet i provided..

Now, the scenario is :

Update is not functioning..
FillBy tool strip is showing all the data, instead of the filtered data. It shows all the data in updated form ie. after updating newly added data as well, dont know how !!
Load All button is showing some filtered data instead of showing all data in database..

I m finally attaching current code for ur reference..


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

        'Dim change As DataSet = ds.GetChanges()
        'Dim commandbuild As New OleDbCommandBuilder(da)

        'If change IsNot Nothing Then
        '    da.Update(change)
        'End If

        dgv1.DataSource = bs
        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 = Me.HemDatabase1DataSet.partno
        bs.DataSource = ds.Tables(0)

        'dgv1.DataMember = "partno"

        '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

        dgv1.DataSource = Me.HemDatabase1DataSet.partno
        ds.Tables.Clear()

        If tsText.Text <> "" Then

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

            Dim cmd As New OleDbCommand(sql, cnn)
            da = New OleDbDataAdapter(cmd)

            '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




Where :

update : button1
fillbytoolstrip : button2
loadall : button3

Thank u :)
Was This Post Helpful? 0
  • +
  • -

#18 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Datagridview and databse

Posted 07 October 2011 - 08:02 AM

You have two sub that handles button3 click event, that should cause the problem
   Private Sub button3_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles button3.Click

'and
Private Sub FillByToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
you confuse here and cause the problem
Was This Post Helpful? 1
  • +
  • -

#19 Ruchi22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 23-September 11

Re: Datagridview and databse

Posted 07 October 2011 - 10:41 PM

Hello.. I merged the two codes.. Its somehow working a bit better. Though not in one go, but manageable.. The only main problem now is that fillby or load all options give data filtered by type, but they do not include the newly updated data !! Here is the code for fillby:


 Try
            Me.PartnoTableAdapter.FillBy(Me.HemDatabase1DataSet.partno, tsText.Text)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

        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)



Thank you so much for helping me till now !! For update is :


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

        'Dim change As DataSet = ds.GetChanges()
        'Dim commandbuild As New OleDbCommandBuilder(da)

        'If change IsNot Nothing Then
        '    da.Update(change)
        'End If

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

    End Sub



Was This Post Helpful? 0
  • +
  • -

#20 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Datagridview and databse

Posted 07 October 2011 - 11:03 PM

According to your requirements here:

Quote

update : button1
fillbytoolstrip : button2
loadall : button3
the fillby is done in button2, so that code to fill by should not be there, instead in button3 we will have only this part
       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)
Or I get it wrong? Is the update one working?

This post has been edited by smohd: 07 October 2011 - 11:05 PM

Was This Post Helpful? 1
  • +
  • -

#21 Ruchi22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 23-September 11

Re: Datagridview and databse

Posted 07 October 2011 - 11:10 PM

hey !!

Loadall is button2
Fillby is button3

No... update is not working !!
Thanks..

This post has been edited by Ruchi22: 07 October 2011 - 11:34 PM

Was This Post Helpful? 0
  • +
  • -

#22 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Datagridview and databse

Posted 07 October 2011 - 11:20 PM

Now you confuse your self(look at post 17)
So if button2 is to load by then the code which I removed should be in button3 and those posted above in button2.
Then post our updated code and put comment in each sub saying what that sub should do and what is doing now

This post has been edited by smohd: 07 October 2011 - 11:25 PM

Was This Post Helpful? 1
  • +
  • -

#23 Ruchi22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 23-September 11

Re: Datagridview and databse

Posted 07 October 2011 - 11:33 PM

This is for update process.. It sohuld update the databse with newly added values but not working..


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

        'Dim change As DataSet = ds.GetChanges()
        'Dim commandbuild As New OleDbCommandBuilder(da)

        'If change IsNot Nothing Then
        '    da.Update(change)
        'End If

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

    End Sub




Next is for loadall.. It should load all the data in database, even the updated ones.. Usually, a user may need to view all data after he/she is done with filtered data. It is for that.. However, this button shows only filtered data by type, that too excluding the updated/newly added data.



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

        Me.PartnoTableAdapter.Fill(Me.HemDatabase1DataSet.partno)
        dgv1.DataSource = Me.HemDatabase1DataSet.partno
        ds.Tables.Clear()

        If tsText.Text <> "" Then

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

            Dim cmd As New OleDbCommand(sql, cnn)
            da = New OleDbDataAdapter(cmd)

            '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




Final is the toolstripbutton.. Used to display data that are filtered by type. This does show data filtered by type but does not include the update/newly added data.


Private Sub button3_Click(ByVal sender As System.Object, ByVal e As 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

        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



Was This Post Helpful? 0
  • +
  • -

#24 Ruchi22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 23-September 11

Re: Datagridview and databse

Posted 07 October 2011 - 11:45 PM

I hope this information is suffice !!

Sorry for the mess up in post 17..

This post has been edited by Ruchi22: 07 October 2011 - 11:47 PM

Was This Post Helpful? 0
  • +
  • -

#25 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Datagridview and databse

Posted 08 October 2011 - 12:10 AM

Now I start to see your problem. Since you say button2 is for load all, then the code in button2 is for button3 and those in button3 is for button2.
By starting, your code should be like this for button2( you missed the last lline which tells the data grid view where to take data now)
    Private Sub button2_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.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)
        dgv1.DataSource = bs 'this line was missing
    End Sub

Ok try it and I am looking for others
Was This Post Helpful? 1
  • +
  • -

#26 Ruchi22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 23-September 11

Re: Datagridview and databse

Posted 08 October 2011 - 12:37 AM

Button 2 worked !!

Thank you.. Awaiting your reply for other two !!

Thank you so much, again !! :)
Was This Post Helpful? 0
  • +
  • -

#27 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Datagridview and databse

Posted 08 October 2011 - 12:53 AM

The same thing which you missed in button2 is the one which you miss in button3, telling the data grid view about the the new coming data. So at the end button3 click you will have this:
        ds.Tables.Clear()
        If tsText.Text <> "" Then
            Dim sql As String = "SELECT * FROM (partno) WHERE type='" & tsText.Text & "';"
            Dim cmd As New OleDbCommand(sql, cnn)
            da = New OleDbDataAdapter(cmd)

            da.Fill(ds, "partno")
            bs.DataSource = ds.Tables(0)
            dgv1.DataSource = bs
Glad this will work too, test it and give back feeds if working or not.
Was This Post Helpful? 1
  • +
  • -

#28 Ruchi22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 23-September 11

Re: Datagridview and databse

Posted 08 October 2011 - 01:10 AM

Heya !!

This button also worked.. Now finally the last one.. update remains..

Thank you so much for your help !!
Was This Post Helpful? 0
  • +
  • -

#29 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Datagridview and databse

Posted 08 October 2011 - 01:14 AM

And for update, I will use the style which I gave you before, just forgetting to initialize the data adapter, when putting the initialization, it should work, test this code for your update code and hope they will work, just give us update to know if working or still have problems:
        Dim change As DataSet = HemDatabase1DataSet.GetChanges()
        Dim commandbuild As New OleDbCommandBuilder(da)
        da.SelectCommand = New OleDbCommand("SELECT * FROM partno", cnn) 'this line was missing

        If change IsNot Nothing Then
            da.Update(change, "partno")
        End If

This post has been edited by smohd: 08 October 2011 - 01:15 AM

Was This Post Helpful? 0
  • +
  • -

#30 Ruchi22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 23-September 11

Re: Datagridview and databse

Posted 08 October 2011 - 01:19 AM

Update is not working !! :online2long:
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3