Objective: To have drop down list select a specific value based on some data on a data table
Issue: Some of the data on the datatable does not have a value and this results in an out of range exception the code is as follows
dtMyDataTable = objMyObject.MyStoredProcedure();
if(dtMyDataTable.Rows.Count > 0)
{
ddlMyDropDownList = dtMyDataTable.Rows[0]["OptionalField"].ToString();
}
Thank you in advance for any comments, suggestions or recommendations.
For anyone interested this is how I resolved my issue:
if (dtMyTable.Rows[0]["OptionalField"] != null)
{
if (dtMyTable.Rows[0]["OptionalField"].ToString() == "")
{
ddlMyDropDownList .SelectedIndex = 0;
}
else
{
ddlMyDropDownList .SelectedValue = dtMyTable.Rows[0]["OptionalField"].ToString();
}
}