Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i have 5 Columns pfno, passno, relation,status,here want to display age ,dob

Now i want to display age of person between status and dob according to dob.
But i do not want to store age in database i want to show it dynamically.

suppose dob is 19/10/1992

then in age i want to display 20 years 2 mothns 1 day something like this i want

please help
Posted
Updated 28-Aug-12 1:27am
v2
Comments
[no name] 28-Aug-12 7:41am    
I have all the values in database except age
[no name] 28-Aug-12 7:41am    
I only want age to be added at runtime

you can use data table for this issue,


public DataTable Sel()
        {
            DataTable dt = new DataTable();
            try
            {
                Con_Open();
                cmd = new SqlCommand("select * from Stu", con);
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    dt.Columns.Add(new DataColumn("Reg No"));
                    dt.Columns.Add(new DataColumn("Name"));
                    dt.Columns.Add(new DataColumn("Mark1"));
                    dt.Columns.Add(new DataColumn("Mark2"));
                    dt.Columns.Add(new DataColumn("Mark3"));
                    while (dr.Read())
                    {
                        DataRow temp;
                        temp = dt.NewRow();
                        temp[0] = (System.Convert.IsDBNull(dr[0]) ? "" : dr[0].ToString());
                        temp[1] = (System.Convert.IsDBNull(dr[1]) ? "" : dr[1].ToString());
                        temp[2] = (System.Convert.IsDBNull(dr[2]) ? "" : dr[2].ToString());
                        temp[3] = (System.Convert.IsDBNull(dr[3]) ? "" : dr[3].ToString());
                        temp[4] = (System.Convert.IsDBNull(dr[4]) ? "" : dr[4].ToString());
                        dt.Rows.Add(temp);
                    }
                    cmd.Dispose();
                    dr.Close();
                    con.Close();
                }
                else
                {

                    dt.Columns.Add(new DataColumn("Records"));
                    DataRow temp;
                    temp = dt.NewRow();
                    temp[0] = "No Records Found!!";
                    dt.Rows.Add(temp);
                }
                return dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
                if (!dr.IsClosed)
                {
                    dr.Close();
                }
                return null;
            }
        }


Call:

C#
Griview1.DataSource=Sel();



regards
sarva
 
Share this answer
 
Comments
[no name] 28-Aug-12 7:40am    
Can u please explain it to me a bit
[no name] 28-Aug-12 7:40am    
in this which data is coming from database and which is auto gen?
Sarrrva 28-Aug-12 7:56am    
Actually Whats Your Need? Send Your Full Code... Onurag
regards
sarva
Sarrrva 28-Aug-12 8:03am    
public DataTable Sel()
{
DataTable dt = new DataTable();
try
{
Con_Open();
cmd = new SqlCommand("select * from YourTable", con);
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dt.Columns.Add(new DataColumn("Pfno"));
dt.Columns.Add(new DataColumn("passno"));
dt.Columns.Add(new DataColumn("relation"));
dt.Columns.Add(new DataColumn("Age")); // Non Database column
dt.Columns.Add(new DataColumn("Dob"));
dt.Columns.Add(new DataColumn("Status"));
while (dr.Read())
{
DataRow temp;
temp = dt.NewRow();
temp[0] = (System.Convert.IsDBNull(dr[0]) ? "" : dr[0].ToString());
temp[1] = (System.Convert.IsDBNull(dr[1]) ? "" : dr[1].ToString());
temp[2] = (System.Convert.IsDBNull(dr[2]) ? "" : dr[2].ToString());
temp[3] = Some C#.net Statement; // Do Something Here for Your Age Calculation
temp[4] = (System.Convert.IsDBNull(dr[4]) ? "" : dr[4].ToString());
temp[5] = (System.Convert.IsDBNull(dr[5]) ? "" : dr[5].ToString());
dt.Rows.Add(temp);
}
cmd.Dispose();
dr.Close();
con.Close();
}
else
{

dt.Columns.Add(new DataColumn("Records"));
DataRow temp;
temp = dt.NewRow();
temp[0] = "No Records Found!!";
dt.Rows.Add(temp);
}
return dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
if (con.State == ConnectionState.Open)
{
con.Close();
}
if (!dr.IsClosed)
{
dr.Close();
}
return null;
}
}

regards
sarva
[no name] 28-Aug-12 8:17am    
public void getgridview()
{
//MySqlDataAdapter da;

try
{
string constr = @"server=localhost;uid=root;password=password;database=passdb";
MySqlConnection con = new MySqlConnection(constr);
con.Open();
string str = "select * from empdependents where pfno = '" + textBox1.Text + "'";
ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(str, con);
DataSet ds1 = new DataSet();
da.Fill(ds1, "empdependents");
DataGridView age=new DataGridView();
dataGridView3.Rows.Insert(dataGridView3.Rows.Count -1,age );
this.dataGridView3.DataSource = ds1.Tables[0];
con.Close();


}
catch (Exception ex)
{
MessageBox.Show("Check Data" + ex.Message);
}
}
You can calculate Age in your select statement (pretty easy in SQL or in LINQ, whichever you use)
If dob is DateTime:

SQL
SELECT pfno, passno, relation, status, DATEDIFF(year, dob, getdate()) as age, dob


If it's not datetime, you'll need to convert it.
 
Share this answer
 
v2
Comments
[no name] 28-Aug-12 13:52pm    
What is getdate() here?
sjelen 30-Aug-12 6:32am    
getdate() is MS SQL function that returns current date and time, same as DateTime.Now in C#.
This will calculate age as years, if you need "year, month, day" take a closer look at DATEDIFF function.
It can calculate date difference in many ways.
[no name] 28-Aug-12 14:03pm    
Thanks a Lot
Inside the Itemdatabound Event of the datagrid (In Gridview RowDataBound event)
calculate the age like DOB - Today and store in the age column no need.

For more reference Click Here[^]
 
Share this answer
 

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