Results 1 to 6 of 6

Thread: Using a value from database on page load in c#

  1. #1
    Join Date
    Dec 2004
    Posts
    2,040
    Rep Power
    22

    Default Using a value from database on page load in c#

    I have a dropdownlist (let's call it combo1) which is databound, now i'd like to use a value from it in other fields such as in a label as well as add it to an href which will be made on page load. Problem is I either have to reload the page before I can use combo1.SelectedValue. Is there a way to get a value from the database to use in the dropdownlist and the href or a statement like combo1.items.selectedindex.text that will give me the value right away without doing some sort of postback?
    This posting is provided "AS IS" with no warranties, and confers no rights.
    You assume all risk for your use. © 2006 Cloud Solutions. All rights reserved.

  2. #2
    Join Date
    May 2007
    Posts
    45
    Rep Power
    0

    Default

    I'm not completely sure what you're asking. Could you post a sample of the code?

    is the drop down populated on form load before you try to use an item from it?

    If so is the selectedIndex set to 0?

    If not, are you sure the comboBox datasource set on load?

  3. #3
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    The SelectedIndex at that time might be -1 (no item selected), which should mean SelectedValue is null also. I am not too sure.

    Did you try defaulting the selected index to some exiting item (meaning the list is not empty).

    Did you check if selected index is -1 before trying to get selected value?
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  4. #4
    Join Date
    Dec 2004
    Posts
    2,040
    Rep Power
    22

    Default

    Hm, I just tried a different method to solve the problem instead of using the database and it worked, I had tried it when I tried the database way and it didn't work then, perhaps I was hungry or tired, tomorrow i'll try back the database method and see whether there really was a problem but thanks for the responses though.
    Last edited by Cloud; Jan 28, 2009 at 10:10 AM.
    This posting is provided "AS IS" with no warranties, and confers no rights.
    You assume all risk for your use. © 2006 Cloud Solutions. All rights reserved.

  5. #5
    Join Date
    Dec 2004
    Posts
    2,040
    Rep Power
    22

    Default

    Made a new post rather than edit old post cuz editing doesn't show up as a new post from the main page.

    Quote Originally Posted by icymint3 View Post
    The SelectedIndex at that time might be -1 (no item selected), which should mean SelectedValue is null also. I am not too sure.

    Did you try defaulting the selected index to some exiting item (meaning the list is not empty).

    Did you check if selected index is -1 before trying to get selected value?
    Ok, SelectedIndex is -1, I tried setting it to 1 but that's a no go, what would be the next step?

    Here's a snippet of the code, I colored the parts I use the value from the combo red:
    protected void Page_Load(object sender, EventArgs e)
    {
    DropDownList1.SelectedIndex = 1;
    Label2.Text = "Combo Value: " + DropDownList1.SelectedValue;
    Label4.Text = DropDownList1.SelectedIndex.ToString();
    foreach (Shape shape in MapControl1.Shapes)
    {
    shape.Href = "country.aspx?country=" + shape.Name + "&year=" + DropDownList1.SelectedValue + "&filter=" + RadioButtonList1.SelectedValue.ToUpper() + "&purpose=" + DropDownList2.SelectedValue;
    }
    }
    Ok, figured out how to databind in the codebehind, added the following code (deleted my query) and now i'm getting what I needed, again thanks much for the help cuz I would've just given up on the code.\
    string query = "";
    SqlDataSource3.SelectCommand = query;
    SqlDataSource3.SelectCommandType = SqlDataSourceCommandType.Text;
    SqlDataSource3.DataBind();
    DropDownList1.DataBind();
    Last edited by Cloud; Jan 28, 2009 at 10:21 AM.
    This posting is provided "AS IS" with no warranties, and confers no rights.
    You assume all risk for your use. © 2006 Cloud Solutions. All rights reserved.

  6. #6
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    sorry bout the delay, been very busy...

    check the IsPostback property in page load and only load the ddl when its not a postback. you may also want to select a default item from the list after putting(databind) some items in the list.

    based on how your code is, i dont think the href makes any sense if the user has not selected an item from the drop down list.
    so use a button click event or ddl selection change event to set the link.

    additonally you might consider :
    1. adding a default item to the list (ddl.Items.Insert(0, " select an option "), 2. selecting the default item by default (ddl.selectedindex = 0),
    3. use a validator control to ensure user has changed selection from the default item.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •