Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

Problems for Updating database through Datagrid update for Win App. ......urgent, please help!

Hi, all,

I only found one way to make the update successful that is having the primary key field showing in the datagrid and using VB.net's build-in function to make data adapter, dataset and connection. This way I can use the method: OleDbDataAdapter1.Update(Ds1) to do all the insert, delete and update.

However, the records I need to display in the datagrid have to be based on a variable passed from the other form, say strScenarioName. So I have to mannuly write the SQL select statement cause the dataadapter wizard wouldn't recognized the variable. But using this way, the Da.Update(Ds) method won't work anymore. Do you think i need to hand write Update statement? Here is the code for loading and updating the datagrid.  Thanks

    Private da As OleDb.OleDbDataAdapter  'define dataAdapter's name. dataAdapter copies data from db to dataset
    Private ds As DataSet 'define dataset name

    Private Sub Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            da.Update(ds, "Vendor")
            MsgBox("Succeeded!")
        Catch ex As Exception
            MsgBox("nothing")
        End Try

    End Sub
    Private Const strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data    
                                                         Source=C:\SCOPT\GPLogiModData-0815.mdb;"

    Private Sub Load1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Load1.Click
        Dim strSQL As String = "SELECT * FROM TM_Vendor WHERE TM_Vendor.ScenarioName = '" & x & "'"

        da = New OleDb.OleDbDataAdapter(strSQL, strConn)  
        ds = New DataSet  

        ds.Clear()
        da.Fill(ds, "Vendor")

        DataGrid1.DataSource = ds.Tables("Vendor")
        DataGrid1.SetDataBinding(ds, "Vendor")
    End Sub
0
kate_y
Asked:
kate_y
  • 5
  • 3
1 Solution
 
gdexterCommented:
If you do not have a huge amount of data in the table you could use a Dataview to accomplish this.

'Class scope
Dim myDv As New System.Data.DataView


Dim strSQL As String = "SELECT * FROM TM_Vendor"

da = New OleDb.OleDbDataAdapter(strSQL, strConn)  
ds = New DataSet  

ds.Clear()
da.Fill(ds, "Vendor")

 Me.myDv.Table = ds.Tables("Vendor")
 Me.myDv.RowFilter =  String.Format("ScenarioName ={0}", x)
 Me.DataGrid1.DataSource = Me.myDv


You could also manually modify the Update and Insert Command Builder objects

In addtion you may need a CurrencyManager object for the DataView
0
 
kate_yAuthor Commented:
Hmm, actually the datagrid display part worked fine when manually make the SELECT statement like the follows.
Private Sub Load1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Load1.Click
        Dim strSQL As String = "SELECT * FROM TM_Vendor WHERE TM_Vendor.ScenarioName = '" & x & "'"
        da = New OleDb.OleDbDataAdapter(strSQL, strConn)  
        ds = New DataSet  
        ds.Clear()
        da.Fill(ds, "Vendor")
        DataGrid1.DataSource = ds.Tables("Vendor")
        DataGrid1.SetDataBinding(ds, "Vendor")
    End Sub

This update part didn't work.
Private Sub Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            da.Update(ds, "Vendor")
            MsgBox("Succeeded!")
        Catch ex As Exception
            MsgBox("nothing")
        End Try

Do I have to create a update statement and assign it to da? I know the syntax for the regular update statement but how to assign value to the fields from datagrid?
0
 
gdexterCommented:
I think this what you need

Dim cmdBuilder as New OleDbCommandBuilder


                 da.FillSchema(ds, SchemaType.Source, "Vendor")
                 da.Fill(ds, "Vendor")

                'configure the command builder

                cmdBuilder.DataAdapter = da
                cmdBuilder.QuotePrefix = "["
                cmdBuilder.QuoteSuffix = "]"

                da.UpdateCommand = cmdBuilder.GetUpdateCommand
                da.DeleteCommand = cmdBuilder.GetDeleteCommand
                da.InsertCommand = cmdBuilder.GetInsertCommand

                DataGrid1.DataSource = ds.Tables("Vendor")
                DataGrid1.SetDataBinding(ds, "Vendor")



0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
kate_yAuthor Commented:
Sorry, but is this for load button or update button? tks.
0
 
gdexterCommented:
You still need to give the Adapter a reference to the CmdBuilder to do an update it has nothing to do with the event it fires on.
0
 
gdexterCommented:
Use that is the Load Button routine
0
 
kate_yAuthor Commented:
Sorry for the late response. So do I have to write a Update statement as the reference to the CmdBuilder? If so, I have problem with the Update statement's syntax. Could you give me a example on how to set the value of the datagrid to the table fields? BTW, I know the basic syntax (UPDATE table SET column = something).

thanks
0
 
gdexterCommented:
You should be able to use the Update statement that is auto-generated by the CmdBuilder object.

You may want to remove the DataAdapter that you created with the wizard and just create a new instance of the Adapter in code. Then use the CmdBuilder code that I posted before and see if the update will work.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now