troubleshooting Question

Help required in reducing the overhead and issues using the asp.net datagrid

Avatar of srafi78
srafi78Flag for United States of America asked on
ASP.NET
8 Comments1 Solution350 ViewsLast Modified:
I am working with a datagrid,
The datagrid consists of a check box, 2 dropdowns, 1 textbox and labels for each datagrid

row.
Both the dropdowns are populated from tables not linked to the dataset, but the dataset

contains columns for these values to be stored.
This datagrid is used to match the items from 2 databases 1 Receiving , the other PO
Items in the PO database are received and entered in the Receiving database, some times

items received are not originally in the PO.
I have to match/leave alone the extra item received to the PO Item.

The datagrid contains a column for the receivedItemID and a column for POItemID (POItemID

to be displayed in a dropdown list)
The dataset contains the value for receivedItemID and all Nulls for POItemID initially
The extra receivedItemID can either be matched to an POitemID or Left alone.
I populate POItemID dropdown from the POItemIDs, If any received ItemId's match the POItems then I have to show these values even though they are not in the dataset in the POItemID column and have a default empty selected value where there is no match.
So far so good, I am able to do so........

No when I check and uncheck the checkbox in the grid the checkbox selection changed event fires and as per the code below it adds some dynamic values to the dropdown usually an empty value or adds the previous selected value and displays this selected value as the default dropdown selected value.

When I check the checkbox first time the dropdown is made visible and is enabled, I then change the values in the dropdown update the datset with the save button and everything works fine, but if I check and uncheck the dropdown multiple times I get lost
each time I check and uncheck the dropdown an empty string is added to the dropdown, next the Item with index 0 is added every time,

My coding is clumsy and I use the database table to check for particular conditions as I am not able to figure out how can I use the session dataset values to look for these conditions.

I am posting parts of the aspx code and the vb code. Help is appreciated.

Thanks!

aspx.......


