Hi akhter,
Refer below sample.
Database
For this sample I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
SQL
CREATE PROCEDURE SearchCustomerDetailsByDate
@StartDate DATETIME,
@EndDate DATETIME
AS
BEGIN
SELECT TOP 5 OrderID
,CONVERT(VARCHAR,OrderDate,105) OrderDate
,ShipName
,ShipCity
,ShipCountry
FROM Orders WHERE OrderDate BETWEEN @StartDate AND @EndDate
END
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 5 OrderID ,OrderDate ,ShipName ,ShipCity ,ShipCountry FROM Orders ", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
this.gvOrders.DataSource = dt;
this.gvOrders.DataBind();
}
}
}
}
}
protected void Search(object sender, EventArgs e)
{
DataTable dt = GetDate(txtFromDate.Text, txtEndDate.Text);
this.gvOrders.DataSource = dt;
this.gvOrders.DataBind();
}
private DataTable GetDate(string fromdate, string enddate)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SearchCustomerDetailsByDate", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@StartDate", fromdate);
cmd.Parameters.AddWithValue("@EndDate", enddate);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 5 OrderID ,OrderDate ,ShipName ,ShipCity ,ShipCountry FROM Orders ", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Me.gvOrders.DataSource = dt
Me.gvOrders.DataBind()
End Using
End Using
End Using
End If
End Sub
Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = GetDate(txtFromDate.Text, txtEndDate.Text)
Me.gvOrders.DataSource = dt
Me.gvOrders.DataBind()
End Sub
Private Function GetDate(ByVal fromdate As String, ByVal enddate As String) As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SearchCustomerDetailsByDate", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@StartDate", fromdate)
cmd.Parameters.AddWithValue("@EndDate", enddate)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot