Firstly, we need to create an empty project by opening Visual Studio by pressing command devenv in the start menu search bar or using Windows Run-Command by pressing (Windows icon + R ) and enterdevenv. It will open the installed version of Visual Studio.
If by pressing this opens the old one, you can change it by changing the registry value for updated version by following this link.
Step 3: Now give a name to the project and also select desired location of website.
Step 4: Now create a new web form by pressing combination of keys (Ctrl + Shift + A).
Step 5: Now we first add GridView control to the Web Form.
- <asp:GridView ID=“gvitems” runat=“server” AutoGenerateColumns=“false”>
- <asp:BoundField DataField=“ID” HeaderText=“Item ID” ItemStyle-Width=“60” />
- <asp:BoundField DataField=“Name” HeaderText=“Item Name” ItemStyle-Width=“150” />
- <asp:BoundField DataField=“Quantity” HeaderText=“Item Quantity” ItemStyle-Width=“150” />
- Make sure that GridView control must be placed in <form> tag with runat=’server’ attribute.
- Another thing which you need to take care of is that you must use AutoGenerateColumns=”false”.
- CREATE TABLE ITEMS
- ID INT NOT NULL PRIMARY KEY,
- NAME VARCHAR(100) NOT NULL,
- QUANTITY VARCHAR(100) NOT NULL
Connect to the server explorer, connect and drag drop table on the left hand side of the dbml class as elaborated above. Firstly, we open the Server Explorer from View => Server Explorer or by pressing keyboard shortcut combination (Ctrl + W, L).
Right click on Data Connections, New Context Menu opens which shows Add Connection option.
Enter the server name and use your required Authentication to log on the server, after that connect to the database by selecting from drop down menu and select your database and click on Test Connection before clicking on OK button as you are best aware of that the connection is connected mode or not.
- using System.Linq;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- public partial class _Default : System.Web.UI.Page
- protected void Page_Load(object sender, EventArgs e)
- if (!Page.IsPostBack)
- gvitems.DataSource = GetItemsRecord();
- public List<ITEM> GetItemsRecord()
- BindGridViewDataContext db = new BindGridViewDataContext();
- var listitemsrecord = (from x in db.ITEMs select x).ToList<ITEM>();
- return listitemsrecord;
The Final Output