Click here to Skip to main content
15,881,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
ad = new SqlDataAdapter("select * from ShiftAttend where Emp_Code='" + TextBox1.Text + "' and Monthof='" + Label6.Text + "'  ", con);
        DataTable dt3 = new DataTable();
        ad.Fill(dt3);
        if (dt3.Rows.Count > 0)
        {
            for (int i = 1; i <= Convert.ToInt32(Label40.Text); i++)
            {

                if (Convert.ToInt32(dt3.Rows[0][2]) == i)
                    {

                        var label = ((Label)FindControl("OT" + i));

                        label.Text = Convert.ToString(Convert.ToInt32(dt3.Rows[0][5])+Convert.ToInt32(dt3.Rows[0][8])+Convert.ToInt32(dt3.Rows[0][11]));
                        
                    }
            
            }
        }
Posted
Updated 24-Nov-13 0:03am
v2

Well, yes - it will.
C#
label.Text = Convert.ToString(Convert.ToInt32(dt3.Rows[0][5])+Convert.ToInt32(dt3.Rows[0][8])+Convert.ToInt32(dt3.Rows[0][11]));

You always access the same row - row zero...

But, please, don't do things like that!
Firstly, Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Secondly, don't use SELECT * FROM ... as a query, and then:
1) Ignore most of the values. It is very inefficient and wastes your SQL server bandwidth.
2) Use numeric indexes to access columns. If your DB changes, your code displays the wrong data, or falls over - and it may be that nobody notices for ages after the damage was done...
Instead, name the columns you want to return, and use string based indexes:
SQL
SELECT ID, Name FROM ....

C#
string s = (string) dt.Rows[index]["Name"];

Thirdly, if you are going to use numeric indexes, don't use "magic numbers" - it's a lot, lot harder to update them later if you need to change the datasource. Use const int values instead with sensible names.

There are other things you should look at, but those are the biggies!
 
Share this answer
 
Comments
Ujjval Shukla 24-Nov-13 6:53am    
Thanks for reply but my Question is still there that how can i display OT on Other lables like (OT2,OT3....) by using Loops or any other ways..
OriginalGriff 24-Nov-13 6:58am    
Stop accessing the top row only...
Try this:
using System.Collections.Generic; //namespace


List<string> list = new List<string>();//declaration
string value = "";</string></string>


C#
for (int i = 1; i <= Convert.ToInt32(Label40.Text); i++)
           {

list.Add(values.tostring());
 value = String.Join(",", list.ToArray());
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900