1

Working on a project on C# want to fetch emp_id of all workers working on site_id 8 and show it on form. What I have done is

SqlCommand cmd3 = new SqlCommand("Select emp_id from employee_attendance where site_id=8",conn); 
SqlDataReader reader = cmd3.ExecuteReader();
while(reader.Read())
{
int emp_id =Convert.ToString(reader[0]));
label7.Text = Convert.ToString(emp_id);
}

it is showing only one result however i know there are 2 employees match the query

2
  • 1
    int emp_id = Convert.ToString(reader[0])); does not make much sense. Commented Jun 14, 2012 at 16:23
  • 3
    What make less sense is that all the answers keep it in Commented Jun 14, 2012 at 16:28

5 Answers 5

4

You're going to always show the last, since you overwrite the label each time. You could do something like:

StringBuilder builder = new StringBuilder();
while(reader.Read())
{
    builder.Append(reader[0]);
    builder.Append(" ");
}
label7.Text = builder.ToString();

That will create one long string with all of the IDs appended on each other (and a trailing space). If you need to remove the trailing space, you could also just Trim the resulting string, as well:

builder.Remove(builder.Length-1, 1);
label7.Text = builder.ToString();
Sign up to request clarification or add additional context in comments.

2 Comments

Great answer! Much better to use the stringbuilder.
why save to int? int emp_id =Convert.ToString(reader[0])); should be string emp_id = reader[0].ToString()
2

It is showing only one id because you are always overwriting label value:

label7.Text = Convert.ToString(emp_id);

You need to concatenate it:

SqlCommand cmd3 = new SqlCommand("Select emp_id from employee_attendance where site_id=8",conn); 
SqlDataReader reader = cmd3.ExecuteReader();
StringBuilder sb = new StringBuilder();

while(reader.Read())
{
    int emp_id =Convert.ToString(reader[0]));
    sb.Append(Convert.ToString(emp_id) + ", ");
}

label7.Text = sb.ToString();

Comments

2

This is because your next result overrides the previous. You can use something like a StringBuilder class to concatenate emp_id

var cmd3 = new SqlCommand(
    "Select emp_id from employee_attendance where site_id=8",conn); 
var reader = cmd3.ExecuteReader();
var sb = new StringBuilder();
while(reader.Read())
{
    int emp_id =Convert.ToString(reader[0]));
    sb.Append(emp_id + ", ");   
}
label7.Text = sb.ToString();

Comments

1

You are replacing the value in label7.Text every iteration of the loop, so it will only show you the last item. You can either concatenate the strings together as part of the loop if that suits your needs or find another way to represent it other than a label text.

Comments

1

This code overwrites both the id, and the label7 text.

while(reader.Read())
{
    int emp_id =Convert.ToString(reader[0]));
    label7.Text = Convert.ToString(emp_id);
}

To display both, append, the value to one or the other, like below.

label7.Text = "";
while(reader.Read())
{
    label7.Text += reader[0] += " ";
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.