both dropdownlist changes?

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I have 2 dropdownlist boxes on a Asp web page , they are both bound to a database , each one to a different table. I am using linq to sql to get the info from the table and display the results some text boxes.....the problem is when I select one Item from a dropdown ..it seems to get the info from both tables in the database...I have the code below for each of the dropdown list boxes....the code is in the selected index change event for both dropdown boxes

this is for the dropdown for the customers
using (DB_ClassDataContext myDB = new DB_ClassDataContext(conn))
            {
                var cust = from c in myDB.Customers
                           where c.Company_Name == DropDownList3.Text
                           select new
                           {
                               customer = c.Company_Name,
                               contact = c.Contact_Name,
                               phone = c.Bus_Phone,
                               address = c.Address
                           };


                foreach (var item in cust)
                {
                    txtContactName.Text = item.contact;
                    txtPhone.Text = item.phone;
                    txtCompanyAddress.Text = item.address;
                }
            }


this is the dropdown for the Inventory Item Numbers

 using (DB_ClassDataContext Inv = new DB_ClassDataContext(conn))
            {
                var inventory = from inv in Inv.Inventories
                                where inv.ItemNumber == DropDownList2.Text
                                select new
                                {
                                    itemName = inv.ItemName,
                                    itemDesc = inv.ItemDesc,
                                    itemPrice = inv.ItemPrice
                                };


                foreach (var item in inventory)
                {
                    txtItemName.Text = item.itemName;
                    txtItemDesc.Text = item.itemDesc;
                    txtItemPrice.Text = item.itemPrice.ToString();
                }
            }


Thank you for any help

-InkedGFX
 
when I select one Item from a dropdown ..it seems to get the info from both tables in the database
Can you clarify what you mean by that? On second thought, perhaps you mean both event handlers are called after postback? ASP.NET DropDownList always fires SelectedIndexChanged | NSilverBullet

Also a comment about this part:
foreach (var item in inventory)
{
txtItemName.Text = item.itemName;
txtItemDesc.Text = item.itemDesc;
txtItemPrice.Text = item.itemPrice.ToString();
}
Since these controls can only show one item, why are you looping at all? You should probably limit the Linq expression to .First or .FirstOrDefault to only return one item and assign only that to textboxes.
 
ok...yes I will try to clarify..when I choose an item from one dropdown it gets the info for the item selected...but if there is let say "100-101" item number in the item dropdown then it will return the info for the item number dropdown..this makes no sence because the index never changed for the item drop down list...if that makes any sence!

p.s I will change the code to use .first or .firstordefault......thank you for pointing this out!

-InkedGFX
 
but if there is let say "100-101" item number in the item dropdown then it will return the info for the item number dropdown
Sorry, I don't understand you.
 
ok..let me explain in more detail....

I have 2 dropdown listboxes on the page...one is for a list of customers and the other is for a list of Inventory items....

dropdownlist_Example.jpg

so when I choose a customer from the customer listbox it will get the info for that customer and fill in the textboxes with the info...but what is happenning , is when I choose a customer the info for the item in the item dropdown is also filling in the item textboxes, which it should not be doing....until I choose a item from the item dropdown...because the item dropdown index is not changing...so why would it fill in the info for the item when I change the index in the customer dropdown?

I hope this helps explain it a little better

Thank you for your help

-InkedGFX
 
Did you read the article I linked in post 2? Have you debugged what happens during postbacks?

Since each change also queries a database, have you considered partial postbacks on page with multiple UpdatePanels?
 
I have read the article...allthough I'm quite sure what the author is talking about.....what is a partial postback....?

-InkedGFX
 
ok...I have added a update panel to the page with the dropdownlist, and set it up. works as expected now.....so one problem solved...now there is another issue..which was an issue before I added the update panel , so I dont think the update panel is causing this issue......when I choose a item from the customer dropdown everything works as expected...but when I choose a item from the item number dropdown, nothing happens..I have debugged and set a breakpoint at the selectedindexchanged event for the dropdownlist.....when I run the code the breakpoint is never reached......not sure why this would be.....the event for the customer dropdown is fired when I select a customer.....let me know if you need me to paste the code!

thank you for your help so far.....

-InkedGFX
 
**UPDATE**

I set another breakpoint at the selected index changed event for the item number dropdownlist....and stepped thru it...seems the event is firing but isnt setting the textbox text

here is the code for the seleceted index changed event

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (DB_ClassDataContext Inv = new DB_ClassDataContext(conn))
            {
                var inventory = from inv in Inv.Inventories
                                where inv.ItemNumber == DropDownList2.Text
                                select new
                                {
                                    itemName = inv.ItemName,
                                    itemDesc = inv.ItemDesc,
                                    itemPrice = inv.ItemPrice
                                };


                foreach (var item in inventory)
                {
                    txtItemName.Text = item.itemName;
                    txtItemDesc.Text = item.itemDesc;
                    txtItemPrice.Text = item.itemPrice.ToString();
                }
            }
            
        }


I know I shouldnt use a foreach loop to get the details from the "var inventory" because there will only be one item in inventory but I cannot figure out how to use .default() or .firstordefault()

when I step thru this code I can see the txtItemName.Text is in fact the value it should be from the database as well as the other text fields...but the webpage isnt updated when the selected index changes in the dropdown...any idea why this is?

-InkedGFX
 
