Navigator: Home - Connect - How to connect to SQL Server using VB.

How to connect to SQL Server using VB.

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 DB 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>&nbsp;</td>
</tr>
<tr>
<td>Description:&nbsp;</td>
<td><asp:TextBox ID="txtDescription" runat="server"></asp:TextBox></td>
<td>&nbsp;<asp:Button ID="Button2" runat="server" Text="Add" /></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 function.

(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>


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 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 DB Tutorials? Click Here!
411asp.net123aspxDotNetFreaksServer Intellect