Hi
I have below code but it return 0 value in Code behind.
Int32 id = Convert.ToInt32(ddlStudent.SelectedItem.Value);
Code
Select Mentor
--%>
Select StudentC#
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

Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 6, 2023, 2:50 AM EDT
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.
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 6, 2023, 2:52 AM EDT
Hi
Values are getting populated but on code behind it is returning 0
Thanks
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 6, 2023, 2:55 AM EDT
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 RamcoRank #462 · 5.4K reputationPosted Apr 6, 2023, 3:00 AM EDT
Hi
What should i change in my code
Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted Apr 6, 2023, 3:52 AM EDT
please try this one,
protected void ddlMentor_SelectedIndexChanged(object sender, EventArgs e) { try { int mentorId = Convert.ToInt32(ddlMentor.SelectedItem.Value) GetStudent(mentorId); } 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(int mentorId) { try { LMSDBDataContext context = new LMSDBDataContext(); var Result = (from t2 in context.View_SessionDeliveryCalendarDetails_Students where t2.MentorID == mentorId 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"); } }or,
You can do one more thing in page load
//Add below code in your page_load event if (!IsPostBack) { BinddlMentor(); // This is your Mentor dropdownlist bind method }