The above error occurs on a SelectedIndexChanged click event on a listbox.
in debug the value returned is "", however when you look at the webpage source code there are definitely values.
Here's my listbox:
<asp:ListBox ID="lstBxMyList" runat="server" CssClass="binorderlst1"
DataTextField="myName"
DataValueField="myID"
OnSelectedIndexChanged ="lstBxMyList_SelectedIndexChanged"
AutoPostBack="true" Rows="10">
</asp:ListBox>
Here's the event:
protected void lstBxMyList_SelectedIndexChanged(object sender, EventArgs e)
{
myID = Convert.ToInt32(lstBxSiteList.SelectedValue.ToString());
... rest of code
}
For completeness here's the data binding:
private void BindLstBxMyList(int myOtherID)
{
DataTable dt = new DataTable();
SqlConnection conn;
SqlCommand comm;
using (conn = new SqlConnection(aSHconns.aconn))
{
comm = new SqlCommand("myStoredProc", conn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add(new SqlParameter("@myOtherID", SqlDbType.Int));
comm.Parameters["@myOtherID"].Value = myOtherID;
SqlDataAdapter sqlDa = new SqlDataAdapter(comm);
try
{
conn.Open();
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
lstBxMyList.DataSource = dt;
lstBxMyList.DataTextField = "myName";
lstBxMyList.DataValueField = "myID";
lstBxMyList.DataBind();
}
}
finally
{
conn.Close();
}
}
}
If I revert back to a SqlDataSource the listbox returns values. Bt I need to repopulate the list, so I need the code behind databind (unless there is a better way of course)
So why is the listbox selected value returning an empty string? Any help would be gratefully received.