Solved

Help with dataadapter.update

Posted on 2011-09-27
6
184 Views
Last Modified: 2012-05-12
In a VB.Net Windows application, I have a form with a datagridview on which the user can add, update and delete rows.  When the user has completed all entries, I would like to call the dataadapter.update command to write the information to the table in the SQL Server database.  But nothing is being written to the database.  I've mever worked with the DataGridView before, so this is new to me.  I'm missing something.  Attached are segments of code where the connection to the database is made and the data table is filled as well as where I attempt to use the cmdbuilder to insert, update and delete rows in the database.  Can someone help?  It would be greatly appreciated!
'Code to create the data adapter, data table and the cmdbuilder

        Try
            conn.open
            daHospInvData = New SqlClient.SqlDataAdapter("GetInvoiceData", conn)
            dtHospInvData.Clear()
            daHospInvData.SelectCommand.CommandType = CommandType.StoredProcedure
            cmdBuilder = New SqlCommandBuilder(daHospInvData)
            daHospInvData.Fill(dtHospInvData)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn.Close()
        End Try
        DataGridView1.DataSource = dtHospInvData

'Add/Update/Delete information on the datagridview

'Code to save the information to the database

cmdBuilder.GetInsertCommand()
daHospInvData.Update(dtHospInvData)
cmdBuilder.GetUpdateCommand()
daHospInvData.Update(dtHospInvData)
cmdBuilder.GetDeleteCommand()
daHospInvData.Update(dtHospInvData)

Open in new window

0
Comment
Question by:kshumway
  • 4
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 500 total points
ID: 36709052
can the command builder build update commands for data taken from a stored procedure?

also I always did

daHospInvData.InsertCommand = cmdBuilder.GetInsertCommand()
.

etc, for each of the 3 commands first before the .update, and also there is something about

daHospInvData.FillSchema(dtHospInvData,SchemaType.Source,"isYourTable")

just before the .fill

andy you might need to go to in-line SQL


0
 

Author Comment

by:kshumway
ID: 36709174
Thank you so much for your response.

The stored procedue is simply "SELECT * FROM [Hospital Invoice Data] ORDER BY BillDate;"  Would that cause a problem?  If so, what should I change it to?

I changed the cmdbuilder statements as you recommended, but the statement " daHospInvData.FillSchema(dtHospInvData,SchemaType.Source,"isYourTable") " is giving an error because it is looking for a dataset, not a datatable as the first parameter.  So I did not use that.

When I ran the program with the cmdbuilder statements updated, it still did not save anything to the database.  

0
 

Author Comment

by:kshumway
ID: 36709241
Stop the presses - it is working.  Not sure, but whenI tried it again, it's working fine.  Thank you so much for your help!!!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Closing Comment

by:kshumway
ID: 36709252
Could not use the FillSchema statement because I'm not using a dataset.  But the other recommendations fixed the problem.  thank you sooooo much
0
 
LVL 18

Expert Comment

by:deighton
ID: 36709765
you are welcome, actually that has taught me something I think, the command builder must be able to go into the stored proc somehow.
0
 

Author Comment

by:kshumway
ID: 36709775
Yes it works fine using the stored procedure.  Thanks again!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now