16299

Get specific column of specific user in datatable

<h3>Question</h3>

I have a data table, in the data table, I have 3 columns: USERNAME, PASSWORD, and Money.

I want to make a specific setting in my program to make a label the amount of money the user has, and for each user, it will show something else depending on who is logged on at the moment, is it possible and if yes how can I do it?

Thanks in advance.


<h3>Answer1:</h3>

You can put the amount of money in your label by writing some code. This is used for MSSQL Database connections(in your case). Here is an example:

using System.Data.SqlClient;//Add this in the using statements at the top of your code. using (SqlConnection conn = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename =" +Application.StartupPath+ @"\Database1.mdf; Integrated Security = True")) { conn.Open(); using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = conn; cmd.CommandText = "SELECT Money FROM Users WHERE Username = @username"; cmd.Parameters.AddWithValue("@username", "User1"); var reader = cmd.ExecuteReader(); reader.Read(); label1.Text = reader.GetValue(0).ToString(); //reader returns an object, you have to convert it in your type. //GetValue(selected column number) } conn.Close();//This line is optional. The connection closes automatically when the using() statement ends. }

You can add this code in your Form Load event.(If you are working with Win Forms).

The second method is to use Dataset with binding the label to it. This is also powerful and you do not need to know too much sql or how to code, it is more complicated at the beginning but it`s easier and saves time. You can apply it to any of your form elements(buttons, datagridviews,combobox,textbox,etc).

First, go to your label properties and find "DataBindings". Click on advanced. Just click next until you see the connect to database option. If your are connected already with visual studio to your database it will appear in that combobox, otherwise click new connection(I suppose you worked in service based database).Click next and finish. After you have to bind the label to the database(it will create a generated code in your Form Load Event). If you have only one record(one user) in the table it will show only one value, but if you want to show a specific user, you can change that "Fill" generated method in your Form Load Event in other(filtered one with WHERE SQL Clause). You can change that fill method in the Dataset Added in the bottom of your designer of the form. Click on that little arrow near it and choose "edit in designer" option. Click on the table adapter section and right click on his function(in this case Fill() method) and click configure. Here you can change the sql statement and put a WHERE clause in the end.(ex Where Username = ?) The "?" means some variable. After pass in the function created in the form load event your user`s username next to that dataset thing. Done. If you want to work with Win Forms and sql databases I advise you to learn how to use The Datasets, Bindings and TableAdapters. Hope it helps. Screenshots of my explanations:

!!!! [UPDATE] !!!! Here is my example program on google drive: Link. On the right side you can open my service based database(in the project files(Database1)).I'll attach some useful screenshots for creating addition functions in a dataset's table adaper. Also, you have the second method commented in the Form1 load event.

来源:https://stackoverflow.com/questions/60176742/get-specific-column-of-specific-user-in-datatable

Recommend

  • Coldfusion: CFHTTP with SSL encrypted Page (https://) - got an error
  • Android Application in Gujarati font
  • Multiple increment operators on the same line Python
  • Pytorch RuntimeError: “host_softmax” not implemented for 'torch.cuda.LongTensor'
  • Electron - How to know when renderer window is ready
  • How do you use typed errors in async catch()
  • Updating value of TextBlock in wpf dynamically [Sovled]
  • How to exclude devices with lower GPU in Android? [duplicate]
  • updating an image in an image control
  • Can we implement some code that fires upon selecting something in google document?
  • Guava: How does Cacheloader.load() work
  • Where can I find tomesh.c for windows?
  • Running Applescript from a Cocoa application
  • Quick method to enumerate two big arrays?
  • Tomcat Replacing VM arguments
  • Customize jQuery submenu positioning
  • jsx command not found on mac terminal
  • Is it possible to add a hyperlink to a UIAlertController?
  • Limit number of button clicks
  • Slice assignment to tensorflow variable
  • What does “T extends Junk” mean in a generic class in Java?
  • How do I use libcurl to printf a remote FTP directory listing?
  • Year over Year Stats from a Crossfilter Dataset
  • How to define something in JavaScript [closed]
  • Typeahead.js does give me suggestions but doesn't select them
  • Calculate time from document
  • Neo4j…how to get a visual representation of my data?
  • How to include associated objects using gon in Rails/jQuery
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • How to clear a browser cache in Protractor
  • jQuery scrollTop if URL has hash
  • How to decleare char *const argv[] in swift [duplicate]
  • Background transfer download task failed when app was closed
  • Computing the discrete fourier transform of audio data with FFTW
  • Grails - How to implement a foreign key relationship not using an id column?
  • VLOOKUP in IMPORTRANGE
  • XEP-0166: Jingle protocol implementation for voice/video chat in iOS
  • Firebase: How to read from external DB?
  • ReferenceError: TextEncoder is not defined