Solved

Insert Row into sql dbo from Dataset using Data Adapter

Posted on 2006-11-21
3
303 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
[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
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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