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...;)

5
  • Maybe you can try to show in code what you really want to achieve. Sep 3, 2012 at 6:47
  • Have u managed to get the result from SQL query? Also please share your C# code.
    – ssilas777
    Sep 3, 2012 at 6:49
  • Hello @Tomas, thanks for help.. For you information, I don`t know to much to write code in c#.. Can I use dataReader to solve this problem? Hello @Arkain, thanks for your respons. Im still newbie in C#, can you explain me what function for ADO.Net, LINQ and ENTITY Framework? Thanks for help...:) Hello @ssilas777, ok i will share... Thanks again...;) Sep 3, 2012 at 6:53
  • Is this the same question as this? Sep 3, 2012 at 7:59
  • @FrancescoBaruchelli , Actually is same.. hehe... Sep 3, 2012 at 8:03

8 Answers 8

2
        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;

1
  • superb answer Gregor, it helped me too
    – hud
    Jun 14, 2016 at 8:42
1

you need to read the result via a SQLDataReader

SQLCommand command = new SQLCommand("your sql string here");
SQLDataReader reader = command.executeReader();
while(reader.read())
{
 set your label values here with reader["cloumn"]
}
4
  • Like this? labelName.Text=["tbl_staff.staffName"]; Sep 3, 2012 at 7:01
  • +1, I thought to write the exact code. But because I didn't know how many rows query has to return I haven't posted. Because if the are several rows, what row should be used to get column value. On the other hand you may use ExecuteScalar method if there will be one value.
    – seeker
    Sep 3, 2012 at 7:02
  • @seeker. Query result just return one rows only.. hehe... Thanks for help my friend...:) Sep 3, 2012 at 7:04
  • @chuki2 like this: labelName.Text=reader["tbl_staff.staffName"];
    – seeker
    Sep 3, 2012 at 7:04
0

You can read content of the row(s) that query returned using DataReader class. It has methods to get single value, or you can iterate for each row. Tell me how many rows youR query returns so I can provide the exact code.

0
0

Check this one - Also set your connection string along with this code

SQLCommand command = new SQLCommand();
command.CommandText = " 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 ";

SQLDataReader reader = command.executeReader();

while(reader.read())
{
 labelName.Text = reader.GetString(0);
 labelDepartment.Text = reader.GetString(1);
}
0
0
//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"].ToString();
    labelDepartment.Text = reader["tbl_department.department"].ToString();
}

Convert Type is what you left in your code....

0

Try Changing only This

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"].toString();
    labelDepartment.Text = reader["tbl_department.department"].toString();
}`
0

One way to do:

private void getData()
{
    DataTable dt = new DataTable();
    SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");
    connection.Open();
    SqlCommand sqlCmd = new SqlCommand(" 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", connection);
    SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

    sqlCmd.Parameters.AddWithValue("@username",user);
    sqlDa.Fill(dt);
    if (dt.Rows.Count > 0)
    {
           lable.Text = dt.Rows[0]["staffName"].ToString(); //Where "staffName" is ColumnName 

    }
        connection.Close();
}

Suggest to use a stored procedure not in line query to avoid SQL injection attacks.

stored procedure example

0
-2

Populate the Dataset with your Query and then retrieve values from Dataset and assign it to your label.

Lets say your Dataset is DS then:

labelName.Text=DS.Tables[0]["tbl_staff.staffName"].tostring();
labelDepartment.Text=DS.Tables[0]["tbl_department.department"].tostring();

Hope this helps.

2
  • What are you using as Backend MSACCESS or SQL?
    – Rohit
    Sep 3, 2012 at 6:57
  • Sorry.. Im use MS SQL 2008..;) By the way, i will try first. Anything I will ask you again.. Thanks for help Rohit...;) Sep 3, 2012 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.