I know I shouldnt use a foreach loop to get the details from the "var inventory" because there will only be one item in inventory but I cannot figure out how to use .default() or .firstordefault()
Look at example code:
Enumerable.First
Enumerable.FirstOrDefault
when I step thru this code I can see the txtItemName.Text is in fact the value it should be from the database as well as the other text fields...but the webpage isnt updated when the selected index changes in the dropdown...any idea why this is?
You now have DDL in UpdatePanel, are those textboxes also in same panel? If not they will not be included in that partial page update.

If placing them in same UpdatePanel is awkward you can place them in a different UpdatePanel that has UpdateMode=Conditional and call Update method for that panel when you need that to update too, or simply add the DDL.SelectedIndexChanged to the other panels Triggers collection.
 
when I add the textboxes to the update panel I lose the items in the dropdownlist...the dropdownlist is databound to the Item table in the database......if I dont have the textboxes in the updatepanel all the items are in the dropdownlist...weird!!!!!

-InkedGFX
 
also, Im not quite sure how to use .first() with what I have....if I had an array of ints I can see how this would work ...but I have an object with 3 attributes

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (DB_ClassDataContext Inv = new DB_ClassDataContext(conn))
            {
                var inventory = from inv in Inv.Inventories
                                where inv.ItemNumber == DropDownList2.Text
                                select new
                                {
                                    itemName = inv.ItemName,
                                    itemDesc = inv.ItemDesc,
                                    itemPrice = inv.ItemPrice
                                };


                foreach (var item in inventory)
                {
                    txtItemName.Text = item.itemName;
                    txtItemDesc.Text = item.itemDesc;
                    txtItemPrice.Text = item.itemPrice.ToString();
                }
            }
            
        }


how would I get itemName , itemDesc and itemPrice with .first()?

again thank you for your help!

-InkedGFX
 
Ok..I figured out how to use .First() with your help.....but the text fields still arent being updated , I have them in the same update panel as the dropdown list....below is the html code

HTML:
<div class="panel-body">                          
  <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">                             
   <ContentTemplate>                             
     <div class="col-md-4">                              
       <div class="input-group left-margin-20">                              
        <span class="input-group-addon" id="basic-addon13">Item Number :</span>                                                                       
       <asp:DropDownList ID="DropDownList2" runat="server" CssClass="form-control" Width="100px" DataSourceID="SqlDataSource3" DataTextField="ItemNumber" DataValueField="ItemNumber" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" OnTextChanged="DropDownList2_TextChanged" AutoPostBack="true">                                                                            
  </asp:DropDownList>                                       
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [ItemNumber] FROM [Inventory]">  </asp:SqlDataSource>                               
 </div>                                
  <div class="input-group left-margin-20">                                 
  <span class="input-group-addon" id="basic-addon9">Item Desc:</span>                                  
   <asp:TextBox ID="txtItemDesc" runat="server" CssClass="form-control" Height="40px" Width="150px" TextMode="MultiLine"></asp:TextBox>                               
    </div>                                 
     <asp:Button ID="btnAddNewItem" CssClass="btn btn-default left-margin-30" runat="server" Text="Add New Item" OnClick="btnAddNewItem_Click" />                                 
    <asp:Label ID="lblWarning" runat="server" Text="Error Adding New Item!" Visible="False" BackColor="#FF9900" BorderColor="#FF9900" Font-Bold="True"></asp:Label>                         </div>                                                      
    <div class="col-md-4">                                 
     <div class="input-group left-margin-20">                                    
       <span class="input-group-addon" id="basic-addon14">Item Name :</span>                                    
       <asp:TextBox ID="txtItemName" runat="server" CssClass="form-control" Height="40px" Width="170" TextMode="SingleLine"></asp:TextBox>                                 
    </div>
     <div class="input-group left-margin-20">                                   
      <span class="input-group-addon" id="basic-addon11">Item Price :</span>                                   
      <asp:TextBox ID="txtItemPrice" runat="server" CssClass="form-control" Height="40px" Width="100px" TextMode="SingleLine"></asp:TextBox>                                   
      </div>                            
   </div>                             
    <div class="col-md-4">                               
     <div class="input-group">                                    
      <span class="input-group-addon" id="basic-addon10">Item Qty :</span>                                    
      <asp:TextBox ID="txtItemQty" runat="server" CssClass="form-control" Height="40px" Width="150" TextMode="SingleLine"></asp:TextBox>                               
    </div>                                                              
  </div>                            
 </ContentTemplate>                                    
  <Triggers>                                       
    <asp:AsyncPostBackTrigger ControlID="DropDownList2" EventName="SelectedIndexChanged" />                                   
  </Triggers>                          
 </asp:UpdatePanel>                                
</div>

and here is the selected index change event code

   protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (DB_ClassDataContext Inv = new DB_ClassDataContext(conn))
            {
                var inventory = (from inv in Inv.Inventories
                                where inv.ItemNumber == DropDownList2.Text
                                select new
                                {
                                    itemName = inv.ItemName,
                                    itemDesc = inv.ItemDesc,
                                    itemPrice = inv.ItemPrice
                                }).First();




                var itemname = inventory.itemName;
                var itemdesc = inventory.itemDesc;
                var itemprice = inventory.itemPrice;


                txtItemName.Text = itemname;
                txtItemDesc.Text = itemdesc;
                txtItemPrice.Text = "$" + itemprice.ToString();
               
            }
            
        }


I have stepped thru this event and all seems to be working as expected.....hopefully you can see why it isn't working....

Thank You

-InkedGFX
 
Last edited:
im an idiot....the problem was the "UpdateMode" in the updatepanel..I had it set to "Always" and it needs to be "Conditional" so everything is working now....thank you for your help..I learned something new with your help!

-InkedGFX
 
Back
Top Bottom