Solved

Help with dataadapter.update

Posted on 2011-09-27
6
186 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

825 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