Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-05-2012, 05:41 PM   PM User | #1
Crystalz
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Crystalz is an unknown quantity at this point
Visual Basic Problem

Hello, i'm trying to make my program change a value in a database.
I've got a database called Account, and a Table called Accounts. I have 4 columns in this table. Id(Key column), UserName, Password and Adminpower.
What I am trying to do is to make a button change a value in that table. What I do not know is how to make the program know which row i want to change the value in.


Code:
    Private Sub btnAdmin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdmin.Click
        Dim cmd As SqlCommand
        Dim reader As SqlDataReader
        con = New SqlConnection(_connectionString)
        cmd = con.CreateCommand()
        con.Open()
        cmd.CommandText = "ALTER Accounts ('Adminpower') VALUES('1')"
        reader = cmd.ExecuteReader()
        If Not reader.HasRows Then
            MessageBox.Show("Something is wrong.")
        End If
    End Sub
That's the code so far for the button. So what I want it to do is to change the Adminpower column from 0 to 1 when pressing that button. Yes I have already connected it to the database.
When I try to press the button I get an error saying that the table doesn't exist.

Any help is appreciated.
Thanks


Screenshots:




EDIT!!!

Problem solved. I got it working, I'll keep all the text here if anyone else has a similar problem. And this is what I used:

Code:
UPDATE Accounts SET Adminpower = 1 WHERE '" & TextBox1.Text & "' = ""UserName"" AND '" & TextBox2.Text & "' = ""Password"""

Last edited by Crystalz; 10-05-2012 at 09:16 PM..
Crystalz is offline   Reply With Quote
Old 10-05-2012, 06:53 PM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
Syntax may be a little bit off....
Code:
dim _user as String = txt_UserName.Text
dim cmdstr as String = String.Format("UPDATE Accounts SET Adminpower = 1 WHERE username = '{0}'", _user)


Try
dim connstr as String = "" ' enter you connection string
dim conn as new SqlConnection(connstr)
dim cmd as new SqlCommand(cmdstr, conn)
cmd.CommandType = CommandType.Text

conn.Open()
cmd.ExecuteNonQuery()
conn.Close()

MessageBox.Show("Updated successfully!", "Results", MessageBoxButtons.OK) 

Catch sX as SqlException
MessageBox.Show("There was a SQL Error!", "Results", MessageBoxButtons.OK) 

Catch eX as Exception
MessageBox.Show("There was an Error!", "Results", MessageBoxButtons.OK) 

End Try
obviously this is quick and dirty, but you should be able to build off of this.
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Old 10-05-2012, 09:17 PM   PM User | #3
Crystalz
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Crystalz is an unknown quantity at this point
Quote:
Originally Posted by alykins View Post
Syntax may be a little bit off....
Code:
dim _user as String = txt_UserName.Text
dim cmdstr as String = String.Format("UPDATE Accounts SET Adminpower = 1 WHERE username = '{0}'", _user)


Try
dim connstr as String = "" ' enter you connection string
dim conn as new SqlConnection(connstr)
dim cmd as new SqlCommand(cmdstr, conn)
cmd.CommandType = CommandType.Text

conn.Open()
cmd.ExecuteNonQuery()
conn.Close()

MessageBox.Show("Updated successfully!", "Results", MessageBoxButtons.OK) 

Catch sX as SqlException
MessageBox.Show("There was a SQL Error!", "Results", MessageBoxButtons.OK) 

Catch eX as Exception
MessageBox.Show("There was an Error!", "Results", MessageBoxButtons.OK) 

End Try
obviously this is quick and dirty, but you should be able to build off of this.
I've got it working already, but I do appreciate it though. This might help someone else with the same question.
Crystalz is offline   Reply With Quote
Reply

Bookmarks

Tags
visual basic

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:18 PM.


Advertisement
Log in to turn off these ads.