Author: Zunnair
Download Source Code : 906_Aspnet_Datasource.zip
In this simple article you will learn to working with asp.net datasource control and datagridview
Create a web application in c#
Drag a asp.net datasource on the form and add new web.config file
Now write connection string in web.config file
xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="DataConnectionString" connectionString="data source= LAPTOP\SQLDEVELOPER;UID=sa;PWD=lahore;DATABASE=shoppingcart"/>
connectionStrings>
<system.web>
<compilation debug="true"/>
<authentication mode="Windows"/>
system.web>
configuration>
Then write code in default.aspx file in form tag
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="DSMobileCategory" runat="server" ConnectionString="<%$ ConnectionStrings:DataConnectionString %>"
OnSelecting="DSMobileCategory_Selecting" ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [PID], [Name], [Description], [Model] FROM [SHOPProducts]">asp:SqlDataSource>
div>
form>
Now put a datagrid control on form and write code on from load event
C#
protected void Page_Load(object sender, EventArgs e)
{
try
{
GridView1.DataSource = DataSource1;
GridView1.DataBind();
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Try
GridView1.DataSource = DataSource1
GridView1.DataBind()
Catch ex As Exception
Label1.Text = ex.Message
End Try
End Sub
This is simple code for asp.net datasource and datagridview.