Solved

Help with dataadapter.update

Posted on 2011-09-27
6
185 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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)

Question has a verified solution.

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

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 …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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 UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

863 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

19 Experts available now in Live!

Get 1:1 Help Now