Hi
I have below code but it return 0 value in Code behind.
Int32 id = Convert.ToInt32(ddlStudent.SelectedItem.Value);
Select Mentor
<%----%>
Select Student
<%-- --%>
protected void ddlMentor_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
GetStudent();
}
catch (Exception ex)
{
Utility.SaveErrorLog(ex.Message, System.IO.Path.GetFileName(Request.Path), System.Reflection.MethodBase.GetCurrentMethod().Name, Convert.ToInt32(hdfLoginCode.Value));
ShowMessage("Oops...", ex.Message, "error");
}
}
private void GetStudent()
{
try
{
LMSDBDataContext context = new LMSDBDataContext();
var Result = (from t2 in context.View_SessionDeliveryCalendarDetails_Students
where t2.MentorID == Convert.ToInt32(ddlMentor.SelectedItem.Value)
select new { t2.StudentID,Name=t2.Name + " - " +t2.MobileNo}).Distinct().ToList();
if (Result != null)
{
ddlStudent.DataTextField = "Name";
ddlStudent.DataValueField = "StudentID";
ddlStudent.DataSource = Result;
ddlStudent.DataBind();
ddlStudent.Items.Insert(0, new ListItem("Select Student", ""));
}
}
catch (Exception ex)
{
Utility.SaveErrorLog(ex.Message, System.IO.Path.GetFileName(Request.Path), System.Reflection.MethodBase.GetCurrentMethod().Name, Convert.ToInt32(hdfLoginCode.Value));
ShowMessage("Oops...", ex.Message, "error");
}
}
Thanks
Jignesh KumarPosted Apr 6, 2023, 7:52 AM
please try this one,
or,
You can do one more thing in page load
Ramco RamcoPosted Apr 6, 2023, 7:00 AM
Hi
What should i change in my code
Thanks
Rajkiran SwainPosted Apr 6, 2023, 6:55 AM
It looks like you are setting the
EnableViewStateproperty of theddlStudentDropDownList tofalse. This could be the reason why you are getting 0 value in code behind.When
EnableViewStateis set tob, the DropDownList's items and their selected values will not be persisted across postbacks. This means that on postback, the DropDownList's selected value will always be the default value (i.e. "0" in your case).Ramco RamcoPosted Apr 6, 2023, 6:52 AM
Hi
Values are getting populated but on code behind it is returning 0
Thanks
Rajkiran SwainPosted Apr 6, 2023, 6:50 AM
Based on the code you provided, it is difficult to determine exactly what is causing the issue. However, there are a few things you can check:
Make sure that the ddlMentor_SelectedIndexChanged event is actually being triggered. You can add a breakpoint in the event handler and see if it is being hit.
Make sure that the ddlMentor DropDownList is populated with values and that the selected value is not empty or null.
Check the data source of the GetStudent() method to ensure that there are actually results returned by the query.
Check if there are any errors being thrown in the GetStudent() method that might be causing the DropDownList to not be populated with values.
You can also try debugging the code step by step to see where the issue is occurring.