Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a problem with my coding. The error which i got is oleDbException was unhandled. Here I am developing function use to subtract number in database based on user input. I don't know how to solve this error , Please help me to correct my coding here. I also highlight the line causing an error


VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         
        'open db connection'
        Dim Con As New OleDb.OleDbConnection("Provider=SQLOLEDB ;Data Source=Danawa;Initial Catalog=Store;Integrated Security=SSPI ")
        Try
            Con.Open()
        Catch ex As InvalidOperationException
            MsgBox(ex.Message)
        End Try


        'start transaction'
        Dim Trans = Con.BeginTransaction

        'define a sql command statement'
        Dim cmd As New OleDb.OleDbCommand("UPDATE ItemIn SET Itemquantity= Itemquantity - @IQ  WHERE ItemName=@IN", Con, Trans)
        cmd.Parameters.AddWithValue("@IQ", Int32.Parse(ItemquantityTextBox.Text))
        cmd.Parameters.AddWithValue("@IN", ItemNameComboBox.Text)
        
        'execute the command'
         cmd.ExecuteNonQuery()  // The probleam are from here//      
         Trans.Commit()
        Con.Close()
End Sub
Posted
Updated 11-Oct-11 17:36pm
v3
Comments
Prerak Patel 11-Oct-11 23:37pm    
Use code blocks.
Prerak Patel 11-Oct-11 23:38pm    
I would suggest to write that part in try catch, and check the exception details.

Hi,

Try this:

VB
  'open db connection'
  Dim Con As New OleDb.OleDbConnection("Provider=SQLOLEDB ;Data Source=Danawa;Initial  Catalog=Store;Integrated Security=SSPI ")
  Dim trans As OleDb.OleDbTransaction
  trans = Con.BeginTransaction()
Try
  Con.Open()
  'define a sql command statement'
  Dim cmd As New OleDb.OleDbCommand("UPDATE ItemIn SET Itemquantity= Itemquantity - @IQ   WHERE ItemName=@IN", Con, Trans)
  cmd.Parameters.AddWithValue("@IQ", Int32.Parse(ItemquantityTextBox.Text))
  cmd.Parameters.AddWithValue("@IN", ItemNameComboBox.Text)
  'execute the command'
  cmd.ExecuteNonQuery() // The probleam are from here// 
  trans.Commit()
Catch
  trans.Rollback()
  Finally
   Con.Close()
   Con.Dispose()
End Try


Hope this could help...

Remember to vote, if help...

Regards,

Algem
 
Share this answer
 
v3
Use try catch in your code, after that you are able to find the exact problem.

I think you have no need to parse the text box value into int, because you are using parametrized query. Remove "Int32.Parse" and try it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900