Hello friend,
I have a problem with my coding that drive me nut for the past three days. Hope someone can help me.
The code is first, I update the current existing record from a database. I use the SQL UPDATE query to update the record after searching for it from an access database. After the update has been complete, I use the SQL SELECT query to search for the column "Amount" in access and fill it up in a database called datDataSet. I then use a FOR loop and loop through each row and column and sum the amount up for each column/row. After I got the new sum of the amount column, I will then display the new sum. But, when I run the code, I receive the error "System.InvalidCastException: Operator is not valid for type 'Long' and type 'DBNul" when it come to the part that executing the code and sum up the "amount" total. I doesn't know why, I use the Debug method and execute each line of code and every line seem to be functioning correctly, except the this line of code:
mDebAmt += datDataSet.Tables(0).Rows(mI)(0). Any help would be appreciated.
I have a problem with my coding that drive me nut for the past three days. Hope someone can help me.
The code is first, I update the current existing record from a database. I use the SQL UPDATE query to update the record after searching for it from an access database. After the update has been complete, I use the SQL SELECT query to search for the column "Amount" in access and fill it up in a database called datDataSet. I then use a FOR loop and loop through each row and column and sum the amount up for each column/row. After I got the new sum of the amount column, I will then display the new sum. But, when I run the code, I receive the error "System.InvalidCastException: Operator is not valid for type 'Long' and type 'DBNul" when it come to the part that executing the code and sum up the "amount" total. I doesn't know why, I use the Debug method and execute each line of code and every line seem to be functioning correctly, except the this line of code:
mDebAmt += datDataSet.Tables(0).Rows(mI)(0). Any help would be appreciated.
VB.NET:
[/COLOR]
Try
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'1 -> Create SQL UPDATE Query to data from the database
[/COLOR][/SIZE][SIZE=2]OleDataAdapter.UpdateCommand.CommandText = _
"UPDATE Expense SET Amount = " & txtAmount.Text & ", " & _
"Dates = '" & Convert.ToDateTime(txtDate.Text) & "', " & _
"Months = " & cboMonth.SelectedIndex & ", " & _
"Years = " & cboYear.SelectedIndex & ", " & _
"Debit = '" & radDebit.Checked & "', " & _
"Credit = '" & radCredit.Checked & "', " & _
"ExpensesType = " & cboExpType.SelectedIndex & ", " & _
"Summary = '" & txtSummary.Text & "';"
[/SIZE][SIZE=2][COLOR=#008000]'2 -> Execute the SQL UPDATE query
[/COLOR][/SIZE][SIZE=2]OleDataAdapter.UpdateCommand.ExecuteNonQuery()
[/SIZE][SIZE=2][COLOR=#008000]'<-------------------------------------------------------------------->
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'3 -> Clear the CreDataSet from the previous operation
[/COLOR][/SIZE][SIZE=2]datDataSet.Clear()
[/SIZE][SIZE=2][COLOR=#008000]'4 -> Use SQL SELECT query to gather the update expense amount and sum
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'---> it up so the a new debit, credit, total, and balance amount can be
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'---> updated.
[/COLOR][/SIZE][SIZE=2]OleDataAdapter.SelectCommand.CommandText = _
"SELECT Amount FROM Expense WHERE " & _
"Debit = '" & [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2] & "' "
[/SIZE][SIZE=2][COLOR=#008000]'5 -> Fill the datDataSet with the data from the SELECT query
[/COLOR][/SIZE][SIZE=2]OleDataAdapter.Fill(datDataSet)
[/SIZE][SIZE=2][COLOR=#008000]'6 -> Store the total of record found from the datDataSet into the
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'---> variable mCreMax
[/COLOR][/SIZE][SIZE=2]mMax = datDataSet.Tables(0).Rows.Count
[/SIZE][SIZE=2][COLOR=#008000]'7 -> If the DataSet is not empty
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] datDataSet.Tables(0).Rows.Count <> 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'8 -> Use FOR loop to loop through every single data and
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'---> sum up the Debit amount. Store the sum in variable
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'---> mDebAmt
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] mI = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] mMax - 1
[/SIZE][SIZE=2][COLOR=#008000]'9 -> Sum up the debit amount and store it in the variable
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'---> mCreAmt
[/COLOR][/SIZE][SIZE=2]mDebAmt += datDataSet.Tables(0).Rows(mI)(0)
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] mI
[/SIZE][SIZE=2][COLOR=#008000]'10 -> Store the Debit amount in textbox txtDebit.Text
[/COLOR][/SIZE][SIZE=2]txtDebit.Text = mDebAmt
[/SIZE][SIZE=2][COLOR=#008000]'11 -> Set the mDebAmt back to the default value of 0
[/COLOR][/SIZE][SIZE=2]mDebAmt = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'12 -> Set the textbox txtDebit.Text = 0 if there's no data from the
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'----> database
[/COLOR][/SIZE][SIZE=2]txtDebit.Text = 0
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'CreDataSetSet.Tables(0).Rows.Count <> 0
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] OleException [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Data.OleDb.OleDbException
[/SIZE][SIZE=2][COLOR=#008000]'6 -> Print the Error report if there's a problem updating the record.
[/COLOR][/SIZE][SIZE=2]Console.WriteLine(OleException.StackTrace)
[/SIZE][SIZE=2][COLOR=#008000]'7 -> Display the Error message
[/COLOR][/SIZE][SIZE=2]MessageBox.Show(OleException.ToString)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][COLOR=black]
Last edited by a moderator: