Solved

Writing content of datatable to the database

Posted on 2011-09-23
2
151 Views
Last Modified: 2012-05-12
I am trying to write the content of a data table back to a SQL Server table.  It writes the first row, but on the second row, I get following error:

"Procedure or Function InsertHospitalInvoice has too many arguments specified."

I have reviewed everything and I cannot see why I am getting this error.  Below is my code to call the stored procedure InsertHospitalInvoice.  I've also provided a copy of the table design as well the Stored Procedure InsertHospitalInvoice in the attached word document.  

Can anyone see where my bug is.  Your help is greatly appreciated,  Thank you stored-procedure-and-Table-desig.doc
Sub InsHospInvData()
        Dim cmdData As New SqlCommand()

        Try
            conn.Open()
            cmdData.Connection = conn
            For Each drow In dtHospInvData.Rows
                cmdData.CommandText = "InsertHospitalInvoice"
                cmdData.CommandType = CommandType.StoredProcedure
                cmdData.Parameters.AddWithValue("@HID", drow.Item("HID"))
                cmdData.Parameters.AddWithValue("@DeptCode", drow.Item("DeptCode"))
                cmdData.Parameters.AddWithValue("@BillDate", drow.item("BillDate"))
                cmdData.Parameters.AddWithValue("@InTests", drow.item("InTests"))
                cmdData.Parameters.AddWithValue("@InAmount", drow.item("InAmount"))
                cmdData.Parameters.AddWithValue("@OutTests", drow.item("OutTests"))
                cmdData.Parameters.AddWithValue("@OutAmount", drow.item("OutAmount"))
                cmdData.Parameters.AddWithValue("@OtherTests", drow.item("OtherTests"))
                cmdData.Parameters.AddWithValue("@OtherAmount", drow.item("OtherAmount"))
                cmdData.Parameters.AddWithValue("@SvDesc", drow.Item("SvDesc"))
                cmdData.ExecuteNonQuery()
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn.Close()
        End Try
    End Sub

Open in new window

0
Comment
Question by:kshumway
2 Comments
 
LVL 15

Accepted Solution

by:
x77 earned 500 total points
ID: 36588062
For first row, the cmdData has 10 parameters- Ok
For second row, you add aditional parameters.

Use cmdData.Parameters.Clear after cmdData.ExecuteNonQuery()
0
 

Author Closing Comment

by:kshumway
ID: 36588091
You are a lifesaver.  Thank you so much.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

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…
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.
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

17 Experts available now in Live!

Get 1:1 Help Now