sumit sankpal

sumit sankpal

  • 1.3k
  • 68
  • 2.1k

System.NullReferenceException

Mar 21 2024 6:23 AM

this is my code in front-end

<div clss="row">
    <div class="col-lg-9 ml-2">
        <asp:DropDownList ID="ddlLocations" runat="server" OnSelectedIndexChanged="ddlLocations_SelectedIndexChanged" AutoPostBack="true" class="form-control form-control-sm  mb-9">
            <asp:ListItem Value="0">Select Locations</asp:ListItem>
        </asp:DropDownList>
    </div>
</div>
<div class="row">
    <div class="col-lg-4 mb-2 ml-4 mt-2">
        <asp:Label ID="lblCourse" runat="server" Text="Course :"></asp:Label>
    </div>
</div>
<div clss="row">
    <div class="col-lg-9 ml-2">
        <asp:DropDownList ID="ddlCourse" runat="server" class="form-control form-control-sm  mb-9">
            <asp:ListItem Value="0">Select Course</asp:ListItem>
        </asp:DropDownList>
    </div>
</div>

this is my code in back-end

 protected void ddlLocations_SelectedIndexChanged(object sender, EventArgs e)
 {
     BindCourse();

 }

 public void BindCourse()
 {            
     
     string mode = null, sp = null;
     mode = "Location"; sp = "sp_Admission2023Onwords";
    
     DropDownList ddlLocations = (DropDownList)FindControl("ddlLocations");
      
     //if (ddlLocations != null && ddlLocations.SelectedItem != null)
     {
         string Location_ID = ddlLocations.SelectedItem.Value;

         DataSet ds2 = objDal.BindCourse(mode, sp, Location_ID);  //

         ddlCourse.Items.Clear();
         ddlCourse.DataSource = ds2.Tables[1];
         ddlCourse.DataTextField = "Description";
         ddlCourse.DataValueField = "Description";
         ddlCourse.DataBind();
         ddlCourse.Items.Insert(0, new ListItem("Select Course", "0"));

     }
     
 }

why i am getting this error. Any solution


Answers (1)