Click here to Skip to main content
15,887,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai, i am creating here coding which use to substract number in database from
textbox. In my coding i use textbox which receive number from user and it will
subtract a number in database. For your info i am using UPDATE sql command for this operation .In the same time i also create table name InItem and have column name Itemquantity After i click the button i got an error invalidOperationException was unhandled from my coding .Here is my coding and please correct my coding if it have missing part. I also highlight which code give this error.Oh ya , is that my sql command for subtraction is correct? i also not sure cause it get it from internet.If my command if false please show me with correct command..Bye

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim ra As Integer

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

Dim Trans = Con.BeginTransaction

Dim cmd As New OleDb.OleDbCommand("UPDATE ItemIn SET Itemquantity = Itemquantity- '" & ItemquantityTextBox.Text & "WHERE ItemName = '" & ItemNameComboBox.Text & "'", Con)

ra = cmd.ExecuteNonQuery()// This is line which cause an error//
Trans.Commit()
Con.Close()
End Sub
Posted

1 solution

Two things here: one is that you have an unmatched quote character in your command string: at the end of the first string there is a single quote, which is not closed after the TextBox is appended.

The other is that that is a very dangerous way to do it: it leaves your database wide open to an accidental or deliberate SQL injection attack. Use Parametrized queries instead:
Dim cmd As New OleDb.OleDbCommand("UPDATE ItemIn SET Itemquantity = Itemquantity - @IQ WHERE ItemName = @IN", Con)
cmd.Parameters.AddWithValue("IQ", Int32.Parse(ItemquantityTextBox.Text))
cmd.Parameters.AddWithValue("IN", ItemNameComboBox.Text)
 
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