Drop Down List problem

Discussion in 'ASP .Net Web Controls' started by Viktor Popov, Sep 4, 2004.

  1. Viktor Popov

    Viktor Popov Guest

    Hi,
    I have a WebForm on which I have 2 buttons: "Retreive" and "Insert"
    When I push "Retreive" I would like to retreive info from my DataBase and to
    make a DropDownList- DDL1 to show the result. The code is:
    private void Retreive_Click(object sender, System.EventArgs e)
    {
    ...........connection to DB, etc.
    DDL1.Items.FindByText(DS.Tables[0].Rows[0][0].ToString()).Selected=true;
    .......
    }
    It works. When I click on the button the exact information shows up in the
    DDL1.
    Than I would like to click on the "Insert" button and to insert the same
    info or other if I change the Item in DDL1. I use this code and I know that
    something is wrong, but I don't know what exactly, because when I push the
    insert Button the first Item in DDL1 is always saved .

    private void Insert_Click(object sender, System.EventArgs e)
    {
    ...........connection to DB, etc.
    cmd.Parameters.Add(new SqlParameter("@ESTTYPEID", SqlDbType.TinyInt));
    cmd.Parameters["@ESTTYPEID"].Value = Byte.Parse(DDL1.SelectedItem.Value);
    .......
    }

    Could you tell me how could be accomplished my goal?

    Thank you in advance!

    Viktor



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.751 / Virus Database: 502 - Release Date: 02.9.2004 a.
     
    Viktor Popov, Sep 4, 2004
    #1
    1. Advertising

  2. Viktor Popov

    ranganh Guest

    Dear Viktor,

    You said you are populating the DDL1 on the click of the retrieve button.
    till that time is your dropdown list empty? or you populate the dropdownlist
    in the page_load event and then change the contents on clicking the retrieve
    button.

    if you are populating it in the page_load event, you need to put it within the

    If !(IsPostBack)
    {

    }

    this is because, once you click the insert button, the page is posted back
    and the dropdownlist will be refilled if you don't put it within the above
    code block.

    if you are not populating it in the page_load, try printing the selected
    value before inserting it into the database.

    thanks.

    "Viktor Popov" wrote:

    > Hi,
    > I have a WebForm on which I have 2 buttons: "Retreive" and "Insert"
    > When I push "Retreive" I would like to retreive info from my DataBase and to
    > make a DropDownList- DDL1 to show the result. The code is:
    > private void Retreive_Click(object sender, System.EventArgs e)
    > {
    > ...........connection to DB, etc.
    > DDL1.Items.FindByText(DS.Tables[0].Rows[0][0].ToString()).Selected=true;
    > .......
    > }
    > It works. When I click on the button the exact information shows up in the
    > DDL1.
    > Than I would like to click on the "Insert" button and to insert the same
    > info or other if I change the Item in DDL1. I use this code and I know that
    > something is wrong, but I don't know what exactly, because when I push the
    > insert Button the first Item in DDL1 is always saved .
    >
    > private void Insert_Click(object sender, System.EventArgs e)
    > {
    > ...........connection to DB, etc.
    > cmd.Parameters.Add(new SqlParameter("@ESTTYPEID", SqlDbType.TinyInt));
    > cmd.Parameters["@ESTTYPEID"].Value = Byte.Parse(DDL1.SelectedItem.Value);
    > .......
    > }
    >
    > Could you tell me how could be accomplished my goal?
    >
    > Thank you in advance!
    >
    > Viktor
    >
    >
    >
    > ---
    > Outgoing mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.751 / Virus Database: 502 - Release Date: 02.9.2004 a.
    >
    >
    >
     
    ranganh, Sep 4, 2004
    #2
    1. Advertising

  3. Viktor Popov

    Viktor Popov Guest

    Hi,

    Thank you very much! It works . I insertes if(!IsPostBack) and it works:)
    Could you explain me what for is IsPostBack?

    Regards,
    Viktor
    "ranganh" <> wrote in message
    news:...
    > Dear Viktor,
    >
    > You said you are populating the DDL1 on the click of the retrieve button.
    > till that time is your dropdown list empty? or you populate the

    dropdownlist
    > in the page_load event and then change the contents on clicking the

    retrieve
    > button.
    >
    > if you are populating it in the page_load event, you need to put it within

    the
    >
    > If !(IsPostBack)
    > {
    >
    > }
    >
    > this is because, once you click the insert button, the page is posted back
    > and the dropdownlist will be refilled if you don't put it within the above
    > code block.
    >
    > if you are not populating it in the page_load, try printing the selected
    > value before inserting it into the database.
    >
    > thanks.
    >
    > "Viktor Popov" wrote:
    >
    > > Hi,
    > > I have a WebForm on which I have 2 buttons: "Retreive" and "Insert"
    > > When I push "Retreive" I would like to retreive info from my DataBase

    and to
    > > make a DropDownList- DDL1 to show the result. The code is:
    > > private void Retreive_Click(object sender, System.EventArgs e)
    > > {
    > > ...........connection to DB, etc.
    > > DDL1.Items.FindByText(DS.Tables[0].Rows[0][0].ToString()).Selected=true;
    > > .......
    > > }
    > > It works. When I click on the button the exact information shows up in

    the
    > > DDL1.
    > > Than I would like to click on the "Insert" button and to insert the same
    > > info or other if I change the Item in DDL1. I use this code and I know

    that
    > > something is wrong, but I don't know what exactly, because when I push

    the
    > > insert Button the first Item in DDL1 is always saved .
    > >
    > > private void Insert_Click(object sender, System.EventArgs e)
    > > {
    > > ...........connection to DB, etc.
    > > cmd.Parameters.Add(new SqlParameter("@ESTTYPEID", SqlDbType.TinyInt));
    > > cmd.Parameters["@ESTTYPEID"].Value =

    Byte.Parse(DDL1.SelectedItem.Value);
    > > .......
    > > }
    > >
    > > Could you tell me how could be accomplished my goal?
    > >
    > > Thank you in advance!
    > >
    > > Viktor
    > >
    > >
    > >
    > > ---
    > > Outgoing mail is certified Virus Free.
    > > Checked by AVG anti-virus system (http://www.grisoft.com).
    > > Version: 6.0.751 / Virus Database: 502 - Release Date: 02.9.2004 a.
    > >
    > >
    > >



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.751 / Virus Database: 502 - Release Date: 02.9.2004 a.
     
    Viktor Popov, Sep 4, 2004
    #3
  4. Viktor Popov

    ranganh Guest

    Dear Viktor,

    You'r welcome! The IsPostBack is the condition we check whether the page is
    loading for the first time or it has been posted back.

    In ASP.NET, by the forms, by default postback to itself and even the server
    controls do postback to the server to perform any action.

    So, everytime, the page is posted back, the page is again loaded right? If
    you put it simply as on page_load, then everytime the code would execute
    resulting the dropdownlist being refilled everytime.

    IF you put it within ! IsPostBack, then we specify that the dropdownlist
    need to be filled only the first time, that is the page is loading for the
    first time and is not posted back.

    Hope it clarifies.



    "Viktor Popov" wrote:

    > Hi,
    >
    > Thank you very much! It works . I insertes if(!IsPostBack) and it works:)
    > Could you explain me what for is IsPostBack?
    >
    > Regards,
    > Viktor
    > "ranganh" <> wrote in message
    > news:...
    > > Dear Viktor,
    > >
    > > You said you are populating the DDL1 on the click of the retrieve button.
    > > till that time is your dropdown list empty? or you populate the

    > dropdownlist
    > > in the page_load event and then change the contents on clicking the

    > retrieve
    > > button.
    > >
    > > if you are populating it in the page_load event, you need to put it within

    > the
    > >
    > > If !(IsPostBack)
    > > {
    > >
    > > }
    > >
    > > this is because, once you click the insert button, the page is posted back
    > > and the dropdownlist will be refilled if you don't put it within the above
    > > code block.
    > >
    > > if you are not populating it in the page_load, try printing the selected
    > > value before inserting it into the database.
    > >
    > > thanks.
    > >
    > > "Viktor Popov" wrote:
    > >
    > > > Hi,
    > > > I have a WebForm on which I have 2 buttons: "Retreive" and "Insert"
    > > > When I push "Retreive" I would like to retreive info from my DataBase

    > and to
    > > > make a DropDownList- DDL1 to show the result. The code is:
    > > > private void Retreive_Click(object sender, System.EventArgs e)
    > > > {
    > > > ...........connection to DB, etc.
    > > > DDL1.Items.FindByText(DS.Tables[0].Rows[0][0].ToString()).Selected=true;
    > > > .......
    > > > }
    > > > It works. When I click on the button the exact information shows up in

    > the
    > > > DDL1.
    > > > Than I would like to click on the "Insert" button and to insert the same
    > > > info or other if I change the Item in DDL1. I use this code and I know

    > that
    > > > something is wrong, but I don't know what exactly, because when I push

    > the
    > > > insert Button the first Item in DDL1 is always saved .
    > > >
    > > > private void Insert_Click(object sender, System.EventArgs e)
    > > > {
    > > > ...........connection to DB, etc.
    > > > cmd.Parameters.Add(new SqlParameter("@ESTTYPEID", SqlDbType.TinyInt));
    > > > cmd.Parameters["@ESTTYPEID"].Value =

    > Byte.Parse(DDL1.SelectedItem.Value);
    > > > .......
    > > > }
    > > >
    > > > Could you tell me how could be accomplished my goal?
    > > >
    > > > Thank you in advance!
    > > >
    > > > Viktor
    > > >
    > > >
    > > >
    > > > ---
    > > > Outgoing mail is certified Virus Free.
    > > > Checked by AVG anti-virus system (http://www.grisoft.com).
    > > > Version: 6.0.751 / Virus Database: 502 - Release Date: 02.9.2004 a.
    > > >
    > > >
    > > >

    >
    >
    > ---
    > Outgoing mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.751 / Virus Database: 502 - Release Date: 02.9.2004 a.
    >
    >
    >
     
    ranganh, Sep 6, 2004
    #4
  5. Viktor Popov

    Viktor Popov Guest

    Thank you again, Ranganh! Everything is clear now!:)

    Regards,
    Viktor

    "ranganh" <> wrote in message
    news:...
    > Dear Viktor,
    >
    > You'r welcome! The IsPostBack is the condition we check whether the page

    is
    > loading for the first time or it has been posted back.
    >
    > In ASP.NET, by the forms, by default postback to itself and even the

    server
    > controls do postback to the server to perform any action.
    >
    > So, everytime, the page is posted back, the page is again loaded right?

    If
    > you put it simply as on page_load, then everytime the code would execute
    > resulting the dropdownlist being refilled everytime.
    >
    > IF you put it within ! IsPostBack, then we specify that the dropdownlist
    > need to be filled only the first time, that is the page is loading for the
    > first time and is not posted back.
    >
    > Hope it clarifies.
    >
    >
    >
    > "Viktor Popov" wrote:
    >
    > > Hi,
    > >
    > > Thank you very much! It works . I insertes if(!IsPostBack) and it

    works:)
    > > Could you explain me what for is IsPostBack?
    > >
    > > Regards,
    > > Viktor
    > > "ranganh" <> wrote in message
    > > news:...
    > > > Dear Viktor,
    > > >
    > > > You said you are populating the DDL1 on the click of the retrieve

    button.
    > > > till that time is your dropdown list empty? or you populate the

    > > dropdownlist
    > > > in the page_load event and then change the contents on clicking the

    > > retrieve
    > > > button.
    > > >
    > > > if you are populating it in the page_load event, you need to put it

    within
    > > the
    > > >
    > > > If !(IsPostBack)
    > > > {
    > > >
    > > > }
    > > >
    > > > this is because, once you click the insert button, the page is posted

    back
    > > > and the dropdownlist will be refilled if you don't put it within the

    above
    > > > code block.
    > > >
    > > > if you are not populating it in the page_load, try printing the

    selected
    > > > value before inserting it into the database.
    > > >
    > > > thanks.
    > > >
    > > > "Viktor Popov" wrote:
    > > >
    > > > > Hi,
    > > > > I have a WebForm on which I have 2 buttons: "Retreive" and "Insert"
    > > > > When I push "Retreive" I would like to retreive info from my

    DataBase
    > > and to
    > > > > make a DropDownList- DDL1 to show the result. The code is:
    > > > > private void Retreive_Click(object sender, System.EventArgs e)
    > > > > {
    > > > > ...........connection to DB, etc.
    > > > >

    DDL1.Items.FindByText(DS.Tables[0].Rows[0][0].ToString()).Selected=true;
    > > > > .......
    > > > > }
    > > > > It works. When I click on the button the exact information shows up

    in
    > > the
    > > > > DDL1.
    > > > > Than I would like to click on the "Insert" button and to insert the

    same
    > > > > info or other if I change the Item in DDL1. I use this code and I

    know
    > > that
    > > > > something is wrong, but I don't know what exactly, because when I

    push
    > > the
    > > > > insert Button the first Item in DDL1 is always saved .
    > > > >
    > > > > private void Insert_Click(object sender, System.EventArgs e)
    > > > > {
    > > > > ...........connection to DB, etc.
    > > > > cmd.Parameters.Add(new SqlParameter("@ESTTYPEID",

    SqlDbType.TinyInt));
    > > > > cmd.Parameters["@ESTTYPEID"].Value =

    > > Byte.Parse(DDL1.SelectedItem.Value);
    > > > > .......
    > > > > }
    > > > >
    > > > > Could you tell me how could be accomplished my goal?
    > > > >
    > > > > Thank you in advance!
    > > > >
    > > > > Viktor
    > > > >
    > > > >
    > > > >
    > > > > ---
    > > > > Outgoing mail is certified Virus Free.
    > > > > Checked by AVG anti-virus system (http://www.grisoft.com).
    > > > > Version: 6.0.751 / Virus Database: 502 - Release Date: 02.9.2004 a.
    > > > >
    > > > >
    > > > >

    > >
    > >
    > > ---
    > > Outgoing mail is certified Virus Free.
    > > Checked by AVG anti-virus system (http://www.grisoft.com).
    > > Version: 6.0.751 / Virus Database: 502 - Release Date: 02.9.2004 a.
    > >
    > >
    > >



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.752 / Virus Database: 503 - Release Date: 03.9.2004 a.
     
    Viktor Popov, Sep 7, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SirPoonga
    Replies:
    2
    Views:
    780
    Ben Strackany
    Jan 7, 2005
  2. weiwei
    Replies:
    0
    Views:
    1,032
    weiwei
    Jan 5, 2007
  3. msimmons
    Replies:
    0
    Views:
    497
    msimmons
    Jul 16, 2009
  4. Replies:
    5
    Views:
    282
  5. Replies:
    3
    Views:
    296
Loading...

Share This Page