0

I have a SQL output like

TotalLeave    Status
----------    ------
    3        PaidLeave
    5        MedicalLave

and I need to show this value in my label controls like,

lblMedicalLeave.text = 5
lblPaidLeave.text    = 3

for this I just created objects for my dataset in my code like,

StaffAttendanceStatusTableAdapters.StaffTypesTableAdapter staffAttendanceStatus = 
                  new StaffAttendanceStatusTableAdapters.StaffTypesTableAdapter();
StaffAttendanceStatus.StaffTypesDataTable StaffDataTable = 
               staffAttendanceStatus.GetDataStaffAttendanceStatus(staff.StaffId);

        if (StaffDataTable[0] != null)
        {
            StaffAttendanceStatus.StaffTypesRow StaffRow = StaffDataTable[0];

            lblTotalMedicalLeave.Text = StaffRow.TotalLeave.ToString();
            lblTotalPaidLeave.Text = StaffRow.TotalLeave.ToString();
        }

its showing the same value(3), is it possible to get the TotalLeave value for corresponding Status? can anyone help me here

2
  • 1
    You are accessing the same row both times. Use StaffDataTable[1] to access the second row. Anyway you should check if there is a result before access any values! Commented May 21, 2012 at 5:01
  • exactly, I understood my mistake, Y don't u post this in answer session, so that I can mark it as answer Commented May 21, 2012 at 5:30

2 Answers 2

2

You are accessing the same row both times. Use StaffDataTable[1] to access the second row. Anyway you should check if there is a result before access any values!

using StaffAttendanceStatusTableAdapters;
....


StaffTypesTableAdapter staffAttendanceStatus = new StaffTypesTableAdapter();
StaffAttendanceStatus.StaffTypesDataTable StaffDataTable = 
           staffAttendanceStatus.GetDataStaffAttendanceStatus(staff.StaffId);

if (StaffDataTable != null && StaffDataTable.Count > 1)
{
    lblTotalMedicalLeave.Text = StaffDataTable[0].TotalLeave.ToString();
    lblTotalPaidLeave.Text = StaffDataTable[1].TotalLeave.ToString();
}

hth

Sign up to request clarification or add additional context in comments.

Comments

1

Since you need to get TotalLeave from two rows, you need to fetch data from two rows.

if (StaffDataTable != null && StaffDataTable.Rows.Count > 1)
{
  StaffAttendanceStatus.StaffTypesRow StaffRow1 = StaffDataTable[0];
  StaffAttendanceStatus.StaffTypesRow StaffRow2 = StaffDataTable[1];

  lblTotalMedicalLeave.Text = StaffRow1.TotalLeave.ToString();
  lblTotalPaidLeave.Text = StaffRow2.TotalLeave.ToString();
}

if there is no order of PaidLeave and MedicalLave status, just check row.Status and assign total value to corresponding label

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.