Monday, July 25, 2011

SQL MULTIPLE TABLE QUERY WITH CONDITION

SQL MULTIPLE TABLE QUERY WITH CONDITION


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

Sub DropDownList3_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 adp2 As New SqlDataAdapter("SELECT innder.EMP_CODE, innder.F_NAME, innder.L_NAME, DESGNMAS.DESGN_DTL,innder.PBASIC,

innder.SALARY_HD FROM EMPMAST innder INNER JOIN DESGNMAS ON innder.DESGN_CD = DESGNMAS.DESGN_CD where ( dept_code IN (select

dept_code from deptt where dept_name=" & "'" & Dropdownlist1.TEXT & "'" & ") AND SECT_CODE IN (select SECT_CODE from

sectmast where sect_name=" & "'" & Dropdownlist2.TEXT & "'" & ")) AND salary_hd=" & "'" & Dropdownlist3.TEXT & "'", conn)
Dim dset2 As New Data.DataSet
adp2.Fill(dset2, "innder")
datagrid1.datasource=dset2
datagrid1.databind()

End Sub
" HERE IN MULTIPLE QUERY WE HAVE TO USE ALIAS NAME OF OUR FIRST TABLE AS WE HAVE TAKEN "indder" in place of our table "EMPMAST" in query as well as we use "IN" in place of '=' condition and we have to use "INNER JOIN" in place of From EMPMAST, DESGNMAS as well as to link between both table we have to use :
ON innder.Desgn_cd=Desgnmas.Desgn_cd but If we want to unite two table result in one DATASET then we may use this like:
Select EMP_CODE FROM EMPMAST UNION SELECT DESGN_CD FROM DESGNMAS...

IN BODY :


OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">

DEPARTMENT

       

Width="104px">
   


SECTION

       

Width="104px">
   

OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" AutoPostBack="true">

N
P


       
       

No comments:

Post a Comment