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

Pallav Kumar
07-02-2020 16:17:32
5.00 (1 votes)
Rate:

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.IsNullOrEmpty(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. :)

 


ASP.NET, C#, SQL Server, Date and Time,



Software Engineer (India)
pallav.kumar837@gmail.com
+918010265036








Search