Solved

Help with dataadapter.update

Posted on 2011-09-27
6
187 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

733 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