Search box Using LInq in C#
Create a Table in SQL server
CREATE TABLE [dbo].[HardwareDbTable](
[AssetID] [int] IDENTITY(1,1) NOT NULL,
[AssetName] [nvarchar](max) NOT NULL,
[Manufacturer] [nvarchar](max) NULL,
[Model] [nvarchar](max) NULL
);
Now Insert Some Values in that table :-
CREATE TABLE [dbo].[HardwareDbTable](
[AssetID] [int] IDENTITY(1,1) NOT NULL,
[AssetName] [nvarchar](max) NOT NULL,
[Manufacturer] [nvarchar](max) NULL,
[Model] [nvarchar](max) NULL
);
Now Insert Some Values in that table :-
Now Go To visualstudio :-
Add LinqSqlClass :-
Now Go to View and select ServerExplorer (or) Press Ctrl+Alt+S
Now Drag and Drop The Table from ServerExplorer to DataClasses.dbml
Now AddOne Webform
and Write aspx code
<div>
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"</asp:TextBox><br /><br />
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
Now goto CodeBehind :-
Before that go to DataClasses1.designer.cs
Copy That DataClasses1DataContext()
and paste in aspx codebehind
protected void Page_Load(object sender, EventArgs e)
{
DataClasses1DataContext db = new DataClasses1DataContext();
GridView1.DataSource = from Lst in db.HardwareDbTables
select new
{
ID = Lst.AssetID,
Name = Lst.AssetName,
CompanyName = Lst.Manufacturer,
ModelNumber = Lst.Model
};
GridView1.DataBind();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
DataClasses1DataContext db = new DataClasses1DataContext();
GridView1.DataSource = from Lst in db.HardwareDbTables
where (Lst.AssetID.ToString().Contains(TextBox1.Text)) || (Lst.AssetName.ToString().Contains(TextBox1.Text)) || (Lst.Manufacturer.ToString().Contains(TextBox1.Text)) || Lst.Model.ToString().Contains(TextBox1.Text)
select new
{
ID = Lst.AssetID,
Name = Lst.AssetName,
CompanyName = Lst.Manufacturer,
ModelNumber = Lst.Model
};
GridView1.DataBind();
}
Comments
Post a Comment