Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

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!




Confused on DELETE and UPDATE Commands

 
Reply to this topicStart new topic

Confused on DELETE and UPDATE Commands

lolwtf
14 Aug, 2008 - 09:04 PM
Post #1

New D.I.C Head
*

Joined: 14 Aug, 2008
Posts: 8

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


        End If


        If problem = False Then

            DBAGame.InsertCommand.CommandText = "INSERT INTO Games (GameName, GameGenre, GameSystem, GameYear, GameDeveloper) VALUES ('" + txtName.Text + "', '" + txtGenre.Text + "', '" + txtSystem.Text + "', '" + txtRelease.Text + "', '" + txtDeveloper.Text + "');"
            DBAGame.InsertCommand.ExecuteNonQuery()

            clear()
            updatelst()
        End If

    End Sub

    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

    End Sub
End Class



Any help is appreciated!

User is offlineProfile CardPM
+Quote Post

modi123_1
RE: Confused On DELETE And UPDATE Commands
15 Aug, 2008 - 06:26 AM
Post #2

D.I.C Addict
Group Icon

Joined: 12 Jun, 2008
Posts: 531



Thanked: 13 times
Dream Kudos: 100
My Contributions
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?
User is offlineProfile CardPM
+Quote Post

lolwtf
RE: Confused On DELETE And UPDATE Commands
15 Aug, 2008 - 10:19 PM
Post #3

New D.I.C Head
*

Joined: 14 Aug, 2008
Posts: 8

QUOTE(modi123_1 @ 15 Aug, 2008 - 07:26 AM) *

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 smile.gif
User is offlineProfile CardPM
+Quote Post

modi123_1
RE: Confused On DELETE And UPDATE Commands
15 Aug, 2008 - 10:38 PM
Post #4

D.I.C Addict
Group Icon

Joined: 12 Jun, 2008
Posts: 531



Thanked: 13 times
Dream Kudos: 100
My Contributions
QUOTE(lolwtf @ 16 Aug, 2008 - 01:19 AM) *

QUOTE(modi123_1 @ 15 Aug, 2008 - 07:26 AM) *

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 smile.gif


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..
User is offlineProfile CardPM
+Quote Post

dineeshd
RE: Confused On DELETE And UPDATE Commands
16 Aug, 2008 - 02:09 AM
Post #5

D.I.C Addict
Group Icon

Joined: 30 Jun, 2008
Posts: 578



Thanked: 16 times
Dream Kudos: 575
My Contributions
You can refer this link for some help -- > Click Here
User is offlineProfile CardPM
+Quote Post

lolwtf
RE: Confused On DELETE And UPDATE Commands
16 Aug, 2008 - 05:25 PM
Post #6

New D.I.C Head
*

Joined: 14 Aug, 2008
Posts: 8

QUOTE(modi123_1 @ 15 Aug, 2008 - 11:38 PM) *

QUOTE(lolwtf @ 16 Aug, 2008 - 01:19 AM) *

QUOTE(modi123_1 @ 15 Aug, 2008 - 07:26 AM) *

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 smile.gif


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..





My key is GameID, and i have these columms:

GameName, GameGenre, GameSystem, GameYear, GameDeveloper.
User is offlineProfile CardPM
+Quote Post

lolwtf
RE: Confused On DELETE And UPDATE Commands
21 Aug, 2008 - 11:41 PM
Post #7

New D.I.C Head
*

Joined: 14 Aug, 2008
Posts: 8

Damn i still cant get it to work. Is there something wrong with my SQL statements?
User is offlineProfile CardPM
+Quote Post

thava
RE: Confused On DELETE And UPDATE Commands
22 Aug, 2008 - 01:27 AM
Post #8

D.I.C Regular
Group Icon

Joined: 17 Apr, 2007
Posts: 450



Thanked: 18 times
Dream Kudos: 50
My Contributions

be cool
the answer is in your hand?
if you use the sql command builder
try this link
http://msdn.microsoft.com/en-us/library/sy...andbuilder.aspx
User is offlineProfile CardPM
+Quote Post

