I need a code in c# for search button [Resolved]

Posted by Abnimkar under C# on 10/1/2013 | Points: 10 | Views : 28898 | Status : [Member] | Replies : 4
dear member,
I need a code for c#.I created a form in which 'from date'(textbox)and 'to date'(textbox)
are there and also a search button and a gridview also.and i need a code for search button when i clicked on search button then the results should be displayed in gridview with the sorted date from database.
hope u all understand my language.please help me
thanks in advance.




Responses

Posted by: Bandi on: 10/1/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Another approach is as follows

CREATE procedure find
@fromdate varchar(10),
@todate varchar(10)
as
begin
select * from emp where dob between @fromdate and @todate
end


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Search_record_between_from_todate._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:TextBox ID="txtFromDate" runat="server"></asp:TextBox><br />
<asp:TextBox ID="txtToDate" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /><br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Search_record_between_from_todate
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com = new SqlCommand();
SqlDataAdapter sqlda;
DataTable dt = new DataTable();


protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
SqlCommand com = new SqlCommand("find", con);
com.Parameters.Add("@fromdate", SqlDbType.VarChar).Value = txtFromDate.Text;
com.Parameters.Add("@todate", SqlDbType.VarChar).Value = txtToDate.Text;
com.CommandType = CommandType.StoredProcedure;
sqlda = new SqlDataAdapter(com);
dt.Clear();
sqlda.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Abnimkar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/1/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer this link for grid view with search button in c#.. tweet same code for searching for date ranges too by modifying WHERE condition in Select statement
http://www.c-sharpcorner.com/UploadFile/0c1bb2/searching-records-from-database-and-display-in-gridview-usin/

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Abnimkar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/1/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView.aspx.cs" Inherits="TextBoxValidation.GridView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

From Date(MM/DD/YYYY): <asp:TextBox Id="txtFromDate" runat="server" /> <br />
To Date(MM/DD/YYYY): <asp:TextBox ID="txtToDate" runat="server" />
<asp:Button id="btnSearch" Text="Search" runat="server" OnClick="btnSearch_Click"/> <br />
<asp:GridView ID="grdUserList" runat="server" AutoGenerateColumns="False" DataKeyNames="FullDateAlternateKey" >
<Columns>
<asp:BoundField HeaderText="FullDateAlternateKey" DataField="FullDateAlternateKey" />
<asp:BoundField HeaderText="EnglishDayNameOfWeek" DataField="EnglishDayNameOfWeek" />
<asp:BoundField HeaderText="CalendarYear" DataField="CalendarYear" />
</Columns>

</asp:GridView>
</div>
</form>
</body>
</html>


protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSearch_Click(object sender, EventArgs e)
{
string connectionstring = "Data source=ServerName ; Initial Catalog=AdventureWorksDW2008R2 ; Integrated Security=true";
//ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

string[] strFromDate = txtFromDate.Text.Split('/');
string[] strToDate = txtToDate.Text.Split('/');

DateTime fromDate = Convert.ToDateTime(strFromDate[2] + '-' + strFromDate[0] + '-' + strFromDate[1]);
DateTime toDate = Convert.ToDateTime(strToDate[2] + '-' + strToDate[0] + '-' + strToDate[1]);


SqlConnection conn = new SqlConnection(connectionstring);
conn.Open();
SqlCommand comm = new SqlCommand("SELECT FullDateAlternateKey,EnglishDayNameOfWeek,CalendarYear from [DimDate] WHERE FullDateAlternateKey Between '" + fromDate + "' and '" + toDate + "' ", conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
grdUserList.DataSource = ds;
grdUserList.DataBind();
}


NOTE: Enter Date value in the MM/DD/YYYY

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Abnimkar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response