<div style="BORDER-RIGHT: #00006f thin solid; BORDER-TOP: #00006f thin solid; BACKGROUND-IMAGE: none; BORDER-LEFT: #00006f thin solid; WIDTH: 99.2%; BORDER-BOTTOM: #00006f thin solid; HEIGHT: 200px; BACKGROUND-COLOR: #d2d2d2"
align="center" overflow="auto"><asp:datagrid id="dgReceivingInfo" runat="server" Width="888px" BorderWidth="1" PageSize="15"
HorizontalAlign="Justify" AutoGenerateColumns="False" BorderColor="Black" BorderStyle="Solid" EnableViewState="True">
<SelectedItemStyle BackColor="Cornsilk" />
<Columns>
<asp:TemplateColumn HeaderText="Select">
      <HeaderStyle></HeaderStyle>
      <ItemStyle></ItemStyle>
      <ItemTemplate>
            <asp:CheckBox ID="chkbox" Runat="server" Checked="False" AutoPostBack="True" OnCheckedChanged="chkbox_CheckedChanged"></asp:CheckBox>
      </ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Receipt Item #" HeaderStyle-BorderStyle="Solid" HeaderStyle-BorderWidth="1"
      ItemStyle-BorderStyle="Solid" ItemStyle-HorizontalAlign="Left" ItemStyle-BorderWidth="1">
      <HeaderStyle HorizontalAlign="Center" BackColor="LightGray" Width="70px"></HeaderStyle>
      <ItemStyle Height="22px" Width="70px"></ItemStyle>
      <ItemTemplate>
            <asp:label Runat = "server" ID="lblItemID" Height="" Width = "67px" text='<%# DataBinder.Eval(Container.DataItem,"ItemID") %>'>
            </asp:label>
      </ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="PO Item #" HeaderStyle-BorderStyle="Solid" HeaderStyle-BorderWidth="1"
      ItemStyle-BorderStyle="Solid" ItemStyle-HorizontalAlign="Left" ItemStyle-BorderWidth="1">
      <HeaderStyle HorizontalAlign="Center" BackColor="LightGray" Width="70px"></HeaderStyle>
      <ItemStyle Height="22px" Width="70px"></ItemStyle>
      <ItemTemplate>
            <asp:Label Runat = "server" ID="lblItemID_PO" text='<%# DataBinder.Eval(Container.DataItem, "ItemID_PO") %>' Width = "67px">
            </asp:Label>
            <asp:DropDownList Runat = "server" Enabled = "False" Visible="False" ID = "ddItemID_PO" Width = "67px" DataSource='<%# TempDataView_ItemID %>' DataValueField="ItemID" DataTextField="ItemID" >
            </asp:DropDownList>
      </ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Item Description" HeaderStyle-BorderStyle="Solid" HeaderStyle-BorderWidth="1"
      ItemStyle-BorderStyle="Solid" ItemStyle-HorizontalAlign="Left" ItemStyle-BorderWidth="1">
      <HeaderStyle HorizontalAlign="Center" BackColor="LightGray" Width="350px"></HeaderStyle>
      <ItemStyle Height="22px" Width="350px"></ItemStyle>
      <ItemTemplate>
            <asp:label Runat = "server" ID="lblItemDesc" Height="" Width = "347px" text='<%# DataBinder.Eval(Container.DataItem,"Description") %>'>
            </asp:label>
      </ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Quantity Received" HeaderStyle-BorderStyle="Solid" HeaderStyle-BorderWidth="1"
      ItemStyle-BorderStyle="Solid" ItemStyle-HorizontalAlign="Right" ItemStyle-BorderWidth="1">
      <HeaderStyle HorizontalAlign="Center" BackColor="LightGray" Width="70px"></HeaderStyle>
      <ItemStyle Height="22px" Width="70px"></ItemStyle>
      <ItemTemplate>
            <asp:Label Runat = "server" ID="lblQtyReceived" Height="" Width="67px" text='<%# DataBinder.Eval(Container.DataItem,"Quantity") %>'>
            </asp:Label>
      </ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Receiving Comments" HeaderStyle-BorderStyle="Solid" HeaderStyle-BorderWidth="1"
      HeaderStyle-HorizontalAlign="Center" ItemStyle-BorderStyle="Solid" ItemStyle-HorizontalAlign="Left"
      ItemStyle-BorderWidth="1" ItemStyle-Height="22px">
      <HeaderStyle BackColor="LightGray" Width="230px"></HeaderStyle>
      <ItemStyle Height="22px" Width="230px"></ItemStyle>
      <ItemTemplate>
            <asp:Label ID="lblRecvComments" Runat="server" Width = "230px"  Font-Size="8" text='<%# Databinder.Eval(Container.DataItem,"ReceivingComments") %>'>
            </asp:Label>
            <asp:DropDownList Visible="False" Enabled = "False" Font-Size="8" ID="ddRecvComments" Width="230px" Runat="server" Height="" DataSource='<%# TempDataView %>' DataValueField="ReceivingComments" DataTextField="ReceivingComments" AutoPostBack="True" OnSelectedIndexChanged="ddRecvComments_SelectedIndexChanged">
            </asp:DropDownList>
            <asp:TextBox ID="txtRecvComments" Visible = "False" Enabled = "true" Font-Size="8" Text='<%# DataBinder.Eval(Container.DataItem,"ReceivingComments") %>' Width="230px" Runat="server" Height="">
            </asp:TextBox>
      </ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="False">
      <Itemtemplate>
            <asp:Label Runat = "server" ID = "lblReceiptID" Text='<%# DataBinder.Eval(Container.DataItem, "ReceiptID")%>'>
            </asp:Label>
      </Itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="False">
      <Itemtemplate>
            <asp:Label Runat = "server" ID = "lblPOID" Text='<%# DataBinder.Eval(Container.DataItem, "POID")%>'>
            </asp:Label>
      </Itemtemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></div>


