Join 136,542 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,867 people online right now. Registration is fast and FREE... Join Now!
I'm working on a simple game program to keep track of my video game collection at home. The program already connects to SQL and populates the data into a listbox. Ive gotten my add button to work, but stumped on the update and delete part. Here is my code:
CODE
Private Sub EditPage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SqlConnection1.Open()
updatelst()
End Sub
Sub clear() txtName.Clear() txtGenre.Clear() txtDeveloper.Clear() txtRelease.Clear() txtSystem.Clear() End Sub
Sub updatelst() lstGName.Items.Clear()
DSGame.Clear() DBAGame.SelectCommand.CommandText = "SELECT * FROM Games ORDER by GameName;" DBAGame.Fill(DSGame)
For i As Integer = 0 To DSGame.Tables(0).Rows.Count - 1 lstGName.Items.Add((DSGame.Tables(0).Rows(i).Item(1)))
Next End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim problem As Boolean = False
If txtName.Text = "" Then MessageBox.Show("Please fill out the form completely", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error) ErrorProvider1.SetError(txtName, "Please enter a game name") problem = True
End If If txtDeveloper.Text = "" Then ErrorProvider1.SetError(txtDeveloper, "Please enter a game developer") problem = True End If If txtGenre.Text = "" Then ErrorProvider1.SetError(txtGenre, "Please enter a game genre") problem = True End If If txtSystem.Text = "" Then ErrorProvider1.SetError(txtSystem, "Please enter a game system") problem = True End If If txtSystem.Text = "" Then ErrorProvider1.SetError(txtSystem, "Please enter a game system") problem = True End If If txtRelease.Text = "" Then ErrorProvider1.SetError(txtRelease, "Please enter a release date") problem = True
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click clear() updatelst() btnAdd.Enabled = True
End Sub
Private Sub txtSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearch.Click txtSearch.Text = "" End Sub
Private Sub txtName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.TextChanged ErrorProvider1.SetError(txtName, "") End Sub
Private Sub txtDeveloper_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) ErrorProvider1.SetError(txtDeveloper, "") End Sub
Private Sub txtGenre_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGenre.TextChanged ErrorProvider1.SetError(txtGenre, "") End Sub
Private Sub txtSystem_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSystem.TextChanged ErrorProvider1.SetError(txtSystem, "") End Sub
Private Sub txtRelease_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRelease.TextChanged ErrorProvider1.SetError(txtRelease, "") End Sub
Private Sub lstGName_SelectedIndexChanged_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstGName.SelectedIndexChanged btnAdd.Enabled = False
txtName.Text = DSGame.Tables(0).Rows(lstGName.SelectedIndex).Item(1) txtGenre.Text = DSGame.Tables(0).Rows(lstGName.SelectedIndex).Item(2) txtDeveloper.Text = DSGame.Tables(0).Rows(lstGName.SelectedIndex).Item(5) txtRelease.Text = DSGame.Tables(0).Rows(lstGName.SelectedIndex).Item(4) txtSystem.Text = DSGame.Tables(0).Rows(lstGName.SelectedIndex).Item(3) End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Maybe I don't understand what your problem is... do you not understand what an update or delete statement does, how to code the SQL for it, or what?
Well basically i don't know what the code would be for the delete and update buttons. Ive done some research but i just cant get it to work. Maybe if you could point me in the right direction
Maybe I don't understand what your problem is... do you not understand what an update or delete statement does, how to code the SQL for it, or what?
Well basically i don't know what the code would be for the delete and update buttons. Ive done some research but i just cant get it to work. Maybe if you could point me in the right direction
Okay.. so you are unsure on the SQL for a delete statement.. cool.. What's your key(s) on your table?
For the update, what fields do you want to update (I would also need the key(s) for the table as well).
Esssentially a delete is similar to the select..
DELETE FROM <table name> WHERE <key column name> = <data>
Updates are: UPDATE <table name> SET <column name> = variable data , <column name2> = variable data2, etc WHERE <key column name> = <condition data>
You would plug these in where you had your SELECT * command earlier and run the same set of code..
Maybe I don't understand what your problem is... do you not understand what an update or delete statement does, how to code the SQL for it, or what?
Well basically i don't know what the code would be for the delete and update buttons. Ive done some research but i just cant get it to work. Maybe if you could point me in the right direction
Okay.. so you are unsure on the SQL for a delete statement.. cool.. What's your key(s) on your table?
For the update, what fields do you want to update (I would also need the key(s) for the table as well).
Esssentially a delete is similar to the select..
DELETE FROM <table name> WHERE <key column name> = <data>
Updates are: UPDATE <table name> SET <column name> = variable data , <column name2> = variable data2, etc WHERE <key column name> = <condition data>
You would plug these in where you had your SELECT * command earlier and run the same set of code..
Yes, your delete statement is not correct. You need value in there unless you are looking for an empty string... and if you are looking for an empty string the format is two apostrophies ('') and not two double quotes.
My guess is you want to delete a specific record, right?
CODE
"DELETE FROM Games WHERE GameID = " + textbox_holding_gameID.text
What that accomplishes is getting the actual VALUE you want into the delete statement instead of the variable name.
Yes, your delete statement is not correct. You need value in there unless you are looking for an empty string... and if you are looking for an empty string the format is two apostrophies ('') and not two double quotes.
My guess is you want to delete a specific record, right?
CODE
"DELETE FROM Games WHERE GameID = " + textbox_holding_gameID.text
What that accomplishes is getting the actual VALUE you want into the delete statement instead of the variable name.
Ok now i understand. I got it to work thank you all so much for your help.