Results 1 to 9 of 9
  1. #1

    Problems databinding to a drop down list C#

    I am having some trouble binding a dataview to a drop down list control. The code I have for binding the data to the control is as follows:
    // Build Country Codes
    System.Data.DataView CountryCodes = AccountInfoSessionClass.BuildCountryCodes();
    droplistCountryCode.DataSource = CountryCodes;
    droplistCountryCode.DataTextField = "CountryNames";
    droplistCountryCode.DataValueField = "CountryCodes";
    The code for the BuildCountryCodesFunction is as follows:

    public System.Data.DataView BuildCountryCodes()
    	// Declare String for Country Names and Codes
    	string CountryCodes =
    		"Algeria,DZ;Benin,BJ;Burkina Faso,BF;Burundi,BI;Cameroon,CM;CapeVerde,CV;Central African Replubic,CF;Chad,TD;Cameros,KM;" +
    				"Congo,CG;Cote D'Ivoire,CI;Djibouti,DJ;Equatorial Guinea,GQ;Eritrea,ER;Ethiopia,ET;Egypt,EG;Gabon,GA;Gambi,GM;" +
    				"Ghana,GH;Guinea,GN;Guinea-Bissau,GW;Kenya,KE;Lesotho,LS;Liberia,LR;Madagascar,MG;Mali,ML;Mauritania,MR;" +
    				"Mayotte,YT;Morocco,MA;Mozambique,MZ;Malawi,MW;Namibia,NA;Niger,NE;Nigeria,NG;Reunion,RE;St. Helena,SH;" +
    				"Sao Tome and Principe,ST;Senegal,SN;Sierra Leone,SL;Somalia,SO;South Africa,ZA;Sudan,SD;" +
    				"Swaziland,SZ;Tanzania,TZ;Togo,TG;Uganda,UG;Western Sahara,EH;Zaire,ZR;Zambia,ZM;Zimbabwe,ZW;" +
    				"Antarctica,AQ;Afghanistan,AF;Bangladesh,BD;Bhutan,BT;Brunel,BN;Cambodia,KH;China,CN;Hong Kong,HK;India,IN;Indonesia,ID;" +
    				"Japan,JP;Kazakhastan,KZ;Kyrgyzstan,KG;Laos,LA;Macau,MO;Malaysia,MY;Maldives,MV;Mongolia,MN;Nepal,NP;" +
    				"Pakistan,PK;Philippines,PH;Replublic of Korea,KR;Russia,RU;Seychelles,SC;Singapore,SG;" +
    				"Sri Lanka,LK;Taiwan,TW;Tajikistan,TJ;Thailand,TH;Turkmenistan,TM;Uzbekistan,UZ;Vitnam,VN;" +
    				"American Samoa,AS;Australia,AU;Federated States of Micronesia,FM;Fiji,FJ;French Polynesia,PF;Guam,GU;" +
    				"Kiribati,KI;Marshal Islands,MH;Nauru,NR;New Caledonia,NC;New Zealand,NZ;Norther Mariana Islands,MP;" +
    				"Palau,PW;Papua New Guinea,PG;Pitcairn,PN;Solomon Islands,SB;Tonga,TO;Tuvalu,TV;Vanuata,VU;" +
    				"Anguilla,AI;Antiqua and Barbuda,AG;Aruba,AW;Bahamas,BS;Barbados,BB;Bermuda,BM;" +
    				"British Virgin Islands,VI;Cayman Islands,KY;Dominica,DM;Dominican Republic,DO;Grenada,GD;Guadeloupe,GP;" +
    				"Haiti,HT;Jamaica,JM;Martinique,MQ;Netherlands Antilles,AN;Puerto Rico,PR;St. Kitts and Nevis,KN;" +
    				"St. Lucia,LC;St. Vincent and the Granadines,VC;Trinidad and Tobago,TT;Turks and Caicos Islands,TC;" +
    				"Belize,BZ;Costa Rica,CR;El Salvador,SV;Guatemala,GT;Honduras,HN;Nicaragua,NI;Panama,PA;" +
    				"Albania,AL;Andorra,AD;Armenia,AM;Austria,AT;Azerbaijan,AZ;Belarus,BY;Belgium,BE;Bulgaria,BG;Croatia,HR;" +
    				"Cyprus,CY;Czech Replubic,CZ;Denmark,DK;Estonia,EE;Faroe Islands,FO;Finland,FI;France,FR;Georgia,GE;Germany,DE;" +
    				"Gibraltar,GI;Greece,GR;Greenland,GL;Hungary,HU;Iceland,IS;Ireland,IE;Italy,IT;Latvia,LV;Liechtenstein,LI;" +
    				"Lithuania,LT;Luxembourg,LU;Malta,MT;Metropolitan France,FX;Moldova,MD;Netherlands,NL;Norway,NO;Poland,PL;Portugal,PT;" +
    				"Romania,RO;Slovakia,SK;Slovenia,SI;Spain,ES;Svalbard and Jan Mayen Islands,SJ;Sweden,SE;Switzerland,CH;" +
    				"The former Yugoslav Replublic of Macedonia,MK;Turkey,TR;Ukreaine,UA;United Kingdon,GB;Vatican City,VA;Yugoslavia,YU;" +
    				"Israel,IL;Jordan,JO;Kuwait,KW;Lebanon,LB;Oman,OM;Qatar,QA;Saudi Arabia,SA;Syria,SY;United Arab Emirates,AE;Yemen,YE;" +
    				"Canada,CA;Mexico,MX;United States,US;Argentina,AR;Bolivia,BO;Brazil,BR;Chile,CL;Columbia,CO;Equador,EC;Falkland Islands,FK;" +
    				"French Guiana,GF;Guyana,GY;Paraguay,PY;Peru,PE;Suriname,SR;Uraguay,UY;Venezuela,VE;Bahrain,BH;Bouvet Islands,BV;British Indian Ocean Territory,IO";
    	// Declare Separator Between Records
    	char[] SeparatorSemicolon = ";".ToCharArray();
    	// Declare Separator Between Fields
    	char[] SeparatorComma = ",".ToCharArray();
    	// Split into an array of records
    	string[] CountryCodesArrayBeforeSplit = CountryCodes.Split(SeparatorSemicolon);
    	// Get the number of Country Codes
    	int NumCountryCodes = CountryCodesArrayBeforeSplit.Length;
    	// Declare an array for temporarily storing the values before they are put into the DataSet
    	string[] CountryCodesNamesArray;
    	// Declare the DataSet
    	System.Data.DataSet Set = new System.Data.DataSet();
    	// Add a Table to the dataset
    	// Add Columns to the DataSet
    	// Declare index for looping through the records
    	int i = 0;
    	// Loop while there are more countries left.
    	while (i < NumCountryCodes)
    		// Initialize CountryCodesNamesArray
    		CountryCodesNamesArray = new string[2];
    		// Assign the values to the array
    		CountryCodesNamesArray[0] = CountryCodesArrayBeforeSplit[i].Split(SeparatorComma)[0];
    		CountryCodesNamesArray[1] = CountryCodesArrayBeforeSplit[i].Split(SeparatorComma)[1];
    		// Add a row with the array as the values
    		// Increment i
    	// Create a view for sorting the records
    	System.Data.DataView View = Set.DefaultViewManager.CreateDataView(Set.Tables[0]);
    	// Sort the Country Codes in Descending order
    	View.Sort = "CountryNames DESC";
           return View;
    When I run this code I get the following errors during the databind:

    Specified argument was out of the range of valid values. Parameter name: value
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: value

    The stack trace is as follows:

    [ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
    Parameter name: value]
    System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e)
    OnLineStore.CheckOut.Page_Load(Object sender, EventArgs e) in c:\svn\onlinestore\checkout.aspx.cs:77
    System.Web.UI.Control.OnLoad(EventArgs e) +67
    System.Web.UI.Control.LoadRecursive() +35
    System.Web.UI.Page.ProcessRequestMain() +750

    Does anyone have any ideas what might be causing this? I have tried making it bind the dataset instead of the dataview with the same results. The strange part is it worked fine the first time through then stopped working and I have rebooted my pc and tried everything I know to try with the same results.

    Thanks for any help you can give.
    Last edited by tjc1027; 05-26-2005 at 03:44 PM.

  2. #2
    Hi tjc1027

    I may be able to help you if you show me what exactly is on the 77-th line in the file c:\svn\onlinestore\checkout.aspx.cs.
    Content Management Systems and websites to achieve your business goals

  3. #3
    The last line in the first code sample is the one it shows on line 77.



  4. #4
    Does anyone have any ideas on this? I have bound datasets to droplists with no problems many times before but this one seems to cause problems. I cannot see what the problem is.

  5. #5
    Join Date
    Oct 2004
    my comment is that the code is unnecessarily complex,

    it could be a lot simpler, I don't have time now but later I might rewrite it if you want.

    just one question, why don't you storage the country codes on a database table or an xml file? it would make life a lot easier for you (expecially if you need to add a country code, or modify one, you won't need to recompile your application).

  6. #6

    I've tried running your code, and it worked without a hitch both with a DropDownList and a ListBox controls.

    The problem is somewhere else.
    Last edited by itspot; 06-02-2005 at 02:48 PM.
    Content Management Systems and websites to achieve your business goals

  7. #7
    Join Date
    May 2005
    Sydney, Australia
    Excuse my stupidity, for that is what it is, but shouldn't

    "while (i < NumCountryCodes)"


    'while (i < NumCountryCodes -1)'?

  8. #8
    T{H}R the Array is zero based and NumCountryCodes is the length of the array so it is not zero based so while i < NumCountryCodes it will be within the arrays bounds once it is equal to NumCountryCodes it will be outside the arrays bounds. Adding the minus 1 is unnecesary because i starts at zero and the count for NumCountryCodes starts at 1. Also the reason I didn't bind from XML is because I have never done so before and didn't know how complicated it was and the reason I didn't add it to a database is because I am working with an existing database and my boss will not allow any modifications to the existing database. Plus that would mean retyping all of them which I would rather not do if I can help it. I will have to try again maybe the error is somewhere else. I just can't figure out why it gives the error on the DataBind() if it is somewhere else.

  9. #9
    Well, I still don't know why it won't databind but I got it woking by doing this.

    	// Build Country Codes
    	System.Data.DataView CountryCodes = AccountInfoSessionClass.BuildCountryCodes();
    	System.Web.UI.WebControls.ListItem CurItem;
    	foreach (System.Data.DataRow CurRow in CountryCodes.Table.Rows)
    		CurItem = new ListItem(CurRow["CountryNames"].ToString(), CurRow["CountryCodes"].ToString());
    I also ended up having to sort the array before I populate the dataset because for some reason the sort expression in the DataView didn't sort correctly.

    Thanks for all the help,

Posting Permissions

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