Insert date in SQL Server Database through Asp.net, C#

You will stuck literally everytime when you try to insert date in databse from C#. I always got stuck in this situation. Then finally I find one solution that works. Best way to do is create a text box in asp.net like this:

<asp:TextBox ID="txtDate" runat="server" TextMode="date"></asp:TextBox>

In backend C#, get textbox data as string:

string aDate = string.IsOrEmpty(txtDate.Text)==false ? Convert.ToDateTime(txtDate.Text).ToString("dd/MM/yyyy") : "";

In sql server store procedure, insert date through this method:

@aDate nvarchar(10)
insert into <tablename> (aDate) values (convert(date,@aDate,103))

if will insert date into dd/mm/yyyy format.

This is the easiest way to insert date into database.

Hope this helps. :)

 

Author Profile

pallav

Pallav Kumar

@pallav
Software Engineer
Delhi, India

Leave a Comment

message*