Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In attendance table thier are columns D1,D2,....D31.below i'm trying to fetch a column by Concatenate D& local variable j1 .here j1 is the last day of month(likeD30 or D31 or D28 or D29) if j1=30 then D"+j1+" consider as D30 and assign it's value to variable att but it assigns 30 not D30's value how can i fetch actual value by adding local variable with Column name (like D"+j1+")..hereif j1=30 the query should return D30's value and store it on att variable ..


string j1 = Convert.ToString(days1);
        att = "";
        ad = new SqlDataAdapter("select D"+j1+"  from Attendance where Emp_Code='" + TextBox1.Text + "',reg='" + regn1 + "'and Type='" + Label112.Text + "'", con);
        
        if (dt6.Rows.Count > 0)
        {
            att = Convert.ToString(dt6.Rows[0][0]);
        }
Posted
Updated 6-Dec-13 20:17pm
v2

Try this...

string j1 = Convert.ToString(days1);
att = "";
ad = new SqlDataAdapter("select D" + j1 + " from Attendance where Emp_Code='" + TextBox1.Text + "' and  reg='" + regn1 + "' and [Type]='" + Label112.Text + "'", con);

DataTable dt6 = new DataTable();

ad.Fill(dt6);

if (dt6.Rows.Count > 0)
{
 att = Convert.ToString(dt6.Rows[0][0]);
}
 
Share this answer
 
v4
below code will help You


C#
string j1 = Convert.ToString(days1);
att = "";
ad = new SqlDataAdapter("select D + j1 + " from Attendance where Emp_Code='" + TextBox1.Text + "' and  reg='" + regn1 + "' and [Type]='" + Label112.Text + "'", con);

DataTable dt6 = new DataTable();

ad.Fill(dt6);

if (dt6.Rows.Count > 0)
{
 att = dt6.Rows[0][0].ToString()+"'s";
}
 
Share this answer
 
Comments
OriginalGriff 7-Dec-13 4:17am    
Reason for my vote of 1: Copying an existing solution and posting it as your own is not a good way to behave. It is the beginnings of "abuse" which can get you banned from the site.
Salman622 7-Dec-13 5:51am    
do you have eyes
Salman622 7-Dec-13 5:53am    
just read the Question above and see the solution below.
and plz don't interfere.
OriginalGriff 7-Dec-13 6:00am    
I suggest you look at the "Solution 1" above your "Solution 2", and check out the time difference between them...
And "interfering" as you call it is part of why I am here: as a Protector I am expected to spot things like this, and take appropriate action. In this case, I felt that a polite comment asking you to desist would be sufficient. If you wish to be banned from the site, I can certainly help you with that, however.
Salman622 7-Dec-13 6:04am    
for your kind information solution 1 is wrong according to the question asked
You should have a look to that also.

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