I am new vb.net and I m working on project that will import excel file in datagrid. After import this file I need to save these file into SQL Server 2005 table. But I am receiving 'Procedure or function insertCashBook has too many arguments specified'.

Private Sub ButtonReconsilation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReconsilation.Click

        Dim connection As SqlConnection = database_connection()
        connection.Open()

        Dim count, I As Integer

        I = 0
        Try
            Dim command As SqlCommand = New SqlCommand("insertCashBook", connection)
            command.CommandType = CommandType.StoredProcedure

            count = LoadCashBookDataSet.Tables(0).Rows.Count
            While I < count
                If NZ(LoadCashBookDataSet.Tables(0).Rows(I)(4)) = 0 Then
                    command.Parameters.AddWithValue("@DR", NZ(LoadCashBookDataSet.Tables(0).Rows(I)(4)))
                    command.Parameters.AddWithValue("@CR", Convert.ToDecimal(LoadCashBookDataSet.Tables(0).Rows(I)(5)))

                ElseIf NZ(LoadCashBookDataSet.Tables(0).Rows(I)(5)) = 0 Then
                    command.Parameters.AddWithValue("@CR", NZ(LoadCashBookDataSet.Tables(0).Rows(I)(4)))
                    command.Parameters.AddWithValue("@DR", Convert.ToDecimal(LoadCashBookDataSet.Tables(0).Rows(I)(4)))

                Else
                    command.Parameters.AddWithValue("@DR", Convert.ToDecimal(LoadCashBookDataSet.Tables(0).Rows(I)(4)))
                    command.Parameters.AddWithValue("@CR", Convert.ToDecimal(LoadCashBookDataSet.Tables(0).Rows(I)(5)))
                End If

                I = I + 1
            End While

            command.ExecuteNonQuery()           'Receive Error while execute this statement 
        Catch ex As Exception
            MsgBox(Err.Description)
        End Try

    End Sub

You must have 2 parameters declared in your Stored Procedure, called @DR and @CR, both of the type Decimal.
While looping through your rows in your LoadCashBookDataSet.Tables(0) you have to execute the command based on the stored procedure & active connection. So, each time you have passed a row, you have to execute the command in order to update your databasetable.

This means you have to try something like this: (the code should be cleaned up & added with cleanup code for your unmanaged objects like the connection and command)

Private Sub ButtonReconsilation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReconsilation.Click

Dim connection As SqlConnection = database_connection()
Try
    connection.Open()
Catch ex as exception
   msgbox ex.message
End Try

Dim count as integer = 0
dim I As Integer = 0
Dim command As SqlCommand = nothing

Try

count = LoadCashBookDataSet.Tables(0).Rows.Count
While I < count
command= New SqlCommand("insertCashBook", connection)
command.CommandType = CommandType.StoredProcedure


If NZ(LoadCashBookDataSet.Tables(0).Rows(I)(4)) = 0 Then
command.Parameters.AddWithValue("@DR", NZ(LoadCashBookDataSet.Tables(0).Rows(I)(4)))
command.Parameters.AddWithValue("@CR", Convert.ToDecimal(LoadCashBookDataSet.Tables(0).Rows(I)(5)))

ElseIf NZ(LoadCashBookDataSet.Tables(0).Rows(I)(5)) = 0 Then
command.Parameters.AddWithValue("@CR", NZ(LoadCashBookDataSet.Tables(0).Rows(I)(4)))
command.Parameters.AddWithValue("@DR", Convert.ToDecimal(LoadCashBookDataSet.Tables(0).Rows(I)(4)))

Else
command.Parameters.AddWithValue("@DR", Convert.ToDecimal(LoadCashBookDataSet.Tables(0).Rows(I)(4)))
command.Parameters.AddWithValue("@CR", Convert.ToDecimal(LoadCashBookDataSet.Tables(0).Rows(I)(5)))
End If
command.ExecuteNonQuery() 
'I = I + 1
[b]I += 1[/b]
End While

'Receive Error while execute this statement 
Catch ex As Exception
MsgBox(Err.Description)
End Try

End Sub

thx dear its works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.