insert dropdownliste
-
I have a problem of integration of the city code in the table of the employer database, it always inserts the last record in the table and here is the city code used:
protected void Page_Load(object sender, EventArgs e)
{
foreach (short vKey in city.Keys)
{
dropdownlist1.Items.Add(city[vKey].cityName);
dropdownlist1.DataTextField = city[vKey].cityName;
dropdownlist1.DataValueField = (city[vKey].id_city).ToString();}
}
and the code in the button is:
employer.cityName= short.Parse(dropdownliste1.DataValueField);
employer.insert(); -
I have a problem of integration of the city code in the table of the employer database, it always inserts the last record in the table and here is the city code used:
protected void Page_Load(object sender, EventArgs e)
{
foreach (short vKey in city.Keys)
{
dropdownlist1.Items.Add(city[vKey].cityName);
dropdownlist1.DataTextField = city[vKey].cityName;
dropdownlist1.DataValueField = (city[vKey].id_city).ToString();}
}
and the code in the button is:
employer.cityName= short.Parse(dropdownliste1.DataValueField);
employer.insert();if(!IsPostBack)
{
foreach (short vKey in city.Keys)
{
dropdownlist1.Items.Add(city[vKey].cityName);
dropdownlist1.DataTextField = city[vKey].cityName;
dropdownlist1.DataValueField = (city[vKey].id_city).ToString();}
}
-
I have a problem of integration of the city code in the table of the employer database, it always inserts the last record in the table and here is the city code used:
protected void Page_Load(object sender, EventArgs e)
{
foreach (short vKey in city.Keys)
{
dropdownlist1.Items.Add(city[vKey].cityName);
dropdownlist1.DataTextField = city[vKey].cityName;
dropdownlist1.DataValueField = (city[vKey].id_city).ToString();}
}
and the code in the button is:
employer.cityName= short.Parse(dropdownliste1.DataValueField);
employer.insert();Wrap in
IsPostback
.protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
foreach (short vKey in city.Keys)
{
dropdownlist1.Items.Add(city[vKey].cityName);
dropdownlist1.DataTextField = city[vKey].cityName;
dropdownlist1.DataValueField = (city[vKey].id_city).ToString();
}
}
}me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum
-
Wrap in
IsPostback
.protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
foreach (short vKey in city.Keys)
{
dropdownlist1.Items.Add(city[vKey].cityName);
dropdownlist1.DataTextField = city[vKey].cityName;
dropdownlist1.DataValueField = (city[vKey].id_city).ToString();
}
}
}me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum
Ignoring the number of seconds, we both replied exactly on the same time :)
-
Ignoring the number of seconds, we both replied exactly on the same time :)
Great minds think alike! I've noticed this before though it doesn't mean anything. I think. Or maybe... :)
me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum
-
Ignoring the number of seconds, we both replied exactly on the same time :)
ok :) , thank you very much for both of you, but :( it does not work with IsPostBack, it always inserts the last record in the table table in town employer, as in adding the new num employer must record the index of the choice of city and not the name of the city
-
I have a problem of integration of the city code in the table of the employer database, it always inserts the last record in the table and here is the city code used:
protected void Page_Load(object sender, EventArgs e)
{
foreach (short vKey in city.Keys)
{
dropdownlist1.Items.Add(city[vKey].cityName);
dropdownlist1.DataTextField = city[vKey].cityName;
dropdownlist1.DataValueField = (city[vKey].id_city).ToString();}
}
and the code in the button is:
employer.cityName= short.Parse(dropdownliste1.DataValueField);
employer.insert();Try:
employer.cityName = dropdownliste1.SelectedItem.Value;
me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum
-
Try:
employer.cityName = dropdownliste1.SelectedItem.Value;
me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum
-
Hmm: well, I was intrigued so I knocked up a wee app to simulate what you are doing and, for me, it works. That is, wrapping with both bits of code. Have you checked to see what it is you're actually outputting to the list; is each id unique or are they all the same? Can't really see a reason why this should not work as advertised. (Course I can't actually see what you are doing!)
me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum
-
I have a problem of integration of the city code in the table of the employer database, it always inserts the last record in the table and here is the city code used:
protected void Page_Load(object sender, EventArgs e)
{
foreach (short vKey in city.Keys)
{
dropdownlist1.Items.Add(city[vKey].cityName);
dropdownlist1.DataTextField = city[vKey].cityName;
dropdownlist1.DataValueField = (city[vKey].id_city).ToString();}
}
and the code in the button is:
employer.cityName= short.Parse(dropdownliste1.DataValueField);
employer.insert();A lot of people have given you the IsPostBack answer, but no one has commented on your loop. What is in your collection and what are you using for your keys? If you had a List<T> you wouldn't have to generate the list of keys and you wouldn't need to loop at all. You could just set the DataSource for the dropdown. If you set a datasource instead of doing Items.Add every time that would also eliminate your duplicates and it would be more efficient the one time it does run (once you had the IsPostBack check).
-
A lot of people have given you the IsPostBack answer, but no one has commented on your loop. What is in your collection and what are you using for your keys? If you had a List<T> you wouldn't have to generate the list of keys and you wouldn't need to loop at all. You could just set the DataSource for the dropdown. If you set a datasource instead of doing Items.Add every time that would also eliminate your duplicates and it would be more efficient the one time it does run (once you had the IsPostBack check).
must use the loop to fill the dropdownlist, and since I work in n-tier gift is not easy to call the database in the datasource in this layer, so you should always call the dictionary but the problem I can not solve is that how to save the id of the city selction instead of city name chosen in the dropdownlist ??????
-
I have a problem of integration of the city code in the table of the employer database, it always inserts the last record in the table and here is the city code used:
protected void Page_Load(object sender, EventArgs e)
{
foreach (short vKey in city.Keys)
{
dropdownlist1.Items.Add(city[vKey].cityName);
dropdownlist1.DataTextField = city[vKey].cityName;
dropdownlist1.DataValueField = (city[vKey].id_city).ToString();}
}
and the code in the button is:
employer.cityName= short.Parse(dropdownliste1.DataValueField);
employer.insert();Okay: think this will do it:
dropdownlist1.Items.Add(new ListItem(city[vKey].cityName, (city[vKey].id_city).ToString()));
I guess we missed that one - you were setting both the text and value to the same thing. If you had stepped through the code or viewed the source on the page you would have probably picked this up - but don't feel bad - several of us missed it as well!
me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum
-
Okay: think this will do it:
dropdownlist1.Items.Add(new ListItem(city[vKey].cityName, (city[vKey].id_city).ToString()));
I guess we missed that one - you were setting both the text and value to the same thing. If you had stepped through the code or viewed the source on the page you would have probably picked this up - but don't feel bad - several of us missed it as well!
me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum
-
must use the loop to fill the dropdownlist, and since I work in n-tier gift is not easy to call the database in the datasource in this layer, so you should always call the dictionary but the problem I can not solve is that how to save the id of the city selction instead of city name chosen in the dropdownlist ??????
I have no idea what that means. I use n-tier architectures all the time and looping is not necessary and neither are dictionaries. A good n-tier architecture has a business layer that contains objects that represent business entities. Using dictionaries is not good practice because you lose some of the benefits from strong typing in C#.