This tutorial will show you how to connect to SQL Server using VB and a SqlDataSource Control.
Looking for the C# .NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!
Design interface for insert data
<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: </td> <td> <asp:TextBox ID="txtDescription" runat="server"></asp:TextBox></td> <td> <asp:Button ID="Button2" runat="server" Text="Add" /></td> </tr> </table> |
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
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> |
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
Performs an insert operation using the InsertCommand SQL string and any parameters that are in the InsertParameters collection
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click SqlDataSource1.InsertParameters("CategoryName").DefaultValue = txtName.Text.ToString() SqlDataSource1.InsertParameters("Description").DefaultValue = txtDescription.Text.ToString() SqlDataSource1.Insert() End Sub
|
Looking for the C# .NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!