modi123_1
RE: Confused On DELETE And UPDATE Commands
22 Aug, 2008 - 08:43 AM
Post #9

D.I.C Addict
Group Icon

Joined: 12 Jun, 2008
Posts: 531



Thanked: 13 times
Dream Kudos: 100
My Contributions
QUOTE(lolwtf @ 22 Aug, 2008 - 02:41 AM) *

Damn i still cant get it to work. Is there something wrong with my SQL statements?


What sort of error message are you getting or actions that are happening?
User is offlineProfile CardPM
+Quote Post

lolwtf
RE: Confused On DELETE And UPDATE Commands
24 Aug, 2008 - 10:25 PM
Post #10

New D.I.C Head
*

Joined: 14 Aug, 2008
Posts: 8

QUOTE(modi123_1 @ 22 Aug, 2008 - 09:43 AM) *

QUOTE(lolwtf @ 22 Aug, 2008 - 02:41 AM) *

Damn i still cant get it to work. Is there something wrong with my SQL statements?


What sort of error message are you getting or actions that are happening?




Ok heres the Error im getting:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll

Additional information: System error.


Here is the code that im using for the DELETE button

CODE
DBAGame.DeleteCommand.CommandText = "DELETE FROM Games WHERE GameID = """
        DBAGame.DeleteCommand.ExecuteNonQuery()



I don't if my SQL statement is right to be quite honest. Here is the table data from my SQL server:


My key is GameID, and i have these columms:

GameName, GameGenre, GameSystem, GameYear, GameDeveloper.


Im so confused crazy.gif
User is offlineProfile CardPM
+Quote Post

modi123_1
RE: Confused On DELETE And UPDATE Commands
25 Aug, 2008 - 10:35 AM
Post #11

D.I.C Addict
Group Icon

Joined: 12 Jun, 2008
Posts: 531



Thanked: 13 times
Dream Kudos: 100
My Contributions
QUOTE(lolwtf @ 25 Aug, 2008 - 01:25 AM) *

QUOTE(modi123_1 @ 22 Aug, 2008 - 09:43 AM) *

QUOTE(lolwtf @ 22 Aug, 2008 - 02:41 AM) *

Damn i still cant get it to work. Is there something wrong with my SQL statements?


What sort of error message are you getting or actions that are happening?




Ok heres the Error im getting:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll

Additional information: System error.


Here is the code that im using for the DELETE button

CODE
DBAGame.DeleteCommand.CommandText = "DELETE FROM Games WHERE GameID = """
        DBAGame.DeleteCommand.ExecuteNonQuery()



I don't if my SQL statement is right to be quite honest. Here is the table data from my SQL server:


My key is GameID, and i have these columms:

GameName, GameGenre, GameSystem, GameYear, GameDeveloper.


Im so confused crazy.gif


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.
User is offlineProfile CardPM
+Quote Post

lolwtf
RE: Confused On DELETE And UPDATE Commands
25 Aug, 2008 - 10:29 PM
Post #12

New D.I.C Head
*

Joined: 14 Aug, 2008
Posts: 8

QUOTE(modi123_1 @ 25 Aug, 2008 - 11:35 AM) *

QUOTE(lolwtf @ 25 Aug, 2008 - 01:25 AM) *

QUOTE(modi123_1 @ 22 Aug, 2008 - 09:43 AM) *

QUOTE(lolwtf @ 22 Aug, 2008 - 02:41 AM) *

Damn i still cant get it to work. Is there something wrong with my SQL statements?


What sort of error message are you getting or actions that are happening?




Ok heres the Error im getting:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll

Additional information: System error.


Here is the code that im using for the DELETE button

CODE
DBAGame.DeleteCommand.CommandText = "DELETE FROM Games WHERE GameID = """
        DBAGame.DeleteCommand.ExecuteNonQuery()



I don't if my SQL statement is right to be quite honest. Here is the table data from my SQL server:


My key is GameID, and i have these columms:

GameName, GameGenre, GameSystem, GameYear, GameDeveloper.


Im so confused crazy.gif


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. biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:45PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month