Tutorial RSS
 
DB Tutorials Server Intellect Cloud Hosting
Navigator: Home - Display - Basic Data Access with SQL Database & ASP.NET 2.0

Basic Data Access with SQL Database & ASP.NET 2.0

This tutorial will show how we can use a GridView control to display and update data in a SQL database.

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.


Looking for more ASP.NET Tutorials? Click Here!

Firstly, we need a connection to the database. In Web.config:

<connectionStrings>
<add name="BasicDataAccessConnectionString" connectionString="Data Source=CLIENT-TASK2\SQLEXPRESS;Initial Catalog=BasicDataAccess;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="BasicDataAccessConnectionString2" connectionString="Data Source=CLIENT-TASK2\SQLEXPRESS;Initial Catalog=BasicDataAccess;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

Using DetailsView and GridView controls to view and update data in two different ways. Both using the same DataSource, from the same database.
The ASPX code:

<form id="form1" runat="server">
<div>
<h1>Edit Database</h1>
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" Height="50px" Width="125px" AutoGenerateRows="False" DataKeyNames="theID">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
<EditRowStyle BackColor="#2461BF" />
<RowStyle BackColor="#EFF3FB" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Fields>
<asp:BoundField DataField="theID" HeaderText="ID" ReadOnly="True" SortExpression="theID" />
<asp:BoundField DataField="theName" HeaderText="Name" SortExpression="theName" />
<asp:BoundField DataField="theCity" HeaderText="City" SortExpression="theCity" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<br />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" PageSize="5">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="theID" HeaderText="ID" SortExpression="theID" />
<asp:BoundField DataField="theName" HeaderText="Name" SortExpression="theName" />
<asp:BoundField DataField="theCity" HeaderText="City" SortExpression="theCity" />
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BasicDataAccessConnectionString2 %>" SelectCommand="SELECT * FROM [tblOne]" DeleteCommand="DELETE FROM [tblOne] WHERE [theID] = @theID" InsertCommand="INSERT INTO [tblOne] ([theID], [theName], [theCity]) VALUES (@theID, @theName, @theCity)" UpdateCommand="UPDATE [tblOne] SET [theName] = @theName, [theCity] = @theCity WHERE [theID] = @theID">
<DeleteParameters>
<asp:Parameter Name="theID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="theName" Type="String" />
<asp:Parameter Name="theCity" Type="String" />
<asp:Parameter Name="theID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="theID" Type="Int32" />
<asp:Parameter Name="theName" Type="String" />
<asp:Parameter Name="theCity" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
&nbsp; &nbsp;

</div>
</form>

No code needs to be written for this simple data manipulation using GridView - Visual Studio generates the code for you.



Looking for more ASP.NET Tutorials? Click Here!

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!


Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!