• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Getting data from a data table in C#

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I currently have a program that will allow for the user to login with a username and password. I am using a data table that has username, password and name as columns. I am trying to get the name to be display on the main form once I have successfully logged in. How would I get the username from the Name column? The code I have that allows for the user to login is:
 
Bartender
Posts: 1381
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are executing a SELECT COUNT(*) on a Login table (which, I suppose, holds users' credentials), to verify if the provided userid and password are corrected. That's ok (honestly, i'd useto verify if credentials are valid; dt.Rows[0][0].ToString() == "1" looks pretty ugly). To retrieve other info (e.g, the Name column values) you have to change your SQL statement (for example, a SELECT * FROM Login).
If row count == 1 you can write:



Hope this helps.
 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I noticed something with your query here:

Just so you know, concatenating strings in an SQL query leaves you open to SQL injection vulnerabilities.  It's a good idea to use parameterized queries and other methods to avoid this, particularly when dealing with public facing systems.  Here's more info on SQL injection prevention.  I've written out the equivalent of your query using parameters for your reference.  

Hope this helps.  Happy coding!



 
Sheriff
Posts: 28347
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Eric, and thanks for the informative post!
 
EricJ Davis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Happy to be here!
 
So you made a portal in time and started grabbing people. This tiny ad thinks that's rude:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic