Solved

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

Posted on 2004-09-21
8
153 Views
Last Modified: 2010-04-23
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
Comment
Question by:kate_y
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 4

Expert Comment

by:gdexter
ID: 12114731
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
 

Author Comment

by:kate_y
ID: 12115478
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
 
LVL 4

Expert Comment

by:gdexter
ID: 12115643
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:kate_y
ID: 12116426
Sorry, but is this for load button or update button? tks.
0
 
LVL 4

Expert Comment

by:gdexter
ID: 12116681
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
 
LVL 4

Expert Comment

by:gdexter
ID: 12116764
Use that is the Load Button routine
0
 

Author Comment

by:kate_y
ID: 12143780
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
 
LVL 4

Accepted Solution

by:
gdexter earned 500 total points
ID: 12144120
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question