7 Replies - 531 Views - Last Post: 24 August 2013 - 12:07 PM Rate Topic: -----

#1 Nigels  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 17-May 13

Application connecting to database with SQL

Posted 13 August 2013 - 01:11 PM

I have a login system so users login and the person that logs in has there username displayed in a label. I then use sql to read the label and get a users details from the database. This works fine, I use the sql statement below to achieve this.

sql = "SELECT * FROM tblContacts Where UserName='" & UserNameLoggedInLabel.Text & "'"


How do I write a sql to overwrite data in my database that is being displayed in a textbox on the same form that has the label above. And have it overwrite info for the correct user.

So my table is called tblContacts the column I want to update is called UserScore1. I just want to press

a button and have that textbox overwrite the data in my database.

sql = "INSERT INTO tblContacts (UserScore1) Where UserScore1='" & TextBox1.Text & "'"



Thanks very much.

Slight edit here

I understand that I am not referencing the label like I was to get the information. The problem I suppose is I do not know how to reference the label to get the details and at the same time update the TextBox. I am only assuming this has to be done in SQL or can I use some VB.NET code here to achieve this.

Is This A Good Question/Topic? 0
  • +

Replies To: Application connecting to database with SQL

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Application connecting to database with SQL

Posted 13 August 2013 - 01:20 PM

I've removed the term VB.NET from your topic title - it is redundant.

Your description is confusing. You talk about a label, but your eventual question is about updating a table from the content of a TextBox. In which case you need to use an SQL-UPDATE statement to update an existing record in a table. (I don't usually reference w3schools, but the SQL section is pretty straight-forward.)

sql = "UPDATE tblContacts SET UserScore1='" & TextBox1.Text & "' WHERE somecondition"

If your intention is to update a record in a table then it requires SQL. You cannot do this with VB.NET code alone. (How would you?! :dontgetit: Well, it could be done with data-binding, without writing a SQL-statement directly, but that is a different story.)

This post has been edited by andrewsw: 13 August 2013 - 01:25 PM

Was This Post Helpful? 1
  • +
  • -

#3 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9098
  • View blog
  • Posts: 34,184
  • Joined: 12-June 08

Re: Application connecting to database with SQL

Posted 13 August 2013 - 01:22 PM

You would use an UPDATE statement. Preferably you would use the row's unique key (id) to only update one row.. but I am not sure how you have your table setup. Also don't concatenate your SQL statements use parameters (prevents a bulk of bad input and increases readability). Also SELECT * is not the best way.. enumerate all your columns you need to a. cut down on excess data you may not need, and b. prevent having to deal with errors from adding new columns to the table and your code is trying to handle them.
Was This Post Helpful? 1
  • +
  • -

#4 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,521
  • Joined: 08-April 09

Re: Application connecting to database with SQL

Posted 13 August 2013 - 01:31 PM

sql = "SELECT * FROM tblContacts Where UserName='" & UserNameLoggedInLabel.Text & "'"

You must unlearn what you have learned, this is very bad practice for beginners and advanced users.
Constructing sql strings like this will lead to sql injections security flaws. I'm gonna advocate against that where ever i see this.

This post has been edited by Michael26: 13 August 2013 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 Nigels  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 17-May 13

Re: Application connecting to database with SQL

Posted 13 August 2013 - 02:08 PM

View Postandrewsw, on 13 August 2013 - 01:20 PM, said:

Your description is confusing.


Hi,
I have a login system so users login and the person that logs in has there username displayed in a label. I then use sql to read the label and get a users details from the database and display there database details in textboxes. The database is quite simple it consists of
Identifier, UserName, UserPassword, UserTimer, UserScore

each one has it's details put in a textbox of it's own.
This works fine, I use the sql statement below to achieve this.

1 sql = "SELECT * FROM tblContacts Where UserName='" & UserNameLoggedInLabel.Text & "'"


Al I want to do is edit the textbox which says "UserScore" and update that one textbox to the database.
So if I can use a label that has a logged in users name on it to get the rest of there details from the database and display them in textboxes. How do I use SQL to read the label, get the users name, and update that one textbox.
I thought it might of been something like below. But as someone has just said I need to use the "Update" keyword.
But I just want smeone to show me how it should be done.

And yes I will look at sql injections at some point as well.

1 sql = "INSERT INTO tblContacts (UserScore1) Where UserScore1='" & TextBox1.Text & "'"

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Application connecting to database with SQL

Posted 13 August 2013 - 02:38 PM

Did you read the link I provided, or the example SQL statement I posted?

This post has been edited by andrewsw: 13 August 2013 - 02:39 PM

Was This Post Helpful? 0
  • +
  • -

#7 Nigels  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 17-May 13

Re: Application connecting to database with SQL

Posted 24 August 2013 - 10:04 AM

Yes I did read the link you provided, thanks.
It is now working as it should. When I first started learning VB.NET I was not expecting to have learned SQL at the same time. It seems they go hand in hand with a lot of tutorials I find on the net.
Anyway thanks for everyone's perseverance.
Was This Post Helpful? 1
  • +
  • -

#8 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Application connecting to database with SQL

Posted 24 August 2013 - 12:07 PM

All the important fundamentals of VB.NET (or any language for that matter) can, and perhaps should, be learnt without requiring a database. See the tutorial linked in my signature, for example.

However, at some stage storing data permanently (between sessions) will be necessary. (There are very few applications that don't require some means of storing data.) Using, and understanding, a database, SQL and database design then becomes necessary.

This said, it is possible to store data in text or XML files (among other formats) but, eventually, more structured data needs to be stored, and retrieved, requiring database knowledge.

Glad you are making progress though :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1