You are on page 1of 23

VB.NET ASP.

NET DropDownList Bind To Table Source Code Example : Example Webform Code:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DropDownListBindToTable.aspx.vb"

Inherits="Standard_DropDownListBindToTable" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server"> </asp:DropDownList> </div> </form> </body> </html>

Example Code Behind:

Imports System Imports System.Data Imports System.Data.SqlClient

Partial Class Standard_DropDownListBindToTable Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim con As SqlConnection = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI") Dim cmd As New SqlCommand() cmd.CommandText = "SELECT EmployeeID, FirstName + ' ' + LastName as FullName FROM Employees" cmd.Connection = con Dim Table1 As DataTable Table1 = New DataTable("Employees") 'creating a table named Employees Dim Row1 As DataRow 'declaring row for the table Dim EmployeeID As DataColumn = New DataColumn("EmployeeID") 'declaring a column named EmployeeID EmployeeID.DataType = System.Type.GetType("System.Int32") 'setting the datatype for the column

Table1.Columns.Add(EmployeeID) 'adding the column to table Dim FullName As DataColumn = New DataColumn("FullName") FullName.DataType = System.Type.GetType("System.String") Table1.Columns.Add(FullName) Try con.Open() Dim reader As SqlDataReader = cmd.ExecuteReader '(CommandBehavior.SingleRow) While reader.Read() Row1 = Table1.NewRow() 'declaring a new row Row1.Item("EmployeeID") = reader.GetInt32(0) 'filling the row with values. Item property is used to set the field value. Row1.Item("FullName") = reader.GetString(1) 'filling the row with values. adding FullName Table1.Rows.Add(Row1) End While reader.Close() Finally con.Close() End Try DropDownList1.DataSource = Table1 Me.DropDownList1.DataTextField = "FullName" Me.DropDownList1.DataValueField = "EmployeeID" DropDownList1.DataBind()

End Sub End Class

VB.NET ASP.NET Sql Command Delete Statement Source Code Example: Example Webform Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="SqlCommandDelete.aspx.vb" Inherits="Database_ADONET_SqlCommandDelete" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td>First Name: </td><td> <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox> </td> </tr> <tr>

<td>Last Name: </td><td> <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox> </td> </tr> </table><br /> <asp:Button ID="btnDelete" runat="server" Text="Delete" /><br /> <asp:Label ID="lblErrMsg" runat="server" Text="lblErrMsg" ForeColor="#FF3300" Visible="False"></asp:Label><br /> <asp:GridView ID="GridView1" runat="server" DataSourceID="sdsEmployees"> </asp:GridView> <asp:SqlDataSource ID="sdsEmployees" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_ConnectionString %>" SelectCommand="SELECT [FirstName], [LastName] FROM [Employees]"> </asp:SqlDataSource> </div> </form> </body> </html>

Example Code Behind:


Imports System Imports System.Data Imports System.Data.SqlClient

Partial Class Database_ADONET_SqlCommandDelete Inherits System.Web.UI.Page

Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click Dim thisConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("Northwind_ConnectionString").ConnectionStr ing)

'Create Command object Dim nonqueryCommand As SqlCommand = thisConnection.CreateCommand()

Try ' Open Connection thisConnection.Open()

' Create DELETE statement with named parameters nonqueryCommand.CommandText = _ "DELETE Employees WHERE FirstName = @FirstName and LastName = @LastName"

' Add Parameters to Command Parameters collection nonqueryCommand.Parameters.Add("@FirstName", SqlDbType.VarChar, 10) nonqueryCommand.Parameters.Add("@LastName", SqlDbType.VarChar, 20)

nonqueryCommand.Parameters("@FirstName").Value = txtFirstName.Text

nonqueryCommand.Parameters("@LastName").Value = txtLastName.Text

nonqueryCommand.ExecuteNonQuery()

Catch ex As SqlException ' Display error lblErrMsg.Text = ex.ToString() lblErrMsg.Visible = True

Finally ' Close Connection thisConnection.Close()

End Try GridView1.DataBind() End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load lblErrMsg.Visible = False

End Sub End Class Example Connection String: <connectionStrings> <add name="Northwind_ConnectionString"

connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI" /> <add name="Pubs_ConnectionString" connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=SSPI" /> </connectionStrings>

VB.NET ASP.NET Table Load From SqlDataReader Source Code Example


Example Webform Code:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="TableLoadFromSqlDataReader.aspx.vb" Inherits="ADONET_TableLoadFromSqlDataReader" %>

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> </div>

</form> </body> </html>

Example Code Behind:


