Solved

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

Posted on 2004-09-21
8
156 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

635 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