I want fill dependent dropdown by XML file which be create(data by sql server) on session intialization.
Loading
I want fill dependent dropdown by XML file which be create(data by sql server) on session intialization.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tahir AnsariRank #366 · 6.8K reputationPosted Jul 27, 2023, 6:05 AM EDT
Hello Yash Singh Try below sample code:
Convert the query result into XML format using SQL Server's
FOR XMLclauseSELECT ProductName, Price FROM products FOR XML AUTO, ELEMENTS; --output--- Widget C14.95Widget D34.99Gadget Z69.95Gizmo 12339.99Thingamajig12.49Doodad 50059.95Gadget A122.99Widget Z244.99Gizmo 78949.95Gadget 123X55.99Widget D427.95Thingamabob 2.019.99Gadget XYZ64.99Widget E17.95Thingy 529.99Gadget AB54.95Widget LMN39.99Gizmo 24649.95Gadget 789Q59.99Widget F122.99Gadget RTX44.99Gizmo 11149.95Thingy 1034.99Widget Z927.95Thingamajig 3.024.99Alovera299.00Save the XML result into an XML file using C# code or any other preferred method.
2. Load XML Data on Session Initialization:
using System.IO; using System.Xml.Linq; // During session initialization string xmlFilePath = "path/to/your/xml/file.xml"; Dictionary> dependentData = new Dictionary>(); if (File.Exists(xmlFilePath)) { XDocument xmlDoc = XDocument.Load(xmlFilePath); foreach (var element in xmlDoc.Root.Elements()) { string parentValue = element.Attribute("Column1").Value; List childValues = element.Elements("Column2").Select(e => e.Value).ToList(); dependentData[parentValue] = childValues; } }Html + Javascript code
Select Parent Option Select Dependent Option // JavaScript/jQuery code $(document).ready(function () { // Function to populate the dependent dropdown function populateDependentDropdown(parentValue) { // Get child values for the selected parent from the data structure (e.g., session) var childValues = /* Retrieve child values for parentValue from your data structure */; // Clear existing options and populate the dependent dropdown var dependentDropdown = $("#dependentDropdown"); dependentDropdown.empty(); $.each(childValues, function (index, value) { dependentDropdown.append($('').text(value).attr('value', value)); }); } // Event handler for parent dropdown selection change $("#parentDropdown").on("change", function () { var selectedParentValue = $(this).val(); if (selectedParentValue !== "") { // Populate the dependent dropdown when the parent dropdown selection changes populateDependentDropdown(selectedParentValue); } }); });adjust the according to your requirement!
Vishal YelveRank #178 · 13.6K reputationPosted Jul 28, 2023, 10:00 AM EDT
Hi Yash,
do refer below article
https://www.c-sharpcorner.com/uploadfile/b19d5a/dropdownlist-data-binding-from-xml-file-in-Asp-Net/