There is a number of developer who get bore to interact with database. Off course, there were many things getting bore. Top of the list, the main reason, interacting with database, to write complex queries, closing database connections, maintaining database connection pools etc.
To get rid off from all these, Microsoft introduces Language Integrate Query names as LINQ. Using LINQ you can use SQL queries directly into your code. There is no need to maintain classes. There are many LINQ providers now, like Excel, Flicker, Google, Sharepoint, WMI etc.
However, the main purpose of LINQ provider is the code that lets you query a specific type of Data.
When you use LINQ, you will find, the code in LINQ is much similar with SQL. But main difference is the command order. Every statement starts with “from”. The reason for this is that intellisence is able to give you hints the moment you have specified the object in the from statement.
Following are the steps which help you to create first web application using asp.net along LINQ.
- Open VS 2008
- Create a new website, code behind language depends on you like VB or C#
- Right Click on Solution Explorer
- Add ASP.Net folder names App_Code
- After adding App_Code folder, right click to Add New Item
- Here, you select LINQ to SQL classes
- Name whatever you want, in my case I name Employee.dbml
- Now open Server Explorer
- Create a database, in my case, I create Employee along one table Employee_Table, the columns are id as int (PK), name as varchar(255), and designation varchar (255), id is auto incremented
- Now, click on Employee.dbml
- Drag new created Employee_Table, save it, and close it
- Right Click on Solution Explorer, Add New Item
- Select Web Page, in my case I name it Default.aspx
- Create two text boxes, one gridview, and one drop down list
- Now, open Default.aspx.cs
- Add a library “using System.Collections;”
- See the Code below
- .
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- EmployessDataContext db = new EmployessDataContext();
- var qry = from emp in db.Employee_Tables select emp.id;
- DropDownList1.DataSource = qry;
- DropDownList1.DataBind();
- this.Show();
- }
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- EmployessDataContext db = new EmployessDataContext();
- Employee_Table emp = new Employee_Table();
- emp.name = TextBox1.Text;
- emp.designation = TextBox2.Text;
- db.Employee_Tables.InsertOnSubmit(emp);
- db.SubmitChanges();
- TextBox2.Text = “”;
- TextBox1.Text = “”;
- this.Show();
- }
- public void Show()
- {
- EmployessDataContext db = new EmployessDataContext();
- var qry = from emp in db.Employee_Tables
- select emp;
- GridView1.DataSource = qry;
- GridView1.DataBind();
- }
- protected void Button2_Click(object sender, EventArgs e)
- {
- EmployessDataContext db = new EmployessDataContext();
- string id = DropDownList1.SelectedItem.ToString();
- Employee_Table emp_table = (from emp in db.Employee_Tables where emp.id == int.Parse(id) select emp).Single();
- emp_table.name = TextBox1.Text;
- emp_table.designation = TextBox2.Text;
- db.SubmitChanges();
- this.Show();
- }
- protected void Button3_Click(object sender, EventArgs e)
- {
- EmployessDataContext db = new EmployessDataContext();
- string id = DropDownList1.SelectedItem.ToString();
- Employee_Table emp_table = (from emp in db.Employee_Tables where emp.id == int.Parse(id) select emp).Single();
- db.Employee_Tables.DeleteOnSubmit(emp_table);
- db.SubmitChanges();
- this.Show();
- }
- classes will be used as given
- using System;
- 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.Collections;
Pingback: online betting
Pingback: Find great deals with bed bath and beyond bonus coupon
Pingback: carl ken
Pingback: Bed Bath and Beyond Store is a Woman's Dream Store
Pingback: ge financial auto warranty services
Introduction to LINQ in ASP.Net 3.5 | pickbeauty.com You made some decent factors there. I looked on the web for the issue and found most individuals will associate with with your website. Regards, Indoor Furniture Manufacturer
Hello,I enjoy reading through your weblog, I wanted to leave just a little comment to support you and wish you an excellent continuation. Wishing you the best of luck for all your blogging efforts.
Hello, just attempting close to a number of information web sites, looks a pretty good technique which you are applying. Im at current utilizing Wp for several connected with the internet websites even so seeking to transform 1 among them to the web site any program similar to the one you have as a demo work. Anything particularly youll suggest regarding it?
I like this post, enjoyed this one appreciate it for posting. “To the dull mind all nature is leaden. To the illumined mind the whole world sparkles with light.” by Ralph Waldo Emerson.
Im impressed, I must say. Really rarely do I come across a weblog thats both informative and entertaining, and let me tell you, youve hit the nail on the head. Your weblog is important; the issue is something that not sufficient folks are talking intelligently about. Im genuinely pleased that I stumbled across this in my search for some thing relating to this problem.
Hi to all, since I am really eager of reading this website’s post to be updated daily. It carries pleasant material.
Heya i’m for the first time here. I came across this board and I find It really useful & it helped me out much. I hope to give something back and aid others like you aided me.