Results 1 to 3 of 3

Thread: Help to update the DropDownList

  1. #1
    Join Date
    Mar 2009
    Posts
    1,221

    Help to update the DropDownList

    I have a page with two DropDownList. One of them populating the list in Page_Load () with information retrieved from a database.

    What I wish is that I select an item in DropDownList min can use what's in it in an SQL query for populating the second box.

    Code:
    protected void Page_Load (object sender, EventArgs e) 
    {
    dbConn = tcObj.returnConnection (); 
    try 
    {
    dbConn.Open (); 
    }
    catch (Exception connectionException) () 
    populateClientDropdown (); 
    }
    Code:
    private void populateClientDropdown () 
    {
    clientDownList.Items.Clear (); 
    executeReader ( "SELECT Company FROM Client"); 
    dropDownElements.Clear (); 
    
    while (dbReader.Read ()) 
    {
    dropDownElements.Add (dbReader [ "Company"]. ToString ()); 
    }
    
    dbReader.Close (); 
    
    for (int i = 0; i <dropDownElements.Count; i + +) 
    {
    Listitem lstItem = new Listitem (Drop Down Elements [i]. ToString ()); 
    clientDownList.Items.Add (lstItem); 
    }
    }
    Code:
    private void populateSubClientDropdown () 
    {
    subClientDropDownList.Items.Clear (); 
    executeReader ( "SELECT Client_Location.Sub_Company_Name FROM Client_Location, Client WHERE Client_Location.Client_ID = Client.ID_Client AND Client.Company = '" + clientDownList.Text +"'"); 
    dropDownElements.Clear (); 
    
    while (dbReader.Read ()) 
    {
    dropDownElements.Add (dbReader [ "Sub_Company_Name"]. ToString ()); 
    }
    
    for (int i = 0; i <dropDownElements.Count; i + +) 
    { 
    Listitem lstItem = new Listitem (Drop Down Elements [i]. ToString ()); 
    subClientDropDownList.Items.Add (lstItem); 
    }
    }
    Code:
    protected void clientDownList_SelectedIndexChanged (object sender, EventArgs e) 
    {
    populateSubClientDropdown (); 
    }
    Code:
    <asp: DropDownList ID = "client Down List" runat = "server" Auto Post Back = "True" 
    onselectedindexchanged = "clientDownList_SelectedIndexChanged"> 
    </asp: DropDownList>
    It's happening now is that the Page_Load () is run every time I change the item in the DropDownList min, resulting in the error value is used when using clientDownList.Text in my query.

  2. #2
    Join Date
    Feb 2008
    Posts
    194

    Re: Help to update the DropDownList

    Think you may well start by adding an "if (dropDownElements.Count == 0)" around the call to populateClientDropdown - otherwise you'll ask to do it every post back - the Page_Load is called each post back.

    Otherwise: Make SQL injection ..
    Parameter your always always always always all sql parameters:

    Code:
    SqlCommand cmd = conn.CreateCommand (); 
    cmd.CommandText = "SELECT Client_Location.Sub_Company_Name FROM Client_Location, Client WHERE Client_Location.Client_ID = Client.ID_Client AND Client.Company = @ Company"; 
    cmd.Parameters.AddWithValue ( "@ Company", clientDownList.Text);

  3. #3
    Join Date
    Mar 2009
    Posts
    1,221

    Re: Help to update the DropDownList

    This solved the problem:

    Code:
    private void populateClientDropdown () 
    {
    if (clientDownList.Text == "") 
    {
    clientDownList.Items.Clear (); 
    executeReader (getClientString); 
    dropDownElements.Clear (); 
    
    while (dbReader.Read ()) 
    {
    dropDownElements.Add (dbReader [ "Company"]. ToString ()); 
    }
    
    dbReader.Close (); 
    
    for (int i = 0; i <dropDownElements.Count; i++) 
    {
    Listitem lstItem = new Listitem (Drop Down Elements [i]. ToString ()); 
    clientDownList.Items.Add (lstItem); 
    }
    }
    }
    I am 110% sure that the users of the system can not use SQL. When I programmed my webpage I used PHP's built-in feature to avoid this problem:

    Code:
    $name_bad = mysql_real_escape_string($name_bad);

Similar Threads

  1. How to create collection of dropdownlist, datatextfield ?
    By Jevin in forum Software Development
    Replies: 2
    Last Post: 16-06-2009, 09:02 AM
  2. Display two columns in dropdownlist
    By Zool in forum Software Development
    Replies: 2
    Last Post: 05-06-2009, 02:46 PM
  3. How to select dropdownlist default selected
    By Integer in forum Software Development
    Replies: 5
    Last Post: 18-03-2009, 11:41 PM
  4. Putting a dropdownlist in the column header of a GridView
    By ShonaliB in forum Software Development
    Replies: 4
    Last Post: 10-02-2009, 06:52 PM
  5. Replies: 1
    Last Post: 28-02-2007, 07:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,642,666,675.53224 seconds with 17 queries