In this article I will explain with an example, how to insert (save) Hijri Date in SQL Server Database Table using ASP.Net with C# and VB.Net.
Database
I have made use of the following table IslamicEvents with the schema as follows.
Note: You can download the database table SQL by clicking the download link below.
HTML Markup
The following HTML Markup consists of two ASP.Net TextBoxes and a Button.
The Button has been assigned an OnClick event handler.
<div class="container">
<div class="row">
<div class="col-md-12">
<label>Event</label>
<div class="input-group">
<asp:TextBox ID="txtEvent" CssClass="form-control" runat="server"></asp:TextBox>
</div>
</div>
<div class="col-md-12">
<label>Date</label>
<div class="input-group">
<asp:TextBox ID="txtHijriDate" CssClass="form-control" runat="server"></asp:TextBox>
</div>
</div>
<div class="col-md-12">
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="OnSubmit" CssClass="btn btn-primary" />
</div>
</div>
</div>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment-hijri@2.1.0/moment-hijri.js"></script>
<script src="assets/js/bootstrap-hijri-datetimepicker.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=txtHijriDate]").hijriDatePicker();
});
</script>
Namespaces
You will need to import the following namespaces.
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Inserting Hijri Date in Database in ASP.Net
When the Submit Button is clicked, following event handler is executed.
Inside this event handler, the values of the Event and Hijri Date TextBoxes are retrieved and inserted into the SQL Server Database Table.
Finally, the Page is redirected to same page.
C#
protected void OnSubmit(object sender, EventArgs e)
{
string eventName = txtEvent.Text;
string hijriDate = txtHijriDate.Text;
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO IslamicEvents VALUES (@Event, @Date)", con))
{
cmd.Parameters.AddWithValue("@Event", eventName);
cmd.Parameters.AddWithValue("@Date", hijriDate);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
VB.Net
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
Dim eventName As String = txtEvent.Text
Dim hijriDate As String = txtHijriDate.Text
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO IslamicEvents VALUES (@Event, @Date)", con)
cmd.Parameters.AddWithValue("@Event", eventName)
cmd.Parameters.AddWithValue("@Date", hijriDate)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Screenshots
The Form
Date inserted in Table
Downloads