Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 23-04-2009
Member
 
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.
Reply With Quote
  #2  
Old 23-04-2009
Member
 
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);
Reply With Quote
  #3  
Old 23-04-2009
Member
 
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);
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Help to update the DropDownList"
Thread Thread Starter Forum Replies Last Post
How to create collection of dropdownlist, datatextfield ? Jevin Software Development 2 16-06-2009 09:02 AM
Display two columns in dropdownlist Zool Software Development 2 05-06-2009 02:46 PM
How to select dropdownlist default selected Integer Software Development 5 18-03-2009 11:41 PM
Putting a dropdownlist in the column header of a GridView ShonaliB Software Development 4 10-02-2009 06:52 PM
RE: Windows update error 80070103 - Beta 2 Vista NVIDIA Driver Update Nadhe Vista Help 1 28-02-2007 07:56 PM


All times are GMT +5.5. The time now is 10:30 AM.