Ramco Ramco

Ramco Ramco

  • 442
  • 3.4k
  • 528.5k

Value returning 0

Apr 6 2023 6:49 AM

Hi

I have below code but it return 0 value in Code behind.

Int32 id = Convert.ToInt32(ddlStudent.SelectedItem.Value);

<div class="col-lg-6">
    <div class="form-group">
        <asp:DropDownList ID="ddlMentor" class="form-control" required="true" parsley-trigger="change" AutoPostBack="true" OnSelectedIndexChanged="ddlMentor_SelectedIndexChanged" runat="server">
            <asp:ListItem Value="">Select Mentor</asp:ListItem>
        </asp:DropDownList>
    </div>
</div>

<div class="form-horizontal">
                                    <div class="modal-body">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>
        <%--<div class="col-lg-6">--%>
            <div class="form-group">
                <asp:DropDownList ID="ddlStudent" class="form-control" required="true" EnableViewState="false" parsley-trigger="change" runat="server">
                    <asp:ListItem Value="0">Select Student</asp:ListItem>
                </asp:DropDownList>
            </div>
        <%--</div>  --%>                                
   </ContentTemplate>
   <Triggers>
   <asp:AsyncPostBackTrigger ControlID="ddlMentor" EventName="SelectedIndexChanged" />
   </Triggers>
</asp:UpdatePanel>
</div>
</div>
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


Answers (5)