Display only date from gridview to textbox

Posted by Deven under ASP.NET on 4/20/2013 | Points: 10 | Views : 2879 | Status : [Member] | Replies : 1
i want to display only date into textbox from gridview It displays as 11/03/2013 12:00:00 AM but i want only date 11/03/2013 i database it is stored on date

i tried to modify my code as
<asp:TemplateField HeaderText="reg_dt" Visible="False">
<ItemTemplate>
<asp:Label ID="Label29" runat="server"
Text ='<%# Bind("reg_dt" ,"{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

but it displays same as first format what changes should i do to display only date plz help me

I m using .js file of datepicker to select date into textbox

here is my c# code


public partial class Company_Master : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

Util.JQueryUtils.RegisterTextBoxForDatePicker(Page, txtregdt);
DataTable dt;
OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString);
OleDbCommand cmd1 = new OleDbCommand("SELECT cmp_id,cmp_name,reg_no,reg_dt,addr_line_1,addr_line_2,addr_line_3,pin_code,lst_no,cst_no,fax,phone,emailid FROM company_master ORDER BY cmp_id ASC", conn);
OleDbDataAdapter dA = new OleDbDataAdapter(cmd1);
dt = new DataTable();
dA.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["comp"] = dt;
GridView1.Visible = true;

}


private class DateFormat
{
public static DateTime SplitDateStringAndReturnObject(string DateString)
{
if (DateString == "" || DateString == null)
{

int year = 9999;
int month = 1;
int day = 1;
DateTime dtObj = new DateTime(year, month, day);

return dtObj;
}
else
{
int len;
string[] datesplit = DateString.Split('/');
len = datesplit.Length;
if (len < 3)
{
datesplit = DateString.Split('-');
}

int year = Convert.ToInt32(datesplit[2]);
int month = Convert.ToInt32(datesplit[1]);
int day = Convert.ToInt32(datesplit[0]);
DateTime dtObj = new DateTime(year, month, day);

return dtObj;
}

}
}


protected void btnupdate_Click(object sender, EventArgs e)
{
try
{
OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString);
string que = "UPDATE company_master SET cmp_name =@cmp_name,reg_no = @reg_no,reg_dt =@reg_dt,addr_line_1 = @addr_line_1,addr_line_2 =@addr_line_2,addr_line_3 = @addr_line_3,pin_code = @pin_code,lst_no = @lst_no,cst_no =@cst_no,fax = @fax,phone = @phone,emailid =@emailid WHERE cmp_id =?";
using (con)
{
con.Open();
using (OleDbCommand cmd = new OleDbCommand(que, con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("cmp_id", txtcompid.Text);
cmd.Parameters.AddWithValue("@cmp_name", txtcompname.Text);
cmd.Parameters.AddWithValue("@reg_no", txtregno.Text);
cmd.Parameters.AddWithValue("@reg_dt", DateFormat.SplitDateStringAndReturnObject(txtregdt.Text));
cmd.Parameters.AddWithValue("@addr_line_1", txtaddr.Text);
cmd.Parameters.AddWithValue("@addr_line_2", txtaddr1.Text);
cmd.Parameters.AddWithValue("@addr_line_3", txtaddr2.Text);
cmd.Parameters.AddWithValue("@pin_code", txtpin.Text);
cmd.Parameters.AddWithValue("@lst_no", txtlocalsale.Text);
cmd.Parameters.AddWithValue("@cst_no", txtcentraltxt.Text);
cmd.Parameters.AddWithValue("@fax", txtfax.Text);
cmd.Parameters.AddWithValue("@phone", txtphone.Text);
cmd.Parameters.AddWithValue("@emailid", txtemail.Text);

int co = cmd.ExecuteNonQuery();
if (co > 0)
{
Response.Write("Update Success");
}
else
{
Response.Write("Update was not successful");
}

}
con.Close();
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}





Responses

Posted by: Kmandapalli on: 4/23/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

While displaying the datetim einto gridview, instead of assigning datetime to the date field, assign it as DateTime.Now.Date so that only the date will be display.
So from GridView you can directly read into Textbox.

Regards,
Shree M.

Kavya Shree Mandapalli

Deven, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response