1

I want to show the result in Label, I wrote a query by which I am getting some records, I want to assign that records based on the Name.

I have 5 label: A, B , C, D, E in these label I want to show the records.

Query:

select s.Name, count(1)as Records from tbl_Case tc
inner join tbl_subcase ts on ts.id = tc.Id
inner join tbl_supercase tsc on tsc.id = tc.supercaseid
inner join course c on c.id = b.courseid
where s.isvalid = 1 group by s.Name

From the query suppose I am getting A = 4, B = 10, c = 2, E= 100 and I did not get the record for D. so I want to show in label as 4 Records, 10 Records, 2 Records, No Records Found, 100 Records.

Please some one tell me how could I assign records in label from Code Behind.

2
  • 1
    Have you written any code to execute the query from the code behind yet? The answer will depend on how you are executing the query from code i.e. whether you are returning a DataReader or using a DataAdapter to return a DataSet so if there is code already written to execute the query can you post it please? Commented Jun 22, 2012 at 9:22
  • @AndyM: I am using DataSet.. ] Commented Jun 25, 2012 at 8:25

2 Answers 2

2
lblA.Text = lblB.Text = ... = "No records found";
using (var con = new SqlConnection("Data Source=myServerAddress;" +
        "Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"))
{
    con.Open();
    var com = con.CreateCommand();
    com.CommandType = CommandType.Text;
    com.CommandText = @"
        select s.Name, count(1)as Records from tbl_Case tc
        inner join tbl_subcase ts on ts.id = tc.Id
        inner join tbl_supercase tsc on tsc.id = tc.supercaseid
        inner join course c on c.id = b.courseid
        where s.isvalid = 1 group by s.Name";
    using (var read = com.ExecuteReader())
    {
        while (read.Read())
        {
            if (read["Name"] as string == "A")
                lblA.Text = Convert.ToString(read["tc"]);
            else if (read["Name"] as string == "B")
                lblA.Text = Convert.ToString(read["tc"]);
            ...
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Are you saying you want to know how to match the label control id in code behind against the name returned by the query. If so, you can use

Page.FindControl()

or

Control.FindControl()

to match the correct control as you iterate through the returned data. You will need to add some extra code to handle the missing record(s) (i.e. D in your example). Your code behind will need to know how many records there could be (i.e. maybe a simple for loop to iterate the required number of times).

http://msdn.microsoft.com/en-us/library/31hxzsdw.aspx

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.