DotNet Tutorials

Server Intellect

 Connect to SQL Server using a SqlDataSource Control C#

This tutorial will show you how to connect to SQL Server using C# and a SqlDataSource Control. Design interface for insert data

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.

<table style="width: 426px">
<tr>
<td>

Category Name:</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
<td>
</td>
</tr>
<tr>
<td>
Description:&nbsp;</td>
<td>
<asp:TextBox ID="txtDescription" runat="server"></asp:TextBox></td>
<td>
<asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" /></td>
</tr>
</table>

Connecting the SqlDataSource Control to a Data Source

The following example shows a connection to the SQL Server Northwind sample database using a connection string stored in the <connectionStrings> configuration element.

Create "Data Command" for handle Delete, Insert,Select and Update funcition.

(The default prefix is "@" for Parameter)

Parameter Names

The data source control creates parameters automatically for the values passed in the IDictionary collections. For an insert operation, the data source control populates its InsertParameters collection with values from the name/value pairs in the Values collection. For an update operation, the data source control populates its UpdateParameters collection with values from the name/value pairs in the Keys, NewValues, and OldValues collections. For a delete operation, the data source control populates its DeleteParameters collection with values from the name/value pairs in the Keys and OldValues collections.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=localhost;Initial Catalog=Northwind;User ID=sa;Password="
DeleteCommand="DELETE FROM [Categories] WHERE [CategoryID] = @CategoryID"
InsertCommand="INSERT INTO [Categories] ([CategoryName], [Description]) VALUES (@CategoryName, @Description)"
SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]"
UpdateCommand="UPDATE [Categories] SET [CategoryName] = @CategoryName, [Description] = @Description WHERE [CategoryID] = @CategoryID"
>
<DeleteParameters>
<asp:Parameter Name="CategoryID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="CategoryName" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="CategoryID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="CategoryName" Type="String" />
<asp:Parameter Name="Description" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

Create a Gridview and Bind to SqlDataSource1

The CommandField class is a special field used by data-bound controls (such as GridView and DetailsView) to display command buttons that perform delete, edit, insert, or select operations. Note: This class is new in the .NET Framework version 2.0.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1" Width="426px">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CancelText="Cancel" DeleteText="Delete" EditText="Edit" UpdateText="Update"/>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False"
ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
</Columns>
</asp:GridView>



Performs an insert operation using the InsertCommand SQL string and any parameters that are in the InsertParameters collection

protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource1.InsertParameters["CategoryName"].DefaultValue = txtName.Text.ToString();
SqlDataSource1.InsertParameters["Description"].DefaultValue = txtDescription.Text.ToString();
SqlDataSource1.Insert();
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!
123 ASP

411 ASP

Dot Net Freaks

Server Intellect