Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8497003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:00:19+00:00 2026-06-11T00:00:19+00:00

How can i take value from query result to label? I have two label,

  • 0

How can i take value from query result to label?

I have two label, one is labelName and one more is labelDepartment

So when i run the query, how can i get value from query result and assign it to label using c#?

This is my Sql Command:

  "SELECT tbl_staff.staffName,tbl_department.department 
    FROM tbl_staff,tbl_logs,tbl_department 
    WHERE tbl_staff.userID = tbl_logs." + listStaff.SelectedValue + " and tbl_staff.idDepartment = tbl_department.idDepartment;"

This is current code in C#

//Open SQL connection

SqlConnection openCon = new SqlConnection(connString);
openCon.Open();

string SQL = string.Format("SELECT tbl_staff.staffName,tbl_department.department FROM tbl_staff,tbl_logs,tbl_department WHERE tbl_staff.userID = tbl_logs.userID and tbl_staff.idDepartment = tbl_department.idDepartment" + listStaff.SelectedValue + ";");


SqlCommand command = new SqlCommand(SQL);
SqlDataReader reader = command.ExecuteReader();

while(reader.Read())
{
    labelName.Text = reader["tbl_staff.staffName"];
    labelDepartment.Text = reader["tbl_department.department"];
}

Note : Our record will return one row only..
MS SQL and C#.. thanks for help…;)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:00:21+00:00Added an answer on June 11, 2026 at 12:00 am
            string name = null;
            string department = null;
            string listStaff = "MylistStaff";
    
            string sql =  "SELECT tbl_staff.staffName,tbl_department.department " +
                "FROM tbl_staff,tbl_logs,tbl_department " +
                "WHERE tbl_staff.userID = tbl_logs." + listStaff + " and tbl_staff.idDepartment = tbl_department.idDepartment;";
            //change this connection string... visit www.connectionstrings.com
            string connString = "Server=localhost; Database=myDatabaseName; Trusted_Connection=Yes";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                using (SqlCommand command = new SqlCommand(sql,conn))
                {
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        name = reader[0] as string;
                        department = reader[1] as string;
                        //break for single row or you can continue if you have multiple rows...
                        break;
                    }
                }
                conn.Close();
            }
    

    department and listStaff can then easily be applied to label text like:

    DepartmentLabel.Text = department;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to take a value from a query in one function and use
Is there a way using php to take a MYSQL query result, get a
can i get validation on weight which can take decimal values and also integer.
xml2js to take my xml result so that i can access the values. I
Is there anyway we can take input from command line in Objective-C, the way
I have a Python3 script that can take optional command line parameters which the
I have a long running method which can take a minute. On my local,
I have the following MySQL query: SELECT value_1, ( SELECT value_4 FROM table_1 WHERE
I have a query which gives me result of my sort the query looks
I have the following SQL: SELECT `table1`.`value`, `table2`.* FROM `table2` INNER JOIN `table1` ON

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.