?
Solved

Help with dataadapter.update

Posted on 2011-09-27
6
Medium Priority
?
196 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
[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
  • 4
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 2000 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

752 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