vb code...............

   Private Sub dgReceivingInfo_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgReceivingInfo.ItemDataBound

        Dim dgReceivingInfo As DataGrid = CType(sender, DataGrid)
        Dim dgi As DataGridItem

        For Each dgi In dgReceivingInfo.Items
            Try
                If dgi.ItemType = ListItemType.Item Or dgi.ItemType = ListItemType.AlternatingItem Then

                    Dim lblReceiptID As Label = CType(dgi.FindControl("lblReceiptID"), Label)
                    Dim lblPOID As Label = CType(dgi.FindControl("lblPOID"), Label)
                    Dim lblItemID As Label = CType(dgi.FindControl("lblItemID"), Label)
                    'Dim txtItemID_PO As TextBox = CType(dgi.FindControl("txtItemID_PO"), TextBox)
                    Dim lblItemID_PO As Label = CType(dgi.FindControl("lblItemID_PO"), Label)
                    Dim ddItemID_PO As DropDownList = CType(dgi.FindControl("ddItemID_PO"), DropDownList)
                    Dim lblRecvComments As Label = CType(dgi.FindControl("lblRecvComments"), Label)
                    Dim ddRecvComments As DropDownList = CType(dgi.FindControl("ddRecvComments"), DropDownList)
                    Dim txtRecvComments As TextBox = CType(dgi.FindControl("txtRecvComments"), TextBox)

                    Dim pkColumn(1) As DataColumn
                    pkColumn(0) = Session("ds_ItemID").Tables("POItem").Columns("ItemID")
                    'set the primary key to the ItemID column
                    Session("ds_ItemID").Tables("POItem").PrimaryKey = pkColumn

                    Dim rowFound As DataRow = Session("ds_ItemID").Tables("POItem").Rows.Find(lblItemID.Text)

                    If fnCheckIfExists("spSearchItemID_PO", lblReceiptID.Text, lblPOID.Text, lblItemID.Text, "ItemID_PO") = False Then
                        If rowFound Is Nothing Then
                            ddItemID_PO.Items.Insert(0, "")
                            ddItemID_PO.SelectedValue = 0
                        Else
                            ddItemID_PO.Items.Insert(0, lblItemID.Text)
                            ddItemID_PO.SelectedIndex = 0
                            ddItemID_PO.Items.Add("")
                            lblItemID_PO.Text = lblItemID.Text
                        End If
                    Else
                        'Dim ds As DataSet = fnCheckIfExists("spSearchItemID_PO", lblReceiptID.Text, lblPOID.Text, lblItemID.Text, "ItemID_PO")
                        Dim ItemID_PO As String = Session("col") 'ds.Tables(0).Columns("ItemID_PO").ToString
                        ddItemID_PO.Items.Insert(0, ItemID_PO)
                        ddItemID_PO.SelectedIndex = 0
                        ddItemID_PO.Items.Add("")
                    End If

                    If fnCheckIfExists("spSearchRecvComments", lblReceiptID.Text, lblPOID.Text, lblItemID.Text, "ReceivingComments") = False Then
                        ddRecvComments.Items.Insert(0, "")
                        ddRecvComments.SelectedIndex = 0
                    Else
                        'Dim ds As DataSet = CType(fnCheckIfExists("spSearchRecvComments", lblReceiptID.Text, lblPOID.Text, lblItemID.Text, "ReceivingComments"), DataSet)
                        Dim RecvComments As String = Session("col") 'ds.Tables(0).Columns("RecvComments").ToString
                        ddRecvComments.Items.Insert(0, RecvComments)
                        ddRecvComments.SelectedIndex = 0
                        ddRecvComments.Items.Add("")
                    End If
                End If
            Catch ex As Exception
            End Try
        Next
    End Sub


