Tuesday 17 June 2014

How to create Optional Search with Entity Framwork and Linq in C# .Net.

How to create Optional Search  with Entity Framwork and Linq in  C# .Net.

--create database sqltopquery
use sqltopquery

--create table MyFriendList in database sqltopquery  

create table MyFriendList(SrNo int identity(1,1) primary key,Name nvarchar(50),Emailid nvarchar(50) unique ,MobileNo nvarchar(50)unique,Address nvarchar(50),Course nvarchar(50))

--insert  into table  
insert into MyFriendList values('Kush Tiwari','kushktiwari@gmail.com','9451119029','Ghazipur','M C A')

--Code  for Search from table with any condition(using dynamic query) without any if else condition

Source code  for Default.aspx ………………………………………………..

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 id="Head1" runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 70%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table class="style1" >
<tr>
<td colspan="2">
<center><b><i>Optional Search in Asp.Net with Linq or Entity Framwork</i></b></center> </td>
</tr>
<tr>
<td>
Search By Name</td>
<td>
Search By EmailId</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtemailid" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Search By Mobile No</td>
<td>
Search By&nbsp; Course</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtmobileno" runat="server"></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="ddlcourse" runat="server" Height="19px" Width="126px">
<asp:ListItem>--Select--</asp:ListItem>
<asp:ListItem>M C A</asp:ListItem>
<asp:ListItem>B C A</asp:ListItem>
<asp:ListItem>B Tech</asp:ListItem>
<asp:ListItem>M Tech</asp:ListItem>
<asp:ListItem>M A</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
Search&nbsp; By Address</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnSearch" runat="server" onclick="btnSearch_Click"
Text="Search" />
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Label ID="lblmessage" runat="server" Text="Label" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</td>
</tr>
</table>
<div>
</div>
</form>
</body>
</html>


Source code  for Default.aspx.cs ………………………………………………..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcwithJqueryModel;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
// create instance .................

MvcwithJqueryEntities mm = new MvcwithJqueryEntities();

protected void btnSearch_Click(object sender, EventArgs e)
{

try
{

SearchFriends(txtname.Text, txtemailid.Text, txtmobileno.Text, ddlcourse.SelectedItem.Text, txtaddress.Text);
}

catch (Exception ex)
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = ex.Message;
}

}

//Create method with different parameter for Search...................

private void SearchFriends(string name, string emailid, string mobileno, string course, string address)
{
if (course == "--Select--")
{
course = "";
}

var v = (from m in mm.MyFriendLists select m);

// Search by Friend Name..................................

if (!string.IsNullOrEmpty(name))
v = v.Where(m => m.Name.Equals(name));

// Search by Friend EmailId..................................

if (!string.IsNullOrEmpty(emailid))
v = v.Where(m => m.Emailid.Equals(emailid));

// Search by Friend MobileNo..................................
if (!string.IsNullOrEmpty(mobileno))
v = v.Where(m => m.MobileNo.Equals(mobileno));

// Search by Friend Course..................................

if (!string.IsNullOrEmpty(course))
v = v.Where(m => m.Course.Equals(course));

// Search by Friend Address..................................

if (!string.IsNullOrEmpty(address))
v = v.Where(m => m.Address.Equals(address));


GridView1.DataSource = v.ToList();
GridView1.DataBind();

}

}

Result


0 comments:

Post a Comment