ASP.NET(vb.net) & SqlDataSource - asp:SqlDataSource The SqlDataSource Represents an SQL database to data-bound controls. To connect to a database, you must set the ConnectionString property to a valid connection string. The SqlDataSource can support any SQL relational database that can be connected to using an ADO.NET provider, such as the SqlClient, OleDb, Odbc, or OracleClient.
Drag SqlDataSource control to ASP.NET Web Form. On SqlDataSource Tasks ->Coose Data Source.
You can select an existing Connection. Or create new New Connection
Select data source or click “Change” to choose a different data source and/or provider…. Click Change…
Select Microsoft SQL Server and Click On…
Input properties Server name : , Log on to the server , Connect to a database. -> OK
Click Next go to next Step.
Save an Connectionstring name.
Configure the Select Statement.
Test Query and Click Finish
Drag Gridview control to ASP.NET Web Form and Choose Data Source from SqlDataSource.
Example Code
Default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>ShotDev.Com Tutorial</title> </head> <body> <form id="form1" runat="server"> <div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" SelectCommand="SELECT * FROM [customer]"></asp:SqlDataSource> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /> <asp:BoundField DataField="CountryCode" HeaderText="CountryCode" SortExpression="CountryCode" /> <asp:BoundField DataField="Budget" HeaderText="Budget" SortExpression="Budget" /> <asp:BoundField DataField="Used" HeaderText="Used" SortExpression="Used" /> </Columns> </asp:GridView> </div> </form> </body> </html>
Default.aspx.vb
Partial Class _Default Inherits System.Web.UI.Page End Class
Screenshot
Download this script.
.
.
Other related articles
ASP.NET(vb.net) SqlDataSource & DataSource
ASP.NET(vb.net) SqlDataSource & Command
ASP.NET(vb.net) SqlDataSource & DataBind
ASP.NET(vb.net) SqlDataSource & GridView RowCommand
ASP.NET(vb.net) SqlDataSource & Visual Studio 2005,2008,2010
1crossbow…
…