Solved

Insert Row into sql dbo from Dataset using Data Adapter

Posted on 2006-11-21
3
300 Views
Last Modified: 2010-04-23

Hello,

I'm attempting to insert a row into an sql dbo.

I receive the following error at the asterik point below. "Update requires a valid InsertCommand when passed DataRow collection with new rows."

Can you show me the nature of the insertCommand or whatever code is necessary to add the row to the original database?

' Test Insert
                    Dim sTestSelect As String = "Select * From Test"
                    Dim daTest As New SqlDataAdapter(sTestSelect, conRyan)
                    Dim dsTest As New DataSet()
                    daTest.Fill(dsTest, "Test")

                    Dim wsPol As String = "CWO8888888"
                    Dim wsLob As String = "0001"
                    Dim wsAgent As String = "0001"

                    Dim drTest As DataRow = dsTest.Tables("Test").NewRow
                    drTest("Policy") = wsPol
                    drTest("Lob") = wsLob
                    drTest("Agent") = wsAgent
                    dsTest.Tables("Test").Rows.Add(drTest)
                    *** daTest.Update(dsTest, "Test")

Ryan

0
Comment
Question by:888_ryan_888
3 Comments
 
LVL 15

Accepted Solution

by:
Solar_Flare earned 50 total points
ID: 17992602
what you need to do is to set the command of the dataadapter


 Dim sTestSelect As String = "Select * From Test"
                    Dim daTest As New SqlDataAdapter(sTestSelect, conRyan)
                    Dim dsTest As New DataSet()
                    daTest.Fill(dsTest, "Test")

                    Dim wsPol As String = "CWO8888888"
                    Dim wsLob As String = "0001"
                    Dim wsAgent As String = "0001"

                    Dim drTest As DataRow = dsTest.Tables("Test").NewRow
                    drTest("Policy") = wsPol
                    drTest("Lob") = wsLob
                    drTest("Agent") = wsAgent
                    dsTest.Tables("Test").Rows.Add(drTest)
daTest.InsertCommand = new sqlcommandbuilder
                    *** daTest.Update(dsTest, "Test")
0
 

Author Comment

by:888_ryan_888
ID: 17993068
                   
Nice thanks, That worked fine.

I'm brain fried today.

Here is the code that I ended up with.

Dim wsPol As String = "CWO8888888"
                    Dim wsLob As String = "0001"
                    Dim wsAgent As String = "0001"

                    Dim sTestSelect As String = "Select * From Test"
                    Dim sTestUpdate As String = "Insert Into Test (policy, lob, agent) Values ('" + wsPol + "','" + wsLob + "','" + wsAgent + "')"
                    Dim cmdTestUpdate As New SqlCommand(sTestUpdate, conRyan)
                    Dim daTest As New SqlDataAdapter(sTestSelect, conRyan)
                    Dim dsTest As New DataSet()
                    daTest.Fill(dsTest, "Test")

                   

                    Dim drTest As DataRow = dsTest.Tables("Test").NewRow
                    drTest("Policy") = wsPol
                    drTest("Lob") = wsLob
                    drTest("Agent") = wsAgent
                    dsTest.Tables("Test").Rows.Add(drTest)
                    Dim cbTest As New SqlCommandBuilder(daTest)
                    daTest.InsertCommand = cmdTestUpdate
                    daTest.Update(dsTest, "Test")
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17993069
solarflare,
i think per recommendation of MSDN, (http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.aspx)

this line you put in his code
daTest.InsertCommand = new sqlcommandbuilder
should read
Dim builder As SqlCommandBuilder = New SqlCommandBuilder(daTest)
builder.GetUpdateCommand()
daTest.Update(dsTest, "Test")

NY
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

16 Experts available now in Live!

Get 1:1 Help Now