Pankouri Pankouri - 24 days ago 8
C# Question

C# Coding issue with Drop Down List

First, let me tell you what error I am getting.


'DDLTesttoAppear' has a
SelectedIndex
which is invalid because it
does not exist in the list of items. Parameter name: value


I have many enums in my project, Here two enums are related two this question.
This Two enums are

public enum Gender
{
NA = 0, Male = 1, Female = 2
}


and

public enum NumberOfAdmissionTest
{
NA = 0, First = 1, Second = 2, Third = 3, Fourth =4
}


In UI page I have two DDls they are like

DDLGender.DataSource = Enum.GetNames(typeof(Gender));
DDLGender.DataBind();

DDLTestApearnce.DataSource = Enum.GetNames(typeof(NumberOfAdmissionTest));
DDLTestApearnce.DataBind();


This fields can be inserted as null into the database. Therefore, while returning the record I am using a null hander function
where the line of code to execute is that

candidateEntity.CandidateGender = nullHandler.GetInt32(CANDIDATE_GENDER);
candidateEntity.TestToAppear = nullHandler.GetInt32(TEST_TO_APPEAR);

public int GetInt32(String sFieldName)
{
return (_reader[sFieldName] == DBNull.Value) ? 0 : _reader.GetInt32(_reader.GetOrdinal(sFieldName));
}


After retrieving the record, I am binding this with two ddls like

DDLGender.SelectedIndex = candidateEntity.CandidateGender;
DDLTesttoAppear.SelectedIndex = candidateEntity.TestToAppear;


Now, the interesting or confusing, whatever you say, part of this problem is that for gender, it's not generating any error, but for test appearance it's generating the error.

cjk cjk
Answer Source

You are binding DDLTestApearnce in your sample, but are getting the error (and setting the selected value) on DDLTesttoAppear.