NET DropDownList Bind To Table Source Code Example : Example Webform Code:
Inherits="Standard_DropDownListBindToTable" %>
"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>
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()
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" %>
<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>
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 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()
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>
<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>
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")
cmd.CommandText = "SELECT EmployeeID, FirstName + ' ' + LastName as FullName FROM Employees" cmd.Connection = con
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)
'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)
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" %>
<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>
' 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.ExecuteNonQuery()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load lblErrMsg.Visible = False
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>
<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>
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)
' 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)
UpdateCmd.ExecuteNonQuery()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load lblErrMsg.Visible = False
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>
<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
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)
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>