ControlParameter object of SqlDataSource in VB.NET

Use ControlParameter object of SqlDataSource to represent the value of any control property. I am creating two page both will represent data in GridView when you select value from DropDownList.
  • 3583
 

Use ControlParameter object of SqlDataSource to represent the value of any control property. I am creating two page both will represent data in GridView when you select value from DropDownList. First page, Default.aspx page use the control parameter to represent data in GridView but second page, Dynamic.aspx represent data in GridView dynamically.

ControlParameter object provide two additional properties listed below.

ControlID-The ID of the control to get the value form other controls.
PropertyName -The name of the property indicating the property from which to get the value.
 
 Default.aspx
 

 <%
@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
 
    CodeFile="Default.aspx.vb" Inherits="_Default" %>
 
 
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
 </
asp:Content>
 <
asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
 
    <div style="font-family: Verdana; font-weight: bold">
 
        Employee Name:<asp:DropDownList
             ID="DropDownList1" runat="server"
             DataSourceID="SqlDataSource1" DataTextField="EMP_FNAME"
             DataValueField="EMP_FNAME" AppendDataBoundItems="True" AutoPostBack="True">
 
            <asp:ListItem>----SELECT---</asp:ListItem>
 
        </asp:DropDownList>
 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
             ConnectionString="<%$ ConnectionStrings:masterConnectionString %>"
             SelectCommand="SELECT [EMP_ID], [EMP_FNAME] FROM [EMPLOYEE1]"
             DataSourceMode="DataReader"></asp:SqlDataSource>
 
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
             DataSourceID="SqlDataSource2" AllowPaging="True"
             CellPadding="4" Font-Bold="True" Font-Size="Small" ForeColor="#333333"
             GridLines="None">
 
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
 
            <Columns>
 
                <asp:BoundField DataField="EMP_ID" HeaderText="EMP_ID" InsertVisible="False"
                     ReadOnly="True" SortExpression="EMP_ID" />
 
                <asp:BoundField DataField="EMP_FNAME" HeaderText="EMP_FNAME"
                     SortExpression="EMP_FNAME" />
 
                <asp:BoundField DataField="EMP_LNAME" HeaderText="EMP_LNAME"
                     SortExpression="EMP_LNAME" />
 
                <asp:BoundField DataField="EMP_COUNTRY" HeaderText="EMP_COUNTRY"
                     SortExpression="EMP_COUNTRY" />
 
                <asp:BoundField DataField="EMP_PHONE_NO" HeaderText="EMP_PHONE_NO"
                     SortExpression="EMP_PHONE_NO" />
 
                <asp:BoundField DataField="EMP_SALARY" HeaderText="EMP_SALARY"
                     SortExpression="EMP_SALARY" />
 
            </Columns>
 
            <EditRowStyle BackColor="#999999" />
 
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
 
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
 
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
 
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
 
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
 
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
 
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
 
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
 
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
 
        </asp:GridView>
 
        <asp:SqlDataSource ID="SqlDataSource2" runat="server"
             ConnectionString="<%$ ConnectionStrings:masterConnectionString %>"
             SelectCommand="SELECT * FROM EMPLOYEE1 WHERE (EMP_FNAME = @id)">
 
            <SelectParameters>
 
                <asp:ControlParameter ControlID="DropDownList1" Name="id"
                     PropertyName="SelectedValue" />
 
            </SelectParameters>
 
        </asp:SqlDataSource> 
     </div>

 </
asp:Content>
 
 Output

Untitled-1.gif

 Dynamic.aspx

 
 <%
@ Page Language="VB" AutoEventWireup="false" CodeFile="Dynamic.aspx.vb" Inherits="Dynamic" %>
 <!
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 style="font-family: Verdana; font-weight: bold">
 
        Employee Name:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
 
        </asp:DropDownList>
 
        <br />
 
        <br />
 
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Bold="True" Font-Names="Verdana"
 
            Font-Size="Small" ForeColor="#333333" GridLines="None">
 
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
 
            <EditRowStyle BackColor="#999999" />
 
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
 
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
 
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
 
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
 
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
 
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
 
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
 
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
 
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
 
        </asp:GridView>
 
    </div>
 
    </form>

 </
body>
 </
html>
 
 Dynamic.aspx.vb

 
 Imports
System.Collections.Generic
 Imports
System.Linq
 Imports
System.Web
 Imports
System.Web.UI
 Imports
System.Web.UI.WebControls
 Imports
System.Web.Configuration
 Imports
System.Data.SqlClient
 
 
Partial Class Dynamic
 
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
         If Not IsPostBack Then
 
            DropDownList1.Items.Insert(0, New ListItem("---Select---"))
             FillDropDownList()
         End If
 
    End Sub
 
 
    ' Show data in GridView
 
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
         Dim s As String = WebConfigurationManager.ConnectionStrings("masterConnectionString").ConnectionString
         Dim con As SqlConnection = New SqlConnection(s)
         con.Open()
         Dim cmd As SqlCommand = New SqlCommand("Select * from Employee1 where EMP_FName='" +DropDownList1.SelectedItem.ToString() + "'", con)
         Dim dr As SqlDataReader = cmd.ExecuteReader()
         GridView1.DataSource = dr
         GridView1.DataBind()
         dr.Close()
         con.Close()
     End Sub
 
 
    ' Fill Dropdownlist
 
    Public Sub FillDropDownList()
         Dim s1 As String = WebConfigurationManager.ConnectionStrings("masterConnectionString").ConnectionString
         Dim con1 As SqlConnection = New SqlConnection(s1)
         con1.Open()
         Dim cmd1 As SqlCommand = New SqlCommand("Select EMP_FName from Employee1", con1)
         Dim dr1 As SqlDataReader = cmd1.ExecuteReader()
         While dr1.Read()
             DropDownList1.Items.Add(dr1(0).ToString())
         End While
 
        dr1.Close()
         con1.Close()
     End Sub

 End
Class
 

 Output

Untitled-1.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.