Drop Down List problem

V

Viktor Popov

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
 
R

ranganh

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.
 
V

Viktor Popov

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 said:
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 said:
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
 
R

ranganh

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 said:
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 said:
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 said:
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
 
V

Viktor Popov

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

Regards,
Viktor

ranganh said:
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 said:
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 said:
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.

:

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

DropDownList problem 0
Code Error 16
Drop Down list question 3
DropDownList problem 0
drop list question 2
Drop Down list 3
dynamic drop down list problem 3
SELECT drop down list - SKIP 2

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top