Solved

Insert record usin SqlDataAdapter

Posted on 2008-10-29
4
568 Views
Last Modified: 2012-05-05
Hello, im trying to insert a record via stored procedure using SqlDataAdapter but it is not working.

Dim cmd As SqlCommand
        Dim da As SqlDataAdapter
        Dim cn As SqlConnection

        Dim cnStr As String = ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString.ToString()

        cn = New SqlConnection(cnStr)

        cmd = New SqlCommand("spInsertCustomer", cn)
        cmd.CommandType = CommandType.StoredProcedure

        da = New SqlDataAdapter(cmd)

        Dim param As SqlParameter

        param = cmd.Parameters.Add("@CName", SqlDbType.NVarChar, 50, "CName")
        param.SourceVersion = DataRowVersion.Original
        param.Direction = ParameterDirection.Input

        param = cmd.Parameters.Add("@CPhone", SqlDbType.NVarChar, 13, "CPhone")
        param.SourceVersion = DataRowVersion.Original
        param.Direction = ParameterDirection.Input

        da.InsertCommand = cmd
        da.Update(ds.Customers)
0
Comment
Question by:arcross
[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
  • 2
  • 2
4 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 22830385
What error do you get ?

Using Stored Procedures in Conjuction with DataAdapter
http://www.c-sharpcorner.com/UploadFile/dclark/UseSPwithDP11282005035417AM/UseSPwithDP.aspx
0
 
LVL 8

Author Comment

by:arcross
ID: 22831304
i dont get any errors
it is just not inserting the record.

If I add the the record to the datable first it will work. Like:

   ................
        param = cmd.Parameters.Add("@CPhone", SqlDbType.NVarChar, 13, "CPhone")
        param.SourceVersion = DataRowVersion.Original
        param.Direction = ParameterDirection.Input

       ' --HERE ADD RECORD TO THE DATATABLE.
        ds.Customers.Row.Add(ID_HERE,"NAME","PHONE")
        '------- ADDING THE LINE ABOVE IT WORKS. THE RECORD IS INSERTED INTO THE DATABASE      
         da.InsertCommand = cmd
        da.Update(ds.Customers)

So, is that how it should work? do i have to add it first to the datatble??

0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 22831322
Yes, because the datatable will be used through the dataadapter to see which rows are added, deleted, changed, ...
0
 
LVL 8

Author Comment

by:arcross
ID: 22850164
ok, I understand !
thank you!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - SQL Timeout 9 51
Error message when using PrintSocket.Send 7 28
Need syntax help building hyperlink 2 41
VB.Net Data Class 1 23
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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