Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can anybody suggest any solution for the below:

In sql table column, I defined a field as DATETIME.
while retrieving I want on only date.
And my project is in ASP.net, C#;

And I am displaying data in grid.

public DataTable load_Project()
{

DataTable dtTower = new DataTable();
string sql = "select projName from tbl_Project";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(cmd);

sda.Fill(dtTower);
return dtTower;

}
Posted

select projName,CONVERT(VARCHAR(10),GETDATE() ,103) from tbl_Project

in place of GETDATE() you can put your datetime field name.

it will give a date in dd/mm/yyyy format.

Minal Shah.
 
Share this answer
 
Just take your DateTime field into DateTime Class of CSharp and then
you can use it's function ToDateString() to get only date.

You can do this at the time of rowdata bound event of grid.

Refer following link for mapping of sql server and ado.net data types.

http://msdn.microsoft.com/en-us/library/cc716729.aspx[^]

Hope this will help!
 
Share this answer
 
shanawazway wrote:
string sql = "select projName from tbl_Project";


Change your sql query to

select projName, convert(varchar,dateField,101) from tbl_Project
(where dateField is your date field in the database).

101 will return your date in mm/dd/yyyy format but there are other formats available as well (check out the Convert keyword on msdn for other formats).
 
Share this answer
 
v3

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