Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Insert record usin SqlDataAdapter

Posted on 2008-10-29
4
Medium Priority
?
572 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

718 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