Solved

Insert record usin SqlDataAdapter

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
More on Time zones in vb 2010 12 38
vb.net - How to check if current user is an administrator? 6 47
VB.Net How to Exit Sub - Exit Form??? 5 57
cannot connect to sqlserver 8 28
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

770 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