Protected Sub ddRecvComments_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddRecvComments.SelectedIndexChanged
        Dim ddRecvComments As DropDownList = CType(sender, DropDownList)
        Dim txtRecvComments As TextBox = ddRecvComments.Parent.FindControl("txtRecvComments")
        Try
            If ddRecvComments.SelectedValue = "Other" Then
                ddRecvComments.Visible = False
                txtRecvComments.Visible = True
            End If
        Catch ex As Exception
        End Try
    End Sub

    Public Sub LoadDropDownList_ItemID()
        Dim drv As DataRowView

        Dim ds_ItemID As DataSet = New DataSet
        Dim da As SqlDataAdapter
        Dim conn As SqlConnection = clsConnection.GetConnection()

        Dim cmd As SqlCommand = New SqlCommand("spLoadDropDownList_ItemID", conn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@POID", Session("arrVal_Ix"))
        da = New SqlDataAdapter(cmd)
        da.Fill(ds_ItemID, "POItem")
        Dim y As Int32 = ds_ItemID.Tables("POItem").Rows.Count
        Session.Add("ds_ItemID", ds_ItemID)

        TempDataView_ItemID = New DataView(ds_ItemID.Tables("POItem"))

    End Sub

    Protected Sub chkbox_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkbox.CheckedChanged

        'Dim dg As DataGrid = Me.dgReceivingInfo
        'Dim dgi As DataGridItem
        'For Each dgi In dg.Items
        'If dgi.ItemType = ListItemType.Item Or dgi.ItemType = ListItemType.AlternatingItem Then

        Try
            Dim chkbox As CheckBox = CType(sender, CheckBox)

            Dim lblItemID As Label = CType(chkbox.Parent.FindControl("lblItemID"), Label)
            Dim lblItemID_PO As Label = CType(chkbox.Parent.FindControl("lblItemID_PO"), Label)
            Dim ddItemID_PO As DropDownList = CType(chkbox.Parent.FindControl("ddItemID_PO"), DropDownList)
            Dim ddRecvComments As DropDownList = CType(chkbox.Parent.FindControl("ddRecvComments"), DropDownList)
            Dim txtRecvComments As TextBox = CType(chkbox.Parent.FindControl("txtRecvComments"), TextBox)
            Dim lblRecvComments As Label = CType(chkbox.Parent.FindControl("lblRecvComments"), Label)
            Dim lblReceiptID As Label = CType(chkbox.Parent.FindControl("lblReceiptID"), Label)
            Dim lblPOID As Label = CType(chkbox.Parent.FindControl("lblPOID"), Label)
            Dim ItemID_PO As String
            Dim RecvComments As String

            If chkbox.Checked = True Then

                ddRecvComments.Items.Remove(ddRecvComments.Items.Item(0))
                ddItemID_PO.Items.Remove(ddRecvComments.Items.Item(0))
                lblItemID_PO.Visible = False
                lblRecvComments.Visible = False
                ddRecvComments.Visible = True
                ddRecvComments.Enabled = True
                Dim pkColumn(1) As DataColumn
                pkColumn(0) = Session("ds_ItemID").Tables("POItem").Columns("ItemID")
                'set the primary key to the ItemID column
                Session("ds_ItemID").Tables("POItem").PrimaryKey = pkColumn

                Dim rowFound As DataRow = Session("ds_ItemID").Tables("POItem").Rows.Find(lblItemID.Text)

                If fnCheckIfExists("spSearchItemID_PO", lblReceiptID.Text, lblPOID.Text, lblItemID.Text, "ItemID_PO") = False Then

                    If rowFound Is Nothing Then

                        ddItemID_PO.Visible = True
                        ddItemID_PO.Enabled = True

                    Else

                        ddItemID_PO.Visible = True
                        ddItemID_PO.SelectedValue = lblItemID.Text

                    End If
                Else
                    'Dim ds As DataSet = fnCheckIfExists("spSearchItemID_PO", lblReceiptID.Text, lblPOID.Text, lblItemID.Text, "ItemID_PO")
                    'ItemID_PO = ds.Tables(0).Columns("ItemID_PO").ToString

                    'ddItemID_PO.Items.Insert(0, ItemID_PO)
                    'ddItemID_PO.SelectedIndex = 0
                    ddItemID_PO.Visible = True
                    ddItemID_PO.Enabled = True
                    ddItemID_PO.Items.Add("")
                End If

                If fnCheckIfExists("spSearchRecvComments", lblReceiptID.Text, lblPOID.Text, lblItemID.Text, "ReceivingComments") = False Then
                    lblRecvComments.Visible = False
                    ddRecvComments.Visible = True
                    ddRecvComments.Enabled = True
                    ddRecvComments.Items.Insert(0, "")
                    ddRecvComments.SelectedIndex = 0

                Else
                    'Dim ds As DataSet = fnCheckIfExists("spSearchRecvComments", lblReceiptID.Text, lblPOID.Text, lblItemID.Text)
                    lblRecvComments.Visible = False
                    ddRecvComments.Visible = True
                    ddRecvComments.Enabled = True
                    'RecvComments = ds.Tables(0).Columns("ReceivingComments").ToString
                    ddRecvComments.Items.Insert(0, ddRecvComments.SelectedValue)
                    ddRecvComments.SelectedIndex = 0
                    ddRecvComments.Items.Add("")
                End If

            Else

                    ddItemID_PO.Enabled = False
                    ddItemID_PO.Visible = False
                    lblItemID_PO.Visible = True
                    lblItemID_PO.Text = ddItemID_PO.SelectedValue
                    ddRecvComments.Enabled = False
                    ddRecvComments.Visible = False
                    lblRecvComments.Visible = True
                    lblRecvComments.Text = ddRecvComments.SelectedValue

            End If

        Catch ex As Exception
        End Try
        'End If
        'Next

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim btnSave As Button = CType(sender, Button)
        Dim dgReceivingInfo As DataGrid = Me.dgReceivingInfo
        Dim arEdit() As DataRow

        Dim ds As DataSet = CType(Session("ds_ReceivingInfo"), DataSet)
        Dim dgi As DataGridItem

        Dim i As Int32 = 0

        For Each dgi In dgReceivingInfo.Items

            Dim cb As CheckBox = CType(dgi.FindControl("chkBox"), CheckBox)
            Dim lblItemID As Label = CType(dgi.FindControl("lblItemID"), Label)
            Dim lblItemID_PO As Label = CType(dgi.FindControl("lblItemID_PO"), Label)
            Dim ddItemID_PO As DropDownList = CType(dgi.FindControl("ddItemID_PO"), DropDownList)
            Dim lblItemDesc As Label = CType(dgi.FindControl("lblItemDesc"), Label)
            Dim lblQtyReceived As Label = CType(dgi.FindControl("lblQtyReceived"), Label)
            Dim lblRecvComments As Label = CType(dgi.FindControl("lblRecvComments"), Label)
            Dim ddRecvComments As DropDownList = CType(dgi.FindControl("ddRecvComments"), DropDownList)
            Dim txtRecvComments As TextBox = CType(dgi.FindControl("txtRecvComments"), TextBox)
            Dim lblReceiptID As Label = CType(dgi.FindControl("lblReceiptID"), Label)
            Dim lblPOID As Label = CType(dgi.FindControl("lblPOID"), Label)

            If cb.Checked = True Then

                Dim p As String = lblReceiptID.Text
                Dim q As String = lblPOID.Text
                Dim r As String = lblItemID.Text

                arEdit = Session("ds_ReceivingInfo").Tables(0).Select("ReceiptID='" + lblReceiptID.Text + "' AND POID='" + lblPOID.Text + "' AND ItemID='" + lblItemID.Text + "'")
                arEdit(0)("ItemID_PO") = ddItemID_PO.SelectedValue
                If ddRecvComments.SelectedValue = "Other" Then
                    arEdit(0)("ReceivingComments") = txtRecvComments.Text
                    txtRecvComments.Visible = False
                    ddRecvComments.Visible = True
                    ddRecvComments.Items.Insert(0, "")
                    ddRecvComments.Items.Insert(0, txtRecvComments.Text)
                    ddRecvComments.SelectedIndex = 0
                Else
                    arEdit(0)("ReceivingComments") = ddRecvComments.SelectedValue
                End If

                Dim x As String = ds.Tables(0).Rows(i)("ItemID_PO")
                Dim y As String = ds.Tables(0).Rows(i)("ReceivingComments")

                cb.Checked = False
                chkbox_CheckedChanged(cb, Nothing)
            End If

            i = i + 1
        Next
        Session("ds_ReceivingInfo") = ds

    End Sub

 
    Private Function fnCheckIfExists(ByVal spName As String, ByVal ReceiptID As String, ByVal POID As String, ByVal ItemID As String, ByVal Colname As String)
        Try
            Dim Conn As SqlConnection = clsConnection.GetConnection()
            Dim cmd As SqlCommand = New SqlCommand(spName, Conn)
            Dim da As SqlDataAdapter
            Dim ds As New DataSet

            cmd.CommandType = CommandType.StoredProcedure

            cmd.Parameters.Add("@ReceiptID", ReceiptID)
            cmd.Parameters.Add("@POID", POID)
            cmd.Parameters.Add("@ItemID", ItemID)

            da = New SqlDataAdapter(cmd)

            da.Fill(ds)

            Dim col As String = ds.Tables(0).Rows(0)(Colname)
            Session.Add("col", col)
            If col Is Nothing Or col = "" Then
                Return False
            Else
                Return True
            End If

        Catch ex As Exception
            'Return False
        End Try
    End Function
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros