it is not important to switch code-behind always to bind the data. there are ton of datacontrols that will do automatically it for you say as gridvidw. But remember, if u need to develop any strong or say extra formatted apps, you need to switch in code-behind. In visual studio, switch the web form in design view and from database explorer drag the database table on form (remember web form should be in design view). even it will generate the connection string in web.cofig file automatically. this process will automatically bind the data as well. Find the sample code here:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" AllowPaging="True"
DataSourceID="EmployeesSqlSource" DataKeyNames="EmployeeID"
OnDataBound="GridView1_DataBound"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="EmployeeID"
HeaderText="EmployeeID" InsertVisible="False"
ReadOnly="True" SortExpression="EmployeeID" />
<asp:BoundField DataField="LastName"
HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="FirstName"
HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="Title"
HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="BirthDate"
HeaderText="BirthDate" SortExpression="BirthDate" />
<asp:BoundField DataField="PostalCode"
HeaderText="PostalCode" SortExpression="PostalCode" />
<asp:BoundField DataField="Extension"
HeaderText="Extension" SortExpression="Extension" />
</Columns>
<EmptyDataTemplate>
<div style="text-align: center">
No Data Available.
</div>
</EmptyDataTemplate>
</asp:GridView>
<asp:SqlDataSource ID="EmployeesSqlSource" runat="server"
ConnectionString=
"Data Source=MELIGY\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient"
SelectCommand=
"SELECT [EmployeeID], [LastName], [FirstName], [Title], [BirthDate], [PostalCode], [Extension] FROM [Employees]"
OnSelected="EmployeesSqlSource_Selected"></asp:SqlDataSource>
Student of M.Tech IT | Microsoft MVP | Mindcracker MVP | IT Faculty | Blogger | Author
Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator