Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET MS-Access
Hello frndz , 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 datepicker to select date into textbox
 
this is my c# code
 
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
using System.Data.SqlClient;
 
public partial class Company_Master : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
/* lbluser.Text = Session["user_id"].ToString();
lbldiv.Text = Session["division_id"].ToString();
lblunit.Text = Session["unitcode"].ToString();
lblloyr.Text = Session["year"].ToString();
lbldate.Text = Session["date"].ToString();*/
}
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;

}
protected void btnadd_Click(object sender, EventArgs e)
{
try
{
OleDbConnection myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString);
string query = "INSERT INTO company_master(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) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
using (myConnection)
{
myConnection.Open();
using (OleDbCommand cmd = new OleDbCommand(query, myConnection))
{
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);
 
cmd.ExecuteNonQuery();
}
myConnection.Close();
}
 
}
catch (Exception ex)
{
Response.Write("Error In An Application :" + ex.StackTrace + ex.Message);
}
}

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 GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
txtcompid.Text = GridView1.SelectedDataKey.Values[0].ToString();
txtcompname.Text = GridView1.SelectedDataKey.Values[1].ToString();
txtregno.Text = GridView1.SelectedDataKey.Values[2].ToString();
txtregdt.Text = GridView1.SelectedDataKey.Values[3].ToString();
txtaddr.Text = GridView1.SelectedDataKey.Values[4].ToString();
txtaddr1.Text = GridView1.SelectedDataKey.Values[5].ToString();
txtaddr2.Text = GridView1.SelectedDataKey.Values[6].ToString();
txtpin.Text = GridView1.SelectedDataKey.Values[7].ToString();
txtlocalsale.Text = GridView1.SelectedDataKey.Values[8].ToString();
txtcentraltxt.Text = GridView1.SelectedDataKey.Values[9].ToString();
txtfax.Text = GridView1.SelectedDataKey.Values[10].ToString();
txtphone.Text = GridView1.SelectedDataKey.Values[11].ToString();
txtemail.Text = GridView1.SelectedDataKey.Values[12].ToString();
}
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());
}
}
}
Posted 20-Apr-13 2:45am
Edited 20-Apr-13 5:51am
v3
Comments
TrushnaK at 20-Apr-13 7:54am
   
are you want to show your date in label or other textbox present outside.
If you want to change in item template then use DataFormatString="{0:dd/MM/yyyy}"
Devendra Dighe at 20-Apr-13 8:01am
   
i want to show it into the textbox
TrushnaK at 20-Apr-13 8:12am
   
you want value in text-box after clicking on particular row from server-side?
Devendra Dighe at 20-Apr-13 8:17am
   
whenever i click on particular row from the gridview
it shows the date but with time i only want date
TrushnaK at 20-Apr-13 8:25am
   
can you show me that code so i can help you..
Devendra Dighe at 20-Apr-13 8:29am
   
this is my c# code
 
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
using System.Data.SqlClient;
 
public partial class Company_Master : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
/* lbluser.Text = Session["user_id"].ToString();
lbldiv.Text = Session["division_id"].ToString();
lblunit.Text = Session["unitcode"].ToString();
lblloyr.Text = Session["year"].ToString();
lbldate.Text = Session["date"].ToString();*/
}
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;

}
protected void btnadd_Click(object sender, EventArgs e)
{
try
{
OleDbConnection myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString);
string query = "INSERT INTO company_master(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) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
using (myConnection)
{
myConnection.Open();
using (OleDbCommand cmd = new OleDbCommand(query, myConnection))
{
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);
 
cmd.ExecuteNonQuery();
}
myConnection.Close();
}
 
}
catch (Exception ex)
{
Response.Write("Error In An Application :" + ex.StackTrace + ex.Message);
}
}

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 = D
TrushnaK at 20-Apr-13 8:38am
   
hey, i saw your code but i didn't get where you set Date to text box.
 
try this:-
txtStartDate.Text = Format(CDate(your column name), "dd/MM/yyyy")
Devendra Dighe at 20-Apr-13 8:42am
   
here is how i insert into texbox
 
cmd.Parameters.AddWithValue("reg_dt", DateFormat.SplitDateStringAndReturnObject(txtregdt.Text));
 
i have used date picker
for formating date i used following code
 
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;
}
 
}
}
TrushnaK at 21-Apr-13 4:35am
   
in your splitDateStringAndReturnObject() before returning onject
try this:-
 
DateTime dtObj = new DateTime(year, month, day);
dtObj.ToString("dd/MM/yyyy");

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

A few thoughts:
 
To convert a date from a DateTime to string:
date.ToShortDateString(); or
date.ToString("mm/DD/yy");
Btw, the result of ToShortDateString() is culture-sensitive (for en-US, for instance, it would produce 4/20/2013 for today's date).
 
To convert the date from a string to a DateTime:
DateTime.Parse(dateString);
DateTime.TryParse(dateString, out date); or
DateTime.ParseExact(dateString, formatString, formatProvider);
You did't ask about the second part, of course, but those are handy functions and worth knowing.
 
Best of luck!
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 120
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 20 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100