0

i am using vb.net and mysqladmin as my database.

i have a problem in my codes, and i don't know how to debug it. please help me..

my problem is when i click the button update the error shows
"The CommandText property has not been properly initialized."

this is the codes:

 Dim intDB_ID_Selected As Integer


'Private Sub cmdupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdupdate.Click



If MessageBox.Show("Do you want to update this record?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then

 Dim sqlcommand As New MySqlCommand("UPDATE user_info " & _
                                 " SET name = '" & txtname.Text.Trim & "'," & _
                                 " address = '" & txtaddress.Text.Trim & "', " & _
                                 " age = '" & txtage.Text.Trim & "', " & _
                                 " WHERE id= '" & intDB_ID_Selected & "'", sConnection)
        Call execCmd(SQL)
        load1()
        MsgBox("Record updated successfully.", MsgBoxStyle.Information)
    End If
End Sub `


 Public Sub execCmd(ByVal PstrSQL As String)
    With cmd
        .CommandText = PstrSQL
        .ExecuteNonQuery()
    End With
End Sub

the error line is in

.ExecuteNonQuery()

i am a beginner in this language, so please help me. im begging you guys!!

anayaur30
  • 1
  • 1
  • 2
  • 3

2 Answers2

0

what is cmd and has it been initialized?

Oh, and try to used parameterized query. The earlier you get into the habit, the better.

user2930100
  • 346
  • 4
  • 10
0

Try doing this instead after the messagebox selection. cmd is not properly initialized in execCmd.

Dim sqlStr as String = "UPDATE user_info " & _
                       " SET name = '" & txtname.Text.Trim & "'," & _
                       " address = '" & txtaddress.Text.Trim & "', " & _
                       " age = '" & txtage.Text.Trim & "', " & _
                       " WHERE id= '" & intDB_ID_Selected & "'", sConnection)

Dim sqlcommand As New MySqlCommand(sqlStr)
sqlcommand.ExecuteNonQuery()

load1()
MsgBox("Record updated successfully.", MsgBoxStyle.Information)
Codemunkeee
  • 1,585
  • 5
  • 17
  • 29