Link to home
Start Free TrialLog in
Avatar of Jimnoel Valmonte
Jimnoel Valmonte

asked on

Not Working here, trying update some data from sql for school project, please help me

I've tried theses code but these isn't working, please help me T_T

Dim st As String = "Update OrderItems set Item=@item,Price=@price where ID=@id"
        Dim cmd As New SqlCommand(st, con)

        Try

            If cmd.Connection.State = ConnectionState.Open Then cmd.Connection.Close()
            con.ConnectionString = connection
            cmd.Connection.Open()
            cmd.Parameters.AddWithValue("@item", txtitem.Text.Trim)
            cmd.Parameters.AddWithValue("@price", txtprice.Text.Trim)
            cmd.Parameters.AddWithValue("@id", txtid.Text.Trim)

            If cmd.ExecuteNonQuery > 0 Then
                MsgBox("Updated Successfully", MsgBoxStyle.Information)
                txtitem.Clear()
                txtprice.Clear()
                txtid.Clear()
                txtitem.Focus()

            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cmd.Connection.Close()

        End Try
Avatar of Gregory Miller
Gregory Miller
Flag of United States of America image

can you post your error messages or any other info to help
I dont see any issue except you create a cmd using old connection, then close if it is open, then change connection string and re-open!!!

try this

try
  Dim st As String = "Update OrderItems set Item=@item,Price=@price where ID=@id"
  Dim con As New SqlConnection(connection)
  Dim command As New SqlCommand(queryString, con)
  command.Connection.Open()

  cmd.Parameters.AddWithValue("@item", txtitem.Text.Trim)
  cmd.Parameters.AddWithValue("@price", txtprice.Text.Trim)
  cmd.Parameters.AddWithValue("@id", txtid.Text.Trim)

  dim retCode as integer = command.ExecuteNonQuery()
  If cmd.ExecuteNonQuery > 0 Then
    MsgBox("Updated Successfully", MsgBoxStyle.Information)
    txtitem.Clear()
    txtprice.Clear()
    txtid.Clear()
    txtitem.Focus()
  End if
Catch
  MsgBox(ex.Message)
Finally   
  con.close()  
End Try

Open in new window

Avatar of Jimnoel Valmonte
Jimnoel Valmonte

ASKER

There's no error sir, the button just not working, what i want to do is, if i click the button, the data from database will show in textbox then if i change anything on that data then click the update button, the data from database will be updated, my code isn't working T_T
my code isn't working T_T

not sure what that means!
put a breakpoint on the code and debug it...
which line is giving error? which line is not working?
are you really calling that function?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.