Solved

Insert Row into sql dbo from Dataset using Data Adapter

Posted on 2006-11-21
3
299 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
Comment Utility
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
Comment Utility
                   
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

5 Experts available now in Live!

Get 1:1 Help Now