Solved

Insert record usin SqlDataAdapter

Posted on 2008-10-29
4
562 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

12 Experts available now in Live!

Get 1:1 Help Now