Imports System Imports System.Data Imports System.Data.SqlClient

Partial Class ADONET_TableLoadFromSqlDataReader Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim con As SqlConnection = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI")

Dim cmd As New SqlCommand()

cmd.CommandText = "SELECT EmployeeID, FirstName + ' ' + LastName as FullName FROM Employees" cmd.Connection = con

Dim Table1 As DataTable

Table1 = New DataTable("Employees") 'creating a table named Employees Dim Row1 As DataRow 'declaring row for the table

Dim EmployeeID As DataColumn = New DataColumn("EmployeeID") 'declaring a column named EmployeeID EmployeeID.DataType = System.Type.GetType("System.Int32") 'setting the datatype for the column Table1.Columns.Add(EmployeeID) 'adding the column to table Dim FullName As DataColumn = New DataColumn("FullName") FullName.DataType = System.Type.GetType("System.String") Table1.Columns.Add(FullName)

Try con.Open() Dim reader As SqlDataReader = cmd.ExecuteReader '(CommandBehavior.SingleRow) While reader.Read()

Row1 = Table1.NewRow() 'declaring a new row Row1.Item("EmployeeID") = reader.GetInt32(0)

'filling the row with values. Item property is used to set the field value. Row1.Item("FullName") = reader.GetString(1) 'filling the row with values. adding FullName Table1.Rows.Add(Row1)

End While reader.Close() Finally con.Close() End Try

GridView1.DataSource = Table1 GridView1.DataBind()

End Sub End Class

Example Connection String:


<connectionTableLoadFromSqlDataReaders> <add name="Northwind_ConnectionTableLoadFromSqlDataReader" connectionTableLoadFromSqlDataReader="Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI" /> <add name="Pubs_ConnectionTableLoadFromSqlDataReader" connectionTableLoadFromSqlDataReader="Server=(local)\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=SSPI" /> </connectionTableLoadFromSqlDataReaders>

VB.NET ASP.NET Sql Command Insert Statement Source Code Example: Example Webform Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="SqlCommandInsert.aspx.vb" Inherits="Database_ADONET_SqlCommandInsert" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td>First Name: </td><td> <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox> </td> </tr> <tr> <td>Last Name: </td><td> <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox> </td> </tr>

</table><br /> <asp:Button ID="btnInsert" runat="server" Text="Insert" /><br /> <asp:Label ID="lblErrMsg" runat="server" Text="lblErrMsg" ForeColor="#FF3300" Visible="False"></asp:Label><br /> <asp:GridView ID="GridView1" runat="server" DataSourceID="sdsEmployees"> </asp:GridView> <asp:SqlDataSource ID="sdsEmployees" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_ConnectionString %>" SelectCommand="SELECT [FirstName], [LastName] FROM [Employees]"> </asp:SqlDataSource> </div> </form> </body> </html>

Example Code Behind: Imports System Imports System.Data Imports System.Data.SqlClient

Partial Class Database_ADONET_SqlCommandInsert Inherits System.Web.UI.Page

Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click

Dim thisConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("Northwind_ConnectionString").ConnectionStr ing)

'Create Command object Dim nonqueryCommand As SqlCommand = thisConnection.CreateCommand()

Try ' Open Connection thisConnection.Open()

' Create INSERT statement with named parameters nonqueryCommand.CommandText = _ "INSERT INTO Employees (FirstName, LastName) VALUES (@FirstName, @LastName)"

' Add Parameters to Command Parameters collection nonqueryCommand.Parameters.Add("@FirstName", SqlDbType.VarChar, 10) nonqueryCommand.Parameters.Add("@LastName", SqlDbType.VarChar, 20)

nonqueryCommand.Parameters("@FirstName").Value = txtFirstName.Text nonqueryCommand.Parameters("@LastName").Value = txtLastName.Text

nonqueryCommand.ExecuteNonQuery()

Catch ex As SqlException ' Display error lblErrMsg.Text = ex.ToString() lblErrMsg.Visible = True

Finally ' Close Connection thisConnection.Close()

End Try GridView1.DataBind() End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load lblErrMsg.Visible = False

End Sub End Class

Example Connection String: <connectionStrings> <add name="Northwind_ConnectionString" connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI" /> <add name="Pubs_ConnectionString" connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=SSPI" />

</connectionStrings>

VB.NET ASP.NET Sql Command Update Statement Source Code Example:


Example Webform Code: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="SqlCommandUpdate.aspx.vb" Inherits="Database_ADONET_SqlCommandUpdate" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td>Find this First Name: </td><td> <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox> </td> </tr> <tr> <td>Update Last Name with this: </td><td>

<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox> </td> </tr> </table><br /> <asp:Button ID="btnUpdate" runat="server" Text="Update" /><br /> <asp:Label ID="lblErrMsg" runat="server" Text="lblErrMsg" ForeColor="#FF3300" Visible="False"></asp:Label><br /> <asp:GridView ID="GridView1" runat="server" DataSourceID="sdsEmployees"> </asp:GridView> <asp:SqlDataSource ID="sdsEmployees" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_ConnectionString %>" SelectCommand="SELECT [FirstName], [LastName] FROM [Employees]"> </asp:SqlDataSource> </div> </form> </body> </html>

Example Code Behind: Imports System Imports System.Data Imports System.Data.SqlClient

Partial Class Database_ADONET_SqlCommandUpdate Inherits System.Web.UI.Page

Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click Dim thisConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("Northwind_ConnectionString").ConnectionStr ing)

'Create Command object Dim nonqueryCommand As SqlCommand = thisConnection.CreateCommand()

Try ' Open Connection thisConnection.Open()

' 1. Create Command ' Sql Update Statement Dim updateSql As String = _ "UPDATE Employees " & _ "SET LastName = @LastName " & _ "WHERE FirstName = @FirstName" Dim UpdateCmd As New SqlCommand(updateSql, thisConnection)

' 2. Map Parameters

UpdateCmd.Parameters.Add("@FirstName", _ SqlDbType.NVarChar, 10, "FirstName")

UpdateCmd.Parameters.Add("@LastName", _ SqlDbType.NVarChar, 20, "LastName")

UpdateCmd.Parameters("@FirstName").Value = txtFirstName.Text UpdateCmd.Parameters("@LastName").Value = txtLastName.Text

UpdateCmd.ExecuteNonQuery()

Catch ex As SqlException ' Display error lblErrMsg.Text = ex.ToString() lblErrMsg.Visible = True

Finally ' Close Connection thisConnection.Close()

End Try GridView1.DataBind() End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load lblErrMsg.Visible = False

End Sub End Class

Example Connection String: <connectionStrings> <add name="Northwind_ConnectionString" connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI" /> <add name="Pubs_ConnectionString" connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=SSPI" /> </connectionStrings>

VB.NET ASP.NET Sql Command Select Statement Source Code Example:


Example Webform Code: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="SqlCommandSelect.aspx.vb" Inherits="Database_ADONET_SqlCommandSelect" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html> <head> <title>Sql Command Select VB.NET ASP.NET</title> </head> <body>

<form id="Form1" runat="server"> <p> Find: <asp:TextBox id="TextBox1" runat="server" Text="*"></asp:TextBox> <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Go"></asp:Button> </p> <p> <asp:DataGrid id="DataGrid1" runat="server" BorderWidth="5px" BorderColor="Black" CellPadding="5"> <HeaderStyle font-size="X-Small" font-names="Arial Narrow" font-bold="True" forecolor="White" backcolor="Black"></HeaderStyle> <AlternatingItemStyle backcolor="Silver"></AlternatingItemStyle> <ItemStyle font-size="X-Small" font-names="Arial Narrow"></ItemStyle> </asp:DataGrid> </p> </form> </body> </html> Example Code Behind: Imports System Imports System.Data Imports System.Data.SqlClient

Partial Class Database_ADONET_SqlCommandSelect Inherits System.Web.UI.Page

Function FindByTitle(ByVal search As String) As System.Data.DataSet Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("Pubs_ConnectionString").ConnectionString) Dim queryString As String = ""

If search = "*" Then queryString = "SELECT [titles].[title], [titles].[price], [titles].[notes], [titles].[pubdate] F" & _ "ROM [titles]" Else queryString = "SELECT [titles].[title], [titles].[price], [titles].[notes], [titles].[pubdate] F" & _ "ROM [titles] WHERE ([titles].[title] like @search + '%')" End If

Dim sqlCmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, con) If search <> "*" Then sqlCmd.Parameters.Add("@search", System.Data.SqlDbType.NVarChar).Value = search End If

Dim dataAdapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(sqlCmd) Dim dataSet As System.Data.DataSet = New System.Data.DataSet dataAdapter.Fill(dataSet)

Return dataSet End Function

Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Dim sTitle As String = TextBox1.Text DataGrid1.DataSource = FindByTitle(sTitle) DataGrid1.DataBind() End Sub

End Class

Example Connection String: <connectionStrings> <add name="Nortwind_ConnectionString" connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI" /> <add name="Pubs_ConnectionString" connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=SSPI" /> </connectionStrings>

You might also like