ASP.NET - assigning time from database - Asked By chitra ganapathy on 02-Apr-12 08:21 AM

hi,
i have the date in the form of 2012-04-03 00:00:00.000,  i want to select the date from database and assign to label in the same format. suggest some idea
dipa ahuja replied to chitra ganapathy on 02-Apr-12 08:40 AM
void getData()
{
  SqlConnection conn = new SqlConnection("connstring");
  SqlCommand comm = new SqlCommand("SELECT DOB from table1 where id=1", conn);
  conn.Open();
  lbldate.Text = comm.ExecuteScalar().ToString();
  conn.Close();
}
 
Reena Jain replied to chitra ganapathy on 02-Apr-12 08:54 AM
Hi,

please use this code like this pass your textbox ids.
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrin  gs["WhateverYouWantToCallYourCon"].ToString());
try
{
   SqlCommand command = new SqlCommand("select datef from table1 where id=1", connection);
   connection.Open();
   SqlDataReader reader = command.ExecuteReader();
 
   if (reader.HasRows)
   {
   while (reader.Read())
   {
     //Assign to your label here  
     YourLable.Text = reader["datef"].ToString();     
   }
   }
}
catch (SqlException ex)
{
  Response.Write(ex.Message.ToString());
}
finally
{
  connection.Close();
  SqlConnection.ClearPool(connection);
}
chitra ganapathy replied to dipa ahuja on 02-Apr-12 09:13 AM
but i need to assign in the same format
dipa ahuja replied to chitra ganapathy on 02-Apr-12 09:21 AM
   void getData()
  {
    SqlConnection conn = new SqlConnection("connstring");
    SqlCommand comm = new SqlCommand("SELECT name from table1 where id=1", conn);
    conn.Open();
    textBox1.Text = string.Format(comm.ExecuteScalar().ToString(),"dd:MM:yyyy HH:mm:ss");
    conn.Close();
  }