Name:  Screenshot 2022-03-07 at 22-09-09 https localhost 44375.png
Views: 35
Size:  3.6 KB

my drop down list does give me options to select after populated. it only gives options for the entered field but when it comes to empty fileds it remains empty please help
Code:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Searchstudentdata.aspx.vb" Inherits="UNIFORMPROJVB.Searchstudentdata" %>


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div style="width: 500px">
    
    <asp:GridView ID="Stockbooks" runat="server" ClientIDMode="Static" OnRowDataBound="OnRowDataBound" AutoGenerateColumns="False" DataKeyNames="Autofield"  Width="16px" ShowFooter = "True"  

        Height="16px" PageSize="10">
        <Columns>
           <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true"  />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Account" ItemStyle-Width="50">
                <FooterTemplate>
                    <asp:TextBox ID="Tfadmno" runat="server" style="margin-bottom: 0px" 
                        Height="16px" Width="80px"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Ladmno" runat="server" Text='<%# Eval("admno") %>'></asp:Label>
                    <asp:TextBox ID="Txtadmno" runat="server"    Text='<%# Eval("admno") %>' 
                        Visible="false" Height="19px" Width="78px" 
                         AutoPostBack="True"   
                        ></asp:TextBox>
                </ItemTemplate>

<ItemStyle Width="50px"></ItemStyle>
                      </asp:TemplateField>
            <asp:TemplateField HeaderText="Student Name" ItemStyle-Width="170">
                <FooterTemplate>
                    <asp:TextBox ID="FstudentName" runat="server" style="margin-bottom: 0px" 
                        Height="19px" Width="164px"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="LblStudentName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                    <asp:TextBox ID="TxtStudentname" runat="server"  Text='<%# Eval("Name") %>' 
                        Visible="false" Height="16px" Width="165px"></asp:TextBox>
                </ItemTemplate>
                <ItemStyle Width="250px" />
            </asp:TemplateField>
            
            
                                             <asp:TemplateField HeaderText="Class" ItemStyle-Width="150">
                                                 <FooterTemplate>
                                                     <asp:DropDownList ID="FDropClass" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1pp" DataTextField="Class" DataValueField="Class" Height="18px" Width="72px">
                                                     </asp:DropDownList>
                                                     <asp:SqlDataSource ID="SqlDataSource1pp" runat="server" ConnectionString="<%$ ConnectionStrings:STOREConnectionString1 %>" SelectCommand="SELECT [Class] FROM [Classes]"></asp:SqlDataSource>
                                                 </FooterTemplate>
                     <ItemTemplate>
                    <asp:Label ID="Lclass" runat="server" Text='<%# Eval("Classr") %>'></asp:Label>
                         <asp:DropDownList ID="DropDownList1" runat="server">
                         </asp:DropDownList>
    </ItemTemplate>
                                                 <ItemStyle Width="150px" />
                </asp:TemplateField>
            
            <asp:TemplateField HeaderText="Stream" ItemStyle-Width="150">
                <FooterTemplate>
                    <asp:Label ID="Lblstream" runat="server" Text="Label"></asp:Label>
                    <asp:DropDownList ID="ddlStreamF" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1BT" DataTextField="stream" DataValueField="stream" Height="22px" onselectedindexchanged="DropDown_Changed" Width="105px">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource1BT" runat="server" ConnectionString="<%$ ConnectionStrings:STOREConnectionString1 %>" SelectCommand="SELECT [stream] FROM [streamdata]"></asp:SqlDataSource>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Lblstream" runat="server" Text='<%# Eval("Stream") %>'></asp:Label>
                    <asp:DropDownList ID="ddlStream" runat="server" 
                         onselectedindexchanged="DropDown_Changed" AutoPostBack="True" Height="20px" Width="105px" 
                        >
                    </asp:DropDownList>
                </ItemTemplate>
                <ItemStyle Width="150px" />
            </asp:TemplateField>
            
     <asp:TemplateField HeaderText="STDTYPE" ItemStyle-Width="50">
                <FooterTemplate>
                    <asp:Label ID="LblSTDTYPEF" runat="server" Text='<%# Eval("STDTYPE") %>'></asp:Label>
                    <asp:DropDownList ID="ddlSTDTYPE" runat="server" AutoPostBack="True" DataTextField="STDTYPE" DataValueField="STDTYPE" Height="16px" onselectedindexchanged="DropDown_Changed" Width="105px">
                        <asp:ListItem>Old</asp:ListItem>
                        <asp:ListItem>New</asp:ListItem>
                        <asp:ListItem>Old Repeater</asp:ListItem>
                    </asp:DropDownList>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="LblSTDTYPE" runat="server" Text='<%# Eval("STDTYPE") %>'></asp:Label>
                    <asp:DropDownList ID="ddlSTDTYPE0" runat="server" AutoPostBack="True" DataTextField="STDTYPE" DataValueField="STDTYPE" Height="16px" onselectedindexchanged="DropDown_Changed" Width="105px">
                        <asp:ListItem>Old</asp:ListItem>
                        <asp:ListItem>New</asp:ListItem>
                        <asp:ListItem>Old Repeater</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
                
<ItemStyle Width="50px"></ItemStyle>
            </asp:TemplateField>
            
             <asp:TemplateField HeaderText="Sex" ItemStyle-Width="150">
                     <FooterTemplate>
                         <asp:Label ID="Label38" runat="server" Text="Lblsexf"></asp:Label>
                         <asp:DropDownList ID="ddlSexF" runat="server" AutoPostBack="True" Height="18px" onselectedindexchanged="DropDown_Changed" Width="105px">
                             <asp:ListItem>Female</asp:ListItem>
                             <asp:ListItem>NONE</asp:ListItem>
                             <asp:ListItem>Male</asp:ListItem>
                         </asp:DropDownList>
                     </FooterTemplate>
                     <ItemTemplate>
                    <asp:Label ID="Lblsex" runat="server" Text='<%# Eval("Sex") %>'></asp:Label>
                         <asp:DropDownList ID="Dropdownlist2" runat="server" AutoPostBack="True" Height="20px" onselectedindexchanged="DropDown_Changed" Width="105px">
                         </asp:DropDownList>
    </ItemTemplate>
                                                 <ItemStyle Width="150px" />
                </asp:TemplateField>
            
           <asp:TemplateField HeaderText="Colour" ItemStyle-Width="150">
                     <FooterTemplate>
                         <asp:TextBox ID="Yrcolour" runat="server" Height="20px" Width="117px"></asp:TextBox>
                     </FooterTemplate>
                     <ItemTemplate>
                    <asp:Label ID="Lblcolour" runat="server" Text='<%# Eval("House") %>'></asp:Label>
                    <asp:TextBox ID="Txtcolour" runat="server" Text='<%# Eval("House") %>' 
                        Visible="false" Height="20px" Width="119px"></asp:TextBox>
    </ItemTemplate>
                                                 <ItemStyle Width="150px" />
                </asp:TemplateField>  
 
            
            <asp:TemplateField HeaderText="ACNOS">
                <FooterTemplate>
                    <asp:Button ID="Button5" runat="server" OnClick="AddNewBook" Text="Add New Student" />
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="LACNOS" runat="server" Text='<%# Eval("ACNOS") %>'></asp:Label>
                    <asp:TextBox ID="TxtACNOS" runat="server" Height="21px" 
                        Text='<%# Eval("ACNOS") %>' Visible="False" Width="74px"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
 
            
        </Columns>
        <SelectedRowStyle BackColor="#CC00FF" />
    </asp:GridView>
    
    </div>
    </form>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
    <link href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        $(function () {
            $("[id*=Stockbooks]").DataTable(
                {
                    bLengthChange: true,
                    lengthMenu: [[5, 10, -1], [5, 10, "All"]],
                    bFilter: true,
                    bSort: true,
                    bPaginate: true
                });
        });
    </script>
</body>
</html>
my codes
Code:
Imports System.Data.SqlClient
Imports System.Data
Imports System.Linq
Imports System.Configuration
Imports System.Web.Services
Imports System.Collections


Public Class Searchstudentdata
    Inherits System.Web.UI.Page
    Dim conString As String = ConfigurationManager.ConnectionStrings("STOREConnectionString1").ConnectionString

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindGrid()
            Enablall()

        End If
    End Sub
    Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        BindGrid()
        Stockbooks.PageIndex = e.NewPageIndex
        'Stockbooks.DataBind()
        Enablall()

    End Sub
    Protected Sub DropDown_Changed(ByVal sender As Object, ByVal e As EventArgs)
        Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        Dim gvRow As GridViewRow = CType(CType(sender, Control).Parent.Parent,
                                       GridViewRow)
        Dim kk As String = gvRow.RowIndex
        Dim index As Integer = gvRow.RowIndex
        For Each oRow As GridViewRow In Stockbooks.Rows
            For i = 0 To oRow.Cells.Count - 1
                If oRow.RowType = DataControlRowType.DataRow Then
                    Dim country As String = DirectCast(Stockbooks.Rows(kk).Cells(5).FindControl("ddlCountries"), DropDownList).Text()
                    'Dim country2 As String = DirectCast(Stockbooks.Rows(i).Cells(5).FindControl("textbox34"), TextBox).Text()
                    Dim strQuery As String = " select DISTINCT(callno),subject FROM subjects where subject = @CustomerID"
                    Dim conb As New SqlConnection(conString)
                    Dim cmd2 As New SqlCommand()
                    cmd2.Parameters.AddWithValue("@CustomerID", country)
                    cmd2.CommandType = CommandType.Text
                    cmd2.CommandText = strQuery
                    cmd2.Connection = conb
                    conb.Open()
                    Dim sdr As SqlDataReader = cmd2.ExecuteReader()
                    ' While sdr.Read()
                    If sdr.HasRows Then
                        sdr.Read()
                        ' txtCity.Text = sdr(0).ToString()
                        ' lblCountry.Text = sdr("Country").ToString()
                        Dim hold As Integer = kk

                        ' If hold = i Then
                        DirectCast(Stockbooks.Rows(kk).Cells(4).FindControl("txtbookcode"), TextBox).Text() = sdr("Callno").ToString()
                        ' End If
                        ' Dim script2 As String = "window.onload = function() { Updatecall('" & time2 & "'); };"
                        ' ClientScript.RegisterStartupScript(Me.GetType(), "Updatecall", script2, True)
                    End If
                End If
            Next
        Next

    End Sub
    Protected Sub OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim isUpdateVisible As Boolean = False
        Dim chk As CheckBox = TryCast(sender, CheckBox)
        If chk.ID = "chkAll" Then
            For Each row As GridViewRow In Stockbooks.Rows
                If row.RowType = DataControlRowType.DataRow Then
                    row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked = chk.Checked
                End If
            Next
        End If
        Dim chkAll As CheckBox = TryCast(Stockbooks.HeaderRow.FindControl("chkAll"), CheckBox)
        chkAll.Checked = True
        For Each row As GridViewRow In Stockbooks.Rows
            If row.RowType = DataControlRowType.DataRow Then
                Dim isChecked As Boolean = row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
                For i As Integer = 1 To row.Cells.Count - 1
                    row.Cells(i).Controls.OfType(Of Label)().FirstOrDefault().Visible = Not isChecked
                    If row.Cells(i).Controls.OfType(Of TextBox)().ToList().Count > 0 Then
                        row.Cells(i).Controls.OfType(Of TextBox)().FirstOrDefault().Visible = isChecked
                    End If
                    If row.Cells(i).Controls.OfType(Of DropDownList)().ToList().Count > 0 Then
                        row.Cells(i).Controls.OfType(Of DropDownList)().FirstOrDefault().Visible = isChecked
                    End If
                    If isChecked AndAlso Not isUpdateVisible Then
                        isUpdateVisible = True
                    End If
                    If Not isChecked Then
                        chkAll.Checked = False
                    End If
                Next
            End If
        Next
        ' btnUpdate.Visible = isUpdateVisible
    End Sub
    Private Function ExecuteQuery(ByVal cmd As SqlCommand, ByVal action As String) As DataTable
        'Dim conString As String = conString
        Using con As New SqlConnection(conString)
            cmd.Connection = con
            Select Case action
                Case "SELECT"
                    Using sda As New SqlDataAdapter()
                        sda.SelectCommand = cmd
                        Using dt As New DataTable()
                            sda.Fill(dt)
                            Return dt
                        End Using
                    End Using
                Case "UPDATE"
                    con.Open()
                    cmd.ExecuteReader()
                    con.Close()
                    Exit Select
            End Select
            Return Nothing
        End Using
    End Function



    Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        'If e.Row.RowType = DataControlRowType.DataRow AndAlso Stockbooks.EditIndex = e.Row.RowIndex Then


        If (e.Row.RowType = DataControlRowType.DataRow) Then

            'Find the DropDownList in the Row
            Dim ddlCountries As DropDownList = CType(e.Row.FindControl("DropDownList1"), DropDownList)
            ddlCountries.DataSource = GetData("SELECT DISTINCT(Class)as classr FROM Classes")
            ddlCountries.DataTextField = "Classr"
            ddlCountries.DataValueField = "Classr"
            ddlCountries.DataBind()

            'Add Default Item in the DropDownList
            ddlCountries.Items.Insert(0, New ListItem("Please select"))

            'Select the Country of Customer in DropDownList
            Dim country As String = CType(e.Row.FindControl("Lclass"), Label).Text
            If country = "" Then
                Exit Sub
            Else

                ddlCountries.Items.FindByValue(country).Selected = True
            End If
        End If






        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim cmd As New SqlCommand("SELECT DISTINCT(Stream)as Stream FROM Streamdata")
            Dim ddlCountries2 As DropDownList = TryCast(e.Row.FindControl("ddlstream"), DropDownList)
            ' Dim nak As String = TryCast(e.Row.FindControl("txtauthor"), TextBox).Text()
            'Dim nakcall As String = TryCast(e.Row.FindControl("txtboocode"), TextBox).Text()
            'DirectCast(Stockbooks.Rows(i).FindControl("txtauthor"), TextBox).Text()
            ddlCountries2.DataSource = Me.ExecuteQuery(cmd, "SELECT")
            ddlCountries2.DataTextField = "Stream"
            ddlCountries2.DataValueField = "Stream"
            ddlCountries2.DataBind()
            'Add Default Item in the DropDownList
            ddlCountries2.Items.Insert(0, New ListItem("Please select"))
            Dim country2 As String = CType(e.Row.FindControl("Lblstream"), Label).Text
            If country2 = "" Then
                Exit Sub
            Else

                Using Con As New SqlConnection(conString)
                    Con.Open()
                    Using Com2 As New SqlCommand("SELECT DISTINCT(stream) FROM streamdata where stream = '" & country2 & "'", Con)
                        Using RDR2 = Com2.ExecuteReader()
                            If RDR2.HasRows Then
                                RDR2.Read()
                                ' DirectCast(e.Row.FindControl("txtbookcode"), TextBox).Text() = RDR2.Item("callno").ToString()
                                ' DirectCast(Stockbooks.FooterRow.FindControl("callno"), TextBox).Text = RDR2.Item("callno").ToString()

                                ddlCountries2.Items.FindByValue(country2).Selected = True
                            Else

                                Exit Sub
                            End If
                            Con.Close()
                        End Using
                    End Using
                End Using
            End If

        End If
        If e.Row.RowType = DataControlRowType.DataRow Then

            'Find the DropDownList in the Row
            Dim ddlCountriesx As DropDownList = CType(e.Row.FindControl("DropDownList2"), DropDownList)
            ' ddlCountriesx.Items.Insert(1, New ListItem("MALE"))
            '= CType(e.Row.FindControl("DropDownList2"), DropDownList)
            ddlCountriesx.DataSource = GetData("SELECT DISTINCT(TYPE)as Sex FROM Sex")
            ddlCountriesx.DataTextField = "sex"
            ddlCountriesx.DataValueField = "sex"
            ddlCountriesx.DataBind()

            'Add Default Item in the DropDownList
            ddlCountriesx.Items.Insert(0, New ListItem("Please select"))
            ' ddlCountriesx.Items.Insert(1, New ListItem("MALE"))
            '  ddlCountriesx.Items.Insert(2, New ListItem("FEMALE"))
            'Select the Country of Customer in DropDownList
            Dim countryx As String = CType(e.Row.FindControl("Lblsex"), Label).Text
            If countryx = "" Then
                Exit Sub
            Else

                ddlCountriesx.Items.FindByValue(countryx).Selected = True
            End If
        End If


    End Sub
    Private Function GetData(query As String) As DataSet
        Dim conString As String = ConfigurationManager.ConnectionStrings("STOREConnectionString1").ConnectionString
        Dim cmd As New SqlCommand(query)
        Using con As New SqlConnection(conString)
            Using sda As New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using ds As New DataSet()
                    sda.Fill(ds)
                    Return ds
                End Using
            End Using
        End Using
    End Function
    Private Sub BindGrid()
        Dim cmd As New SqlCommand("SELECT Name,class as Classr,stream, House, ACNOS,admno,sex,STDTYPE,Autofield FROM STUDENT")
        Stockbooks.DataSource = Me.ExecuteQuery(cmd, "SELECT")
        Stockbooks.DataBind()
    End Sub


    Protected Sub Enablall()
        For Each row As GridViewRow In Stockbooks.Rows
            If row.RowType = DataControlRowType.DataRow Then
                row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked = True
            End If
        Next

        For Each row As GridViewRow In Stockbooks.Rows
            If row.RowType = DataControlRowType.DataRow Then
                Dim isChecked As Boolean = row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
                For i As Integer = 1 To row.Cells.Count - 1
                    ' Dim country As String = row.Cells(i).Controls.OfType(Of TextBox)().FirstOrDefault().Text
                    row.Cells(i).Controls.OfType(Of Label)().FirstOrDefault().Visible = Not isChecked
                    If row.Cells(i).Controls.OfType(Of TextBox)().ToList().Count > 0 Then
                        row.Cells(i).Controls.OfType(Of TextBox)().FirstOrDefault().Visible = isChecked
                    End If
                    If row.Cells(i).Controls.OfType(Of DropDownList)().ToList().Count > 0 Then
                        row.Cells(i).Controls.OfType(Of DropDownList)().FirstOrDefault().Visible = isChecked
                    End If
                    ' If isChecked AndAlso Not isUpdateVisible Then
                    'isUpdateVisible = True
                    ' End If
                    ' If Not isChecked Then
                    'chkAll.Checked = False
                    ' End If
                Next
            End If
        Next
    End Sub


    Private Function GetData(ByVal cmd As SqlCommand) As DataTable
        Dim dt As New DataTable()
        Dim con As New SqlConnection(conString)
        Dim sda As New SqlDataAdapter()
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        con.Open()
        sda.SelectCommand = cmd
        sda.Fill(dt)
        Return dt
    End Function
End Class