0
'Adding Function
Private Sub AddCustomer()
Try
sql = "INSERT INTO Category(catid, caname) Values('" & TextBox1.Text & "', '" & TextBox2.Text & "')"
ConnD()
cmd = New MySqlCommand(sql, conn1)
Dim i As Integer
i = cmd.ExecuteNonQuery
If i > 0 Then
MsgBox("Customer Added", MsgBoxStyle.Information, "Add Customer")
Else
MsgBox("Failed to add customer", MsgBoxStyle.Critical, "Add Customer")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn1.Close()
End Try
End Sub
'UpdateFunction
Private Sub UpdateCustomer()
Try
sql = "Update category set caname ='" & TextBox2.Text & "' where catid = '" & TextBox1.Text & "' "
ConnD()
cmd = New MySqlCommand(sql, conn1)
Dim i As Integer
i = cmd.ExecuteNonQuery
If i > 0 Then
MsgBox("Customer Updated", MsgBoxStyle.Information, "Update Customer")
Else
MsgBox("Failed to update customer", MsgBoxStyle.Critical, "Update Customer")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn1.Close()
End Try
End Sub
をレコードを更新し、追加したいです。私は、私は1つのボタンを使用して</em>と<em>編集</em>を追加<em>を使用しようとしましたvb.netで一つのボタンを使用して
私は以下のコードを記述しました。
更新部分のみが動作します。 にの部分が追加されていないのはなぜですか?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Len(TextBox1.Text) > 0 Then
UpdateCustomer()
Else
AddCustomer()
End If
End Sub
あなたがする必要がある最初の事はSQLインジェクションについて読み、パラメータ化されましたクエリ。 –
Textbox1のテキストの長さは、更新または保存のいずれの場合でも常に0より大きくなります。ボタンのテキストで確認してください。 – PRABA
SQLインジェクションについて忘れないでください。 –