Saturday, July 23, 2011

DropDownList1_SelectedIndexChanged AND SQL SUB QUERY


DropDownList1_SelectedIndexChanged AND SQL SUB QUERY

Let Suppose you need to refine Dropdownlist2 contents according to list item of Dropdownlist1 which are connected with data base then first you have to use :

OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">

autopostback should true and on indexchanged event you have to fire :
Sub DropDownList1_SelectedIndexChanged(sender As Object, e As System.EventArgs)
Dim conn As New SqlConnection("Data Source=\\10.182.186.100\pipe\sql\query;Initial Catalog=pay-roll;")
Dim adp1 As New SqlDataAdapter("select SECT_NAME from sectmast where ( dept_code = (select dept_code from deptt where

dept_name=" & "'" & Dropdownlist1.TEXT & "'" & "))" , conn)
Dim dset1 As New Data.DataSet
adp1.Fill(dset1, "sectmast")
Dropdownlist2.DataSource = dset1
Dropdownlist2.DataTextField = "SECT_NAME"
Dropdownlist2.DataValueField = "SECT_NAME"
Dropdownlist2.DataBind()


End Sub

where at load event already you have loaded dropdownlist1 with data like :

If Not Page.IsPostBack Then
Dim conn As New SqlConnection("Data Source=\\10.182.186.100\pipe\sql\query;Initial Catalog=pay-roll;")
Dim adp As New SqlDataAdapter("select DEPT_NAME from DEPTT" , conn)
Dim dset As New Data.DataSet
adp.Fill(dset, "DEPTT")
datagrid1.datasource=dset
datagrid1.databind()
Dropdownlist1.DataSource = dset
Dropdownlist1.DataTextField = "DEPT_NAME"
Dropdownlist1.DataValueField = "DEPT_NAME"
Dropdownlist1.DataBind()
End If

End Sub

and your header should be :
<%@ Page Language="VB" %>
<%@ import Namespace="System.Data.Sqlclient" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>

dropdownlist should be like :

OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">

MEMO_NO

but here since you are initiating your dropdownlist at run tine loading with datatable then here listitem should not be added.

No comments:

Post a Comment