In this article I will explain with an example, how to insert Date in dd/MM/yyyy format in SQL Server Database Table using ASP.Net with C# and VB.Net.
This article will illustrate how to insert Date in dd/MM/yyyy format in Database Table using Stored Procedures and the SQL Server DATEFORMAT command in ASP.Net.
Database
I have made use of the following table Medicines with the schema as follows.
Note: You can download the database table SQL by clicking the download link below.
Stored Procedure for inserting Date in dd/MM/yyyy format
Inside the Insert_Medicine Stored Procedure, the Date parameters are of VARCHAR data type so that the Date values can be passed in dd/MM/yyyy format.
Before the INSERT statement, the DATEFORMAT command is executed with DMY option which notifies SQL Server that the values of Dates will be in dd/MM/yyyy format.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE Insert_Medicine
@Name VARCHAR(50),
@ManufacturingDate VARCHAR(10),
@ExpiryDate VARCHAR(10)
AS
BEGIN
SET NOCOUNT ON;
SET DATEFORMAT DMY
INSERT INTO Medicines
VALUES(@Name, @ManufacturingDate, @ExpiryDate)
END
GO
HTML Markup
The following HTML Markup consists of three TextBoxes i.e. Name, Manufacturing Date and Expiry Date.
The Manufacturing Date and Expiry Date TextBoxes are associated with RegularExpressionValidators for validating the dd/MM/yyyy Date format.
<table border="0" cellpadding="3" cellspacing="0" border = "0">
<tr>
<td>Medicine Name:</td>
<td><asp:TextBox ID = "txtName" runat="server" /></td>
<td>
<asp:RequiredFieldValidator ErrorMessage="Required" ForeColor = "Red" ControlToValidate="txtName"
runat="server" Display = "Dynamic"/>
</td>
</tr>
<tr>
<td>Manufacturing Date:</td>
<td><asp:TextBox ID = "txtMfgDate" runat="server" /></td>
<td>
<asp:RequiredFieldValidator ErrorMessage="Required" ForeColor = "Red" ControlToValidate="txtName"
runat="server" Display = "Dynamic" />
<asp:RegularExpressionValidator runat="server" ForeColor = "Red" ControlToValidate="txtMfgDate"
ValidationExpression="(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$"
ErrorMessage="Invalid date format."/>
</td>
</tr>
<tr>
<td>Expiry Date:</td>
<td><asp:TextBox ID = "txtExpDate" runat="server" /></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ErrorMessage="Required" ForeColor = "Red" ControlToValidate="txtName"
runat="server" Display = "Dynamic" />
<asp:RegularExpressionValidator runat="server" ForeColor = "Red" ControlToValidate="txtExpDate"
ValidationExpression="(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$"
ErrorMessage="Invalid date format."/>
</td>
</tr>
<tr>
<td></td>
<td><asp:Button Text="Save" runat="server" OnClick = "Insert" /></td>
<td></td>
</tr>
</table>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Inserting Date in dd/MM/yyyy format in ASP.Net
Inside the Insert Button click event handler, the values of the three TextBoxes are passed to the Insert_Medicine Stored Procedure and it is executed.
C#
protected void Insert(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Insert_Medicine"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
cmd.Parameters.AddWithValue("@ManufacturingDate", txtMfgDate.Text.Trim());
cmd.Parameters.AddWithValue("@ExpiryDate", txtExpDate.Text.Trim());
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
txtName.Text = string.Empty;
txtMfgDate.Text = string.Empty;
txtExpDate.Text = string.Empty;
}
VB.Net
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("Insert_Medicine")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim())
cmd.Parameters.AddWithValue("@ManufacturingDate", txtMfgDate.Text.Trim())
cmd.Parameters.AddWithValue("@ExpiryDate", txtExpDate.Text.Trim())
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
txtName.Text = String.Empty
txtMfgDate.Text = String.Empty
txtExpDate.Text = String.Empty
End Sub
Screenshots
Inserting Date in dd/MM/yyyy format in ASP.Net
Date inserted in Table
Downloads