Ishan Ishan - 4 months ago 19
ASP.NET (C#) Question

DataTable declaration inside SelectedIndexChanged method not working

I am trying to define a

DataTable
inside a dropdownchange method as below :

protected void selectSegment_SelectedIndexChanged(object sender, EventArgs e)
{
String query = "<View><Query><Where><Eq><FieldRef Name='Segment' /><Value Type='Lookup'>" + selectSegment.SelectedItem.Text + "</Value></Eq></Where></Query></View>";
DataTable dt = SPCommon.GetFilteredItems("Approvers Info", "Business_x0020_Unit", "ID", query);
}






When i debug the above code, i get the error as
The name 'dt' does
not exist in the current context



enter image description here
The same thing if i do on PageLoad, works fine :

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable Results = SPCommon.GetAllItems("Segments", "Title", "ID");
System.Web.UI.WebControls.ListItem defalutListItem = new System.Web.UI.WebControls.ListItem();
defalutListItem.Text = "Select";
defalutListItem.Value = "-99";
Common.populateDropdown(selectSegment, Results, "Title", "ID", defalutListItem);
Results.Clear();
}
}





Please help understand why i am unable to declare a new DataTable variable inside the
SelectedIndexChanged
method?

Answer Source

Try to do something with you variable dt, because .net may have "optimized" it (remove it completly) because it's not used anywhere in your code...