Ishan Ishan - 2 months ago 10
ASP.NET (C#) Question

DataTable declaration inside SelectedIndexChanged method not working

I am trying to define a

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);

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

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...