Hi, this is my first post on this forum and I'm new to ASP.NET devolpment. So, my question may be pretty daft.
I'm trying to fill a DropDownList which I have on a FormView in the EditTemplate view.
The first time I fill it, it works ok. If I move to the next FormView page it fails to fill the DropDownList even though my FillDropDownList method is called and executes succesfully (or appears to).
Here is the code:
// Code Start ---------------------------------------------------------------------
// In the FormView Load event I attempt to fill the DropDownList
protected void fvAccFilter1_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
//{
FillDropDownList(GetDropDownList(fvAccFilter1, "ddlAccCat"), "Incident", "IncidentID", "NAME", "NAME", true);
//}
}
// This method retrieves the DropDownList I want to fill from a FormView
protected DropDownList GetDropDownList(FormView formView, String name)
{
DropDownList ddList = null;
try
{
ddList = (DropDownList)formView.FindControl(name);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
return ddList;
}
// This method fills the DropDownList with data from a database and optionally adds
// a blank entry before the data from the database. (The main reason for doing this)
protected void FillDropDownList(DropDownList ddList, String tableName, String dataField,
String listField, String orderByField, bool addBlank)
{
if (ddList != null)
{
SqlConnection connection = null;
try
{
String connStr = dsAccFilter1.ConnectionString;
connection = new SqlConnection(connStr);
DataTable dtable = new DataTable();
String SQL = "SELECT " + dataField + ", " + listField +
" FROM " + tableName +
" ORDER BY " + orderByField;
SqlDataAdapter adapter = new SqlDataAdapter(SQL, connection);
connection.Open();
adapter.Fill(dtable);
ddList.ClearSelection();
ddList.Items.Clear();
if (addBlank)
{
ListItem item = new ListItem("", "0");
ddList.Items.Add(item);
}
int count = dtable.Rows.Count;
for (int i = 0; i < count; i++)
{
ListItem item = new ListItem(dtable.Rows[i][1].ToString(), dtable.Rows[i][0].ToString());
ddList.Items.Add(item);
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
connection.Close();
}
}
else
Response.Write("ERROR: ddList == null");
}
// Code End -----------------------------------------------------------------------
Any help would be very welcome,
Thanks,
Mark