Click here to Skip to main content
15,885,910 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello sir,
I have a text box.
I want to enter date into that textbox in string format then that date will be stored in database in date format like 'dd/mm/yyyy' or 'mm/dd/yyyy'.plz send code in c#.urgent.
Posted
Updated 26-Dec-12 3:10am
v2

Use jQuery Date Picker control, it would be easy for you to handle dates in textbox in such formats and to save then in Database.

Add these script files in your HTML page.
JavaScript
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.js" type="text/javascript" />
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js" type="text/javascript" />

<script type="text/javascript">
        $(function () {
            $("#<%=txtDate.ClientID %>").datepicker();
        });
</script>


Use such textbox for date selection

ASP.NET
<asp:textbox id="txtDate" runat="server" cssclass="fv10" width="150px" xmlns:asp="#unknown"></asp:textbox>


At the end, assign some default date value to textbox at PageLoad event.

C#
protected void Page_Load(object sender, EventArgs e)
   {
           if (!Page.IsPostBack)
           {
               txtDate.Text = DateTime.Now().ToShortDateString();
           }
   }



Now at the Page Submit event, you will simply get your date in desired format as txtDate.Text

C#
command.Parameters.Add("@Date", SqlDbType.VarChar).Value = txtDate.Text;



Hope this helps you.
 
Share this answer
 
 
Share this answer
 
v2
Comments
Anuja Pawar Indore 26-Dec-12 8:57am    
Added link tag
C#
DateTime dt = DateTime.ParseExact(TextBoxID.Text, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

string s = dt.ToString("dd/MM/yyyy");//or dt.ToString("MM/dd/yyyy");


But remember first you will have to validate that the user enters date in correct format. Then only you should use this example.
 
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