941,512 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 418
  • C# RSS
Jul 19th, 2010
0

Populate DropDown from database

Expand Post »
I've got a DropDown which I populate from a database. When an item is selected I want to redirect to another page with the selected id, but the problem is it passes "1" no matter what was selected.

C# Syntax (Toggle Plain Text)
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. Populate();
  4. }
  5.  
  6. public void Populate()
  7. {
  8. OleDbConnection con = new OleDbConnection(
  9. "Provider=Microsoft.Jet.OLEDB.4.0; " +
  10. "Data Source=" + Server.MapPath("connect.mdb"));
  11.  
  12. con.Open();
  13.  
  14. string strQuery = "SELECT A_ID, Title_eng FROM C_about";
  15. OleDbCommand cmd = new OleDbCommand(strQuery, con);
  16. OleDbDataReader reader = cmd.ExecuteReader();
  17.  
  18. titleSelection.DataSource = reader;
  19. titleSelection.DataValueField = "A_ID";
  20. titleSelection.DataTextField = "Title_eng";
  21. titleSelection.DataBind();
  22.  
  23. con.Close();
  24. con.Dispose();
  25. }
  26.  
  27. protected void titleSelection_SelectedIndexChanged(object sender, EventArgs e)
  28. {
  29. string A_ID = titleSelection.SelectedValue.ToString();
  30.  
  31. Response.Redirect("aboutEdit.aspx?A_ID=" + A_ID);
  32. }

this is the view source:
Title:<br />
<select name="titleSelection" onchange="javascript:setTimeout('__doPostBack(\'titleSelection\',\'\')', 0)" id="titleSelection">
<option selected="selected" value="1">WHO WE ARE</option>
<option value="2">WHAT WE DO</option>

<option value="3">Permanent Placements</option>
<option value="4">Temporary staff</option>
<option value="5">Training &amp; Development</option>
<option value="6">CV Writing Tips</option>
<option value="7">Jobs Titles &amp; Descriptions</option>

<option value="8">Looking for Talent?</option>
<option value="9">Looking for a Job</option>
<option value="10">Register</option>
Reputation Points: 6
Solved Threads: 0
Junior Poster
jellybeannn is offline Offline
108 posts
since Mar 2010
Jul 19th, 2010
2
Re: Populate DropDown from database
Hello jellybeannn,
I guess you have to add to Page_Load event couple strings:
C# Syntax (Toggle Plain Text)
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if(!IsPostBack)
  4. Populate();
  5. }

It should help you.
Reputation Points: 11
Solved Threads: 9
Light Poster
Rogachev is offline Offline
44 posts
since Nov 2008
Jul 19th, 2010
0
Re: Populate DropDown from database
Thanks man, it solved it.
Reputation Points: 6
Solved Threads: 0
Junior Poster
jellybeannn is offline Offline
108 posts
since Mar 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Publishing an application
Next Thread in C# Forum Timeline: C# Image.Equals NullReferenceException Error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC