How to Add SqlDataSource in GridView using Asp.Net

Introduction
Here I will explain you, how to add SqlDataSource in .aspx page and how to fetch data from database in Asp.Net.

DescriptionNote: I am using Visual Studio 2010 for demonstration. Let us get started with a Step-by-Step approach to do so

Step 1: Open VS 2010. Click File > New > Website. You can choose Asp.Net Website from the list of installed template. choose target platform as .NET Framework 4.0, choose the desired language C# or VB and select the location where you would like to store the website on you system. After selecting the location, click OK.

Step 2: Right click on your solution and click on Add New Item (Ctrl+Shift+A). Now select the Web Form and name it as you want. If you want to add master page, just tick the check box and click on Add button. After that it will ask you to select the master page from your solution. After Selecting the master page, click OK.

Step 3:
Open your created form. Switch to the design mode of it. Open the toolbox (Ctrl+Alt+X) > Data Tag > Drag and drop GridView and SqlDataSource control on to the form. Click on the smart tag of SqlDataSource or right click SqlDataSource > Show Smart tag > Configure Data Source. Now Configure Data Source wizard will open, click on the New Connection to open Add Connection.
Here you have to type your Server Name and Select a Database Name to connect to. Click on Test Connection to make sure that there is no errors while connecting to server. Then click OK.

Step 4:
After this, click Next in your configuration Data Source, an option will be displayed to save the connection string to the configuration (web.config) file. Select the checkbox Yes, save this connection as , type the name for connectionstring ConnectionString and click Next.

Step 5: In the Configure Select Statement,  click on radiobutton to select Specific Columns from tables or Views option. Select table in the Name and choose the required columns from it. Click Next and  click on Test Query to check the preview of data, and finally click on Finish. The wizard adds a SqlDataSource control to the page as shown below:

      <asp:SqlDataSource ID="SqlDataSource1" runat="server"
               ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
               SelectCommand="SELECT [UserName],[FirstName],[LastName],[Location],[EmailId] FROM  
      [Users]">
</asp:SqlDataSource>

If you check you web.Config file, the connection string is added as shown below:

      <connectionStrings>
            <add name="ConnectionString" connectionString="Data Source=SHEKHAR-PC;Initial
                     Catalog=TEMP;Persist Security Info=True;User ID=sa;Password=trucom@123"

                     providerName="System.Data.SqlClient" />
      </connectionStrings>

Step 6: Now click on the smart tag of GridView or right click GridView > Show Smart tag > Choose data Source, select the created SqlDataSource. Click on Format to formatting the gridView  style and skin.

I hope, this article will help you. You can watch video tutorial from the following link shown below:
    
         Link:  Learn How to Add SqlDataSource in Gridview Using Asp.Net

I hope this video was useful and I thank you for watching it.

No comments:

Post a Comment