Error when changing values in datagridview

Sometime, but not everytime, when I enter or change a value in a dataviewgrid, I get the attached error. I get the error for each of the following three columns (In Amount, Out Amount and Other Amount).  I can click past the errors and everything seems to work okay.  Any ideas where it's finding a NULL in the amount fields?  I set them either to 0 or the calculated amount.  DataGridView-Error-Message.doc
kshumwayAsked:
Who is Participating?
 
Alfredo Luis Torres SerranoDevelopment Director / DBACommented:
The code is too extensive, Could you replicate the error??

In which situation are you geting the exception?

The error seems to be when a field in the data is null you have to change the cell value to dbnull.value cause datagridview does not handle null value like SQL does.

Hope this help
0
 
Alfredo Luis Torres SerranoDevelopment Director / DBACommented:
Could you post you dbgridvew methods??

0
 
kshumwayAuthor Commented:
Below is all the associated code (from the form and in a module).  Thank you so much for your help!
Imports System.Data.SqlClient
Public Class ManualEntries
    Dim sHID As String
    Dim sDeptCode As String
    Dim dtBillDate As Date
    Dim sBilldate As String
    Dim iInTests As Integer
    Dim dblInAmount As Double
    Dim iOutTests As Integer
    Dim dblOutAmount As Double
    Dim iOthTests As Integer
    Dim dblOthAmount As Double
    Dim sSvDesc As String

    Private Sub ManualEntries_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '/* Set up data adapter, data table and command builder to the manual entries.  Format the data view grid */
        '/* and load the HID and Dept combo boxes.  Set the Invoice date in text box which is the last day of     */
        '/* the previous month.                                                                                   */

        Dim dBillDate As Date

        'DelMonthlyManualData()
        dBillDate = InvoiceDate()
        Me.txtInvDate.Text = dBillDate.ToString
        LoadGrid()
    End Sub

    Private Sub btnAddRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRow.Click
        Addrecord()
    End Sub
    Sub FormatDataGridView1()

        '/* Format the datgridview */

        Me.DataGridView1.Columns("HID").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        Me.DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        Me.DataGridView1.Columns("HID").Width = 50
        Me.DataGridView1.Columns("DeptCode").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        Me.DataGridView1.Columns("DeptCode").HeaderText = "Department"
        Me.DataGridView1.Columns("DeptCode").Width = 70
        Me.DataGridView1.Columns("BillDate").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        Me.DataGridView1.Columns("BillDate").HeaderText = "Invoice Date"
        Me.DataGridView1.Columns("BillDate").Width = 65
        Me.DataGridView1.Columns("InTests").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        Me.DataGridView1.Columns("InTests").HeaderText = "Inpatient Tests"
        Me.DataGridView1.Columns("InTests").Width = 65
        Me.DataGridView1.Columns("InAmount").DefaultCellStyle.Format = "c"
        Me.DataGridView1.Columns("InAmount").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        Me.DataGridView1.Columns("InAmount").HeaderText = "Inpatient Amount"
        Me.DataGridView1.Columns("InAmount").Width = 70
        Me.DataGridView1.Columns("OutTests").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        Me.DataGridView1.Columns("OutTests").HeaderText = "Outpatient Tests"
        Me.DataGridView1.Columns("OutTests").Width = 65
        Me.DataGridView1.Columns("OutAmount").DefaultCellStyle.Format = "c"
        Me.DataGridView1.Columns("OutAmount").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        Me.DataGridView1.Columns("OutAmount").HeaderText = "Outpatient Amount"
        Me.DataGridView1.Columns("OutAmount").Width = 70
        Me.DataGridView1.Columns("OtherTests").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        Me.DataGridView1.Columns("OtherTests").HeaderText = "Other Tests"
        Me.DataGridView1.Columns("OtherTests").Width = 65
        Me.DataGridView1.Columns("OtherAmount").DefaultCellStyle.Format = "c"
        Me.DataGridView1.Columns("OtherAmount").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        Me.DataGridView1.Columns("OtherAmount").HeaderText = "Other Amount"
        Me.DataGridView1.Columns("OtherAmount").Width = 70
        Me.DataGridView1.Columns("SvDesc").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
        Me.DataGridView1.Columns("SvDesc").HeaderText = "Service Detail"
        Me.DataGridView1.Columns("SvDesc").Width = 125
    End Sub

    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        DeleteRecord()
    End Sub
    Sub ClearForm()

        '/* Initialize form fields */

        Me.txtInAmt.ReadOnly = False
        Me.txtInAmt.Text = " "
        Me.txtInTests.Text = " "
        Me.txtOutAmt.ReadOnly = False
        Me.txtOthAmt.Text = " "
        Me.txtOthTests.Text = " "
        Me.txtOthAmt.ReadOnly = False
        Me.txtOutAmt.Text = " "
        Me.txtOutTests.Text = " "
        Me.txtSVDetail.Text = " "

    End Sub

    Private Sub txtOutAmt_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtOutAmt.LostFocus
        '/* If Tier 1 or Tier 2 Test, calculate the cost (Num tests * contract price) via a call to TestPriceCalc. */
        '/* Format the value in the text box to display as currency. This is needed if user tries to incorrectly   */
        '/* change the pricing in the text box                                                                     */

        If Not IsEmpty(Me.txtOutAmt.Text) Then
            If Not IsNumeric(Me.txtOutAmt.Text) Then
                MsgBox("Invalid entry.  Must be a  numeric value.")
                Me.txtOutAmt.Focus()
                Me.txtOutAmt.Text = ""
            Else
                If IsNumeric(Me.txtOutAmt.Text) And IsNumeric(Me.txtOutTests.Text) Then
                    Me.txtOutAmt.Text = Format(CDbl(Me.txtOutAmt.Text), "c")
                End If
            End If
        End If

    End Sub

    Private Sub txtOthAmt_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtOthAmt.LostFocus
        '/* If Tier 1 or Tier 2 Test, calculate the cost (Num tests * contract price) via a call to TestPriceCalc. */
        '/* Format the value in the text box to display as currency. This is needed if user tries to incorrectly   */
        '/* change the pricing in the text box                                                                     */

        If Not IsEmpty(Me.txtOthAmt.Text) Then
            If Not IsNumeric(Me.txtOthAmt.Text) Then
                MsgBox("Invalid entry.  Must be a  numeric value.")
                Me.txtOthAmt.Focus()
                Me.txtOthAmt.Text = ""
            Else
                If IsNumeric(Me.txtOthAmt.Text) And IsNumeric(Me.txtOthTests.Text) Then
                    Me.txtOthAmt.Text = Format(CDbl(Me.txtOthAmt.Text), "c")
                End If
            End If
        End If

    End Sub
   
    Private Sub txtSVDetail_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSVDetail.LostFocus
        '/* Verify the Service Detail entry string is not longer than 30 characters which is needed to fit correctly */
        '/* on the invoice.                                                                                          */

        Dim iLen As Integer = Len(Me.txtSVDetail.Text)
        If iLen > 30 Then
            MsgBox("Service Detail string is > 30 characters.  Please shorten.")
            Me.txtSVDetail.Text = " "
            Me.txtSVDetail.Focus()
        Else
            Me.txtSVDetail.Text = UCase(Trim(Me.txtSVDetail.Text))
        End If

    End Sub

    Private Sub txtInTests_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInTests.LostFocus
        '/* If Tier 1 or Tier 2 Test, calculate the cost (Num tests * contract price) via a call to TestPriceCalc. */

        If Not IsEmpty(Me.txtInTests.Text) Then
            If Not IsNumeric(Me.txtInTests.Text) Then
                MsgBox("Invalid entry.  Must be a  numeric value.")
                Me.txtInTests.Focus()
                Me.txtInTests.Text = ""
            Else
                If IsNumeric(Me.txtInTests.Text) Then
                    Me.txtInAmt.Text = TestPriceCalc(Me.cboHID.Text, Me.cboDept.Text, CInt(Me.txtInTests.Text))
                    If IsNumeric(Me.txtInAmt.Text) Then
                        Me.txtInAmt.ReadOnly = True
                    End If
                End If
            End If
        End If

    End Sub

    Private Sub txtOthTests_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtOthTests.LostFocus
        '/* If Tier 1 or Tier 2 Test, calculate the cost (Num tests * contract price) via a call to TestPriceCalc. */

        If Not IsEmpty(Me.txtOthTests.Text) Then
            If Not IsNumeric(Me.txtOthTests.Text) Then
                MsgBox("Invalid entry.  Must be a  numeric value.")
                Me.txtOthTests.Focus()
                Me.txtOthTests.Text = ""
            Else
                If IsNumeric(Me.txtOthTests.Text) Then
                    Me.txtOthAmt.Text = TestPriceCalc(Me.cboHID.Text, Me.cboDept.Text, CInt(Me.txtOthTests.Text))
                    If IsNumeric(Me.txtOthAmt.Text) Then
                        Me.txtOthAmt.ReadOnly = True
                    End If
                End If
            End If
        End If

    End Sub

    Private Sub txtOutTests_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtOutTests.LostFocus
        '/* If Tier 1 or Tier 2 Test, calculate the cost (Num tests * contract price) via a call to TestPriceCalc. */

        If Not IsEmpty(Me.txtOutTests.Text) Then
            If Not IsNumeric(Me.txtOutTests.Text) Then
                MsgBox("Invalid entry.  Must be a  numeric value.")
                Me.txtOutTests.Focus()
                Me.txtOutTests.Text = ""
            Else
                If IsNumeric(Me.txtOutTests.Text) Then
                    Me.txtOutAmt.Text = TestPriceCalc(Me.cboHID.Text, Me.cboDept.Text, CInt(Me.txtOutTests.Text))
                    If IsNumeric(Me.txtOutAmt.Text) Then
                        Me.txtOutAmt.ReadOnly = True
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
        '/* if user changes a field on the DataGridView that affects pricing, recaculate the pricing. */

        DataGridView1.Rows(e.RowIndex).Cells(0).Value = UCase(DataGridView1.Rows(e.RowIndex).Cells(0).Value)
        DataGridView1.Rows(e.RowIndex).Cells(1).Value = UCase(DataGridView1.Rows(e.RowIndex).Cells(1).Value)
        DataGridView1.Rows(e.RowIndex).Cells(9).Value = UCase(DataGridView1.Rows(e.RowIndex).Cells(9).Value)
        DataGridView1.Rows(e.RowIndex).Cells(4).Value = TestPriceCalc(DataGridView1.Rows(e.RowIndex).Cells(0).Value, DataGridView1.Rows(e.RowIndex).Cells(1).Value, DataGridView1.Rows(e.RowIndex).Cells(3).Value)
        DataGridView1.Rows(e.RowIndex).Cells(6).Value = TestPriceCalc(DataGridView1.Rows(e.RowIndex).Cells(0).Value, DataGridView1.Rows(e.RowIndex).Cells(1).Value, DataGridView1.Rows(e.RowIndex).Cells(5).Value)
        DataGridView1.Rows(e.RowIndex).Cells(8).Value = TestPriceCalc(DataGridView1.Rows(e.RowIndex).Cells(0).Value, DataGridView1.Rows(e.RowIndex).Cells(1).Value, DataGridView1.Rows(e.RowIndex).Cells(7).Value)

    End Sub

    Private Sub txtInAmt_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInAmt.LostFocus
        '/* If Tier 1 or Tier 2 Test, calculate the cost (Num tests * contract price) via a call to TestPriceCalc. */
        '/* Format the value in the text box to display as currency. This is needed if user tries to incorrectly   */
        '/* change the pricing in the text box                                                                     */

        If Not IsEmpty(Me.txtInAmt.Text) Then
            If Not IsNumeric(Me.txtInAmt.Text) Then
                MsgBox("Invalid entry.  Must be a  numeric value.")
                Me.txtInAmt.Focus()
                Me.txtInAmt.Text = ""
            Else
                If IsNumeric(Me.txtInAmt.Text) And IsNumeric(Me.txtInTests.Text) Then
                    Me.txtInAmt.Text = Format(CDbl(Me.txtInAmt.Text), "c")
                End If
            End If
        End If

    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        CloseForm()
    End Sub
    Sub CloseForm()
        '/* After verifying the user wants the data from the DataGridView written to the database, using a call to */
        '/* dataAdapter.update, write all inserts, updates and deletes from the datagridview to the database.      */
        '/* Close the Manual Entry form and show the Main form.                                                    */

        
        daHospManData.InsertCommand = cmdBuilder.GetInsertCommand()
        daHospManData.Update(dtHospManData)
        daHospManData.UpdateCommand = cmdBuilder.GetUpdateCommand()
        daHospManData.Update(dtHospManData)
        daHospManData.DeleteCommand = cmdBuilder.GetDeleteCommand()
        daHospManData.Update(dtHospManData)
        SaveManualInvoiceData(Me.txtInvDate.Text)


        Me.Close()
        Main.StartPosition = FormStartPosition.CenterScreen
        Main.Show()
    End Sub
    Sub DeleteRecord()

        '/* After verifying the user wants to delete the row, delete the selected row. */
        Dim cmdDelete As New SqlCommand
        Dim RowIndex = DataGridView1.CurrentRow.Index

        Dim sAns As String
        sAns = MsgBox("Are you sure you want to delete the selected row?", MsgBoxStyle.YesNo, "Delete Record?")
        If sAns = vbYes Then
            DataGridView1.Rows.RemoveAt(RowIndex)
        End If
    End Sub
    Sub Addrecord()
        '/* Ensure all entered data is valid by call to Validate Entries.  If valid, add row to the datagridview and  */
        '/* clear required textboxes for next entry.                                                                  */

        Dim bOK As Boolean = True
        bOK = ValidateEntries()
        If bOK Then
            AddRow()
            ClearForm()
        End If
    End Sub

    Private Sub AddRowToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddRowToolStripMenuItem.Click
        Addrecord()
    End Sub

    Private Sub DeleteRowToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DeleteRowToolStripMenuItem.Click
        DeleteRecord()
    End Sub

    Private Sub btnArchData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArchData.Click
        LoadGrid()
    End Sub
    Sub LoadGrid()
        DelMonthlyManualData()
        LoadHospitalManualData(Me.txtInvDate.Text)
        Try
            conn.Open()
            daHospManData = New SqlClient.SqlDataAdapter("GetManualInvoiceData", conn)
            dtHospManData.Clear()
            daHospManData.SelectCommand.CommandType = CommandType.StoredProcedure
            cmdBuilder = New SqlCommandBuilder(daHospManData)
            daHospManData.Fill(dtHospManData)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn.Close()
        End Try
        DataGridView1.DataSource = dtHospManData
        FormatDataGridView1()
        LoadHIDList()
        LoadDeptList()
    End Sub
End Class
Imports System.Data.SqlClient
Module modManualEntries
    Public InvMonth As String
    Public InvDay As String
    Public InvYear As String
    Public dtHospManData As New DataTable                                                   'Hospital Invoice Data Table
    Public daHospManData As SqlClient.SqlDataAdapter
    Public cmdBuilder As SqlCommandBuilder
    Public dtDept As New DataTable
    Sub LoadHIDList()

        '/* Load the HID combobox with Hospital */

        ManualEntries.cboHID.Items.Clear()
        ManualEntries.cboHID.Items.Add("CGH")
        ManualEntries.cboHID.Items.Add("CH")
        ManualEntries.cboHID.Items.Add("SJH")
    End Sub
    Sub LoadDeptList()

        '/* Load the Department combo box with departments from Main Department table and save the LorS value */
        '/* for later selection in the datatable

        Dim sSQL As String = "SELECT MainDept, LorS from HospDepts ORDER BY MainDept"
        dtDept.Clear()
        Dim daDept As New SqlClient.SqlDataAdapter(sSQL, conn)

        Try
            daDept.Fill(dtDept)
            daDept.Dispose()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        ManualEntries.cboDept.Items.Clear()
        For Each row As DataRow In dtDept.Rows
            ManualEntries.cboDept.Items.Add(row.Item("MainDept"))
        Next
    End Sub
    Sub AddRow()

        '/* Add the added data to the data grid view */

        Dim drHospManData As DataRow = dtHospManData.NewRow
        Dim bCont As Boolean = False

        drHospManData("HID") = ManualEntries.cboHID.Text
        drHospManData("DeptCode") = ManualEntries.cboDept.Text
        drHospManData("BillDate") = ManualEntries.txtInvDate.Text
        If IsNumeric(ManualEntries.txtInTests.Text) Then
            drHospManData("InTests") = ManualEntries.txtInTests.Text
        Else
            drHospManData("InTests") = 0
        End If
        If IsNumeric(ManualEntries.txtInAmt.Text) Then
            drHospManData("InAmount") = CType(ManualEntries.txtInAmt.Text, Decimal)
        Else
            drHospManData("InAmount") = 0
        End If
        If IsNumeric(ManualEntries.txtOutTests.Text) Then
            drHospManData("OutTests") = ManualEntries.txtOutTests.Text
        Else
            drHospManData("OutTests") = 0
        End If
        If IsNumeric(ManualEntries.txtOutAmt.Text) Then
            drHospManData("OutAmount") = CType(ManualEntries.txtOutAmt.Text, Decimal)
        Else
            drHospManData("OutAmount") = 0
        End If
        If IsNumeric(ManualEntries.txtOthTests.Text) Then
            drHospManData("OtherTests") = ManualEntries.txtOthTests.Text
        Else
            drHospManData("OtherTests") = 0
        End If
        If IsNumeric(ManualEntries.txtOthAmt.Text) Then
            drHospManData("OtherAmount") = CType(ManualEntries.txtOthAmt.Text, Decimal)
        Else
            drHospManData("OtherAmount") = 0
        End If
        If Len(ManualEntries.txtSVDetail.Text) > 0 Then
            drHospManData("SvDesc") = ManualEntries.txtSVDetail.Text
        Else
            drHospManData("SvDesc") = "   "
        End If
        drHospManData("LorS") = LorS(ManualEntries.cboDept.Text)
        dtHospManData.Rows.Add(drHospManData)
    End Sub
    Public Function ValidateEntries() As Boolean

        '/* Verify all required entries are entered */

        Dim bOK As Boolean = True
        If IsEmpty(ManualEntries.cboHID.Text) Then
            bOK = False
            MsgBox("Please select a hospital.")
            ManualEntries.cboHID.Focus()
        Else
            If IsEmpty(ManualEntries.cboDept.Text) Then
                bOK = False
                MsgBox("Please select a department.")
                ManualEntries.cboDept.Focus()
            Else
                If IsEmpty(ManualEntries.txtInvDate.Text) Then
                    bOK = False
                    MsgBox("Please enter the Invoice Date")
                    ManualEntries.txtInvDate.Focus()
                Else
                    If IsEmpty(ManualEntries.txtInTests.Text) Then
                        ManualEntries.txtInTests.Text = 0
                    Else
                        If IsEmpty(ManualEntries.txtInAmt.Text) Then
                            ManualEntries.txtInAmt.Text = 0
                        Else
                            If IsEmpty(ManualEntries.txtOutTests.Text) Then
                                ManualEntries.txtOutTests.Text = 0
                            Else
                                If IsEmpty(ManualEntries.txtOutAmt.Text) Then
                                    ManualEntries.txtOutAmt.Text = 0
                                Else
                                    If IsEmpty(ManualEntries.txtOthTests.Text) Then
                                        ManualEntries.txtOthTests.Text = 0
                                    Else
                                        If IsEmpty(ManualEntries.txtOthAmt.Text) Then
                                            ManualEntries.txtOthAmt.Text = 0
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
        Return bOK
    End Function

    Function TestPriceCalc(ByVal sHid As String, ByVal sDept As String, ByVal iNumTests As Integer) As String

        '/* Calculate the test pricing for Tier1 and Tier 2 tests.  Tier1 pricing is based on the Hospital  */
        '/* Tier2 pricing is the same for all three owner hospitals.                                        */

        If Not IsEmpty(sDept) And Not IsEmpty(sHid) Then
            Select Case sDept
                Case "AP", "BB", "C", "H", "IM", "MC", "MCS"
                    Select Case sHid
                        Case "CGH"
                            TestPriceCalc = (iNumTests * Tier1PriceCGH).ToString
                        Case "CH"
                            TestPriceCalc = (iNumTests * Tier1PriceCH).ToString
                        Case "SJH"
                            TestPriceCalc = (iNumTests * Tier1PriceSJH).ToString
                    End Select
                Case "IR"
                    Return iNumTests * Tier2Price
            End Select
        Else
            Return "0"
        End If
    End Function
    Function LorS(ByVal sDept As String) As String
        '/* Get the Lab or Service Field from HospDept for the selected Dept */
        Dim sLorS As String = " "
        For Each row As DataRow In dtDept.Rows
            If row.Item("MainDept") = sDept Then
                sLorS = row.Item("LorS")
            End If
        Next
        Return sLorS
    End Function
    Sub SaveManualInvoiceData(ByVal dBillDate As Date)

        DelMonthlyManualDataArchive(dBillDate)

        Dim dtManInv As New DataTable
        Dim daManInv As New SqlClient.SqlDataAdapter("SELECT * FROM [Hospital Manual Data]", conn)

        Try
            dtManInv.Clear()
            conn.Open()
            daManInv.Fill(dtManInv)
            daManInv.Dispose()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            conn.Close()
        End Try

        For Each mrow In dtManInv.Rows
            InsertManualInvoice(1, mrow.item("HID"), mrow.item("DeptCode"), mrow.item("BillDate"), mrow.item("InTests"), mrow.item("InAmount"), mrow.item("OutTests"), mrow.item("OutAmount"), mrow.item("OtherTests"), mrow.item("OtherAmount"), mrow.item("SvDesc"), mrow.item("LorS"))
        Next
    End Sub
    Sub DelMonthlyManualDataArchive(ByVal dBillDate As Date)
        Dim cmdHP As New SqlCommand
        Try
            conn.Open()
            cmdHP.Connection = conn
            cmdHP.CommandType = CommandType.StoredProcedure
            cmdHP.Parameters.AddWithValue("@BillDate", dBillDate)
            cmdHP.CommandText = "DeleteMonthlyManualData"
            cmdHP.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            conn.Close()
        End Try
    End Sub
    Sub InsertManualInvoice(ByVal iWhich As Integer, ByVal shid As String, ByVal sDeptCode As String, ByVal dbilldate As Date, ByVal iInTests As Integer, ByVal siInAmount As Single, ByVal iOutTests As Integer, ByVal siOutAmount As Single, ByVal iOtherTests As Integer, ByVal siOtherAmount As Single, ByVal sSvDesc As String, ByVal sLorS As String)
        Dim cmdManInv As New SqlCommand
        Try
            conn.Open()
            cmdManInv.Connection = conn
            cmdManInv.CommandType = CommandType.StoredProcedure
            cmdManInv.Parameters.AddWithValue("@HID", shid)
            cmdManInv.Parameters.AddWithValue("@DeptCode", sDeptCode)
            cmdManInv.Parameters.AddWithValue("@BillDate", dbilldate)
            cmdManInv.Parameters.AddWithValue("@InTests", iInTests)
            cmdManInv.Parameters.AddWithValue("@InAmount", siInAmount)
            cmdManInv.Parameters.AddWithValue("@OutTests", iOutTests)
            cmdManInv.Parameters.AddWithValue("@OutAmount", siOutAmount)
            cmdManInv.Parameters.AddWithValue("@OtherTests", iOtherTests)
            cmdManInv.Parameters.AddWithValue("@OtherAmount", siOtherAmount)
            cmdManInv.Parameters.AddWithValue("@SvDesc", sSvDesc)
            cmdManInv.Parameters.AddWithValue("@LorS", sLorS)
            If iWhich = 1 Then
                cmdManInv.CommandText = "InsertManualInvoiceArchive"
            Else
                cmdManInv.CommandText = "InsertHospManualData"
            End If
            cmdManInv.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn.Close()
        End Try
    End Sub
    Sub DelMonthlyManualData()
        Dim cmdHP As New SqlCommand
        Try
            conn.Open()
            cmdHP.Connection = conn
            cmdHP.CommandType = CommandType.StoredProcedure
            cmdHP.CommandText = "DelHospManualData"
            cmdHP.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            conn.Close()
        End Try
    End Sub
    Sub LoadHospitalManualData(ByVal sBillDate As String)
        Dim dtManInv As New DataTable

        Dim ssql = "SELECT * FROM [Hospital Manual Invoice Archive] WHERE BillDate = CONVERT(DATETIME, '" & sBillDate & "', 102) ORDER BY HID, DeptCode"
        Dim daManInv As New SqlClient.SqlDataAdapter(ssql, conn)
        Try
            dtManInv.Clear()
            conn.Open()
            daManInv.Fill(dtManInv)
            daManInv.Dispose()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            conn.Close()
        End Try

        For Each mrow In dtManInv.Rows
            InsertManualInvoice(2, mrow.item("HID"), mrow.item("DeptCode"), mrow.item("BillDate"), mrow.item("InTests"), mrow.item("InAmount"), mrow.item("OutTests"), mrow.item("OutAmount"), mrow.item("OtherTests"), mrow.item("OtherAmount"), mrow.item("SvDesc"), mrow.item("LorS"))
        Next
    End Sub

End Module

Open in new window

0
 
kshumwayAuthor Commented:
I have not been able to get back to this problem due to other priorities.  I'll try to respond soon.
0
 
kshumwayAuthor Commented:
I figured it out.  In some cases, I am calculating the value of cell in the grid and in some cases I am not.  The error was occurring when the cell was not being calculated. I put an if statement in so the code was only entered when the cell was calculated and not when it was manually entered.  That corrected the problem.  Thank you to Masteraco.  I used his suggestion to narrow down when teh error was actually happening.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.