Solved

Insert record usin SqlDataAdapter

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

911 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

21 Experts available now in Live!

Get 1:1 Help Now