skip to Main Content

I want to create 3 Dropdown List for (Country, City and State) Cascading on Selection for The First Dropdown list Changing The Second and Selection for the second Changing The Third with SQL Server (ADO.Net) and ASP.Net C#!
and this error showing me
Can any one solve this error Please ?
Here the asp.net design code The Fist one..enter image description here

<asp:DropDownList ID="ddlFirst" runat="server" AutoPostBack="True" AppendDataBoundItems="True"
    onselectedindexchanged="ddlFirst_SelectedIndexChanged">  
    <asp:ListItem Value="0">--Select Location--</asp:ListItem>
    </asp:DropDownList>

The Second one:

<asp:DropDownList ID="ddlSecond" runat="server" AppendDataBoundItems="true" DataTextField="City"   
    DataValueField="City" AutoPostBack="True"   
    onselectedindexchanged="ddlSecond_SelectedIndexChanged">  
    <asp:ListItem Value="0">-- Select City--</asp:ListItem>  
    </asp:DropDownList>

And The Third..

<asp:DropDownList ID="ddlThird" runat="server" AppendDataBoundItems="true" DataTextField="State"   
    DataValueField="State">  
    <asp:ListItem Value="0">-- Select Area--</asp:ListItem>  
    </asp:DropDownList>

, the code of page load..enter image description here

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)

        {
            try
            {
                txtid.Text = (pr.Project.OrderByDescending(b => b.ProID).FirstOrDefault().ProID     
    +1).ToString();
            }
            catch
            {

                txtid.Text = "1";
            }
            btnadd.Visible = true;
            btndelete.Visible = false;
            btnupdate.Visible = false;
            GridView1.DataSource = pr.Project.ToList();
            GridView1.DataBind();
        }

        if (!Page.IsPostBack)
        {
            SqlConnection con = new SqlConnection(@"Data     
    Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|GROUP.mdf;Integrated Security=True;User     
    Instance=True");
            SqlCommand cmd = new SqlCommand("select * from Project", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            ddlFirst.DataSource = dt;
            ddlFirst.DataBind();

        }

    }

, code of dropdown lists with c#..enter image description here

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlSecond.Items.Clear();
        ddlSecond.Items.Add("Select State");

        SqlConnection con = new SqlConnection(@"Data     
    Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|GROUP.mdf;Integrated Security=True;User     
    Instance=True");
        SqlCommand cmd = new SqlCommand("select [City] from Project where Location=" +     
    ddlFirst.SelectedItem.Value, con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        ddlSecond.DataSource = dt;
        ddlSecond.DataBind();
    }

    protected void ddlSecond_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlSecond.Items.Clear();
        ddlSecond.Items.Add("Select State");

        SqlConnection con = new SqlConnection(@"Data 
    Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|GROUP.mdf;Integrated Security=True;User 
    Instance=True");
        SqlCommand cmd = new SqlCommand("select [State] from Project where City=" + 
    ddlSecond.SelectedItem.Value, con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);

        ddlSecond.DataSource = dt;
        ddlSecond.DataBind();
    }

and < The Error >..enter image description here

Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request.

Please review the following specific error details and modify your source code appropriately.

Compiler Error Message:

CS1061: 'ASP.webusercontrol_project_wuc_ascx' does not contain a definition for 'ddlFirst_SelectedIndexChanged' and no extension method 'ddlFirst_SelectedIndexChanged' accepting a first argument of type 'ASP.webusercontrol_project_wuc_ascx' could be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 94: 
Line 95: 
Line 96:             <asp:DropDownList ID="ddlFirst" runat="server" AutoPostBack="True" AppendDataBoundItems="True"
Line 97:                     onselectedindexchanged="ddlFirst_SelectedIndexChanged">  
Line 98:                     <asp:ListItem Value="0">--Select Location--</asp:ListItem>


Source File: g:ProjectsAdmin-WebApp-Final-VersionWebUserControlProject-WUC.ascx    Line: 96

I tried to create it with javascript only and it didn’t work, and I tried to create it without a database and it didn’t work

2

Answers


  1. The error message…

    CS1061: ‘ASP.webusercontrol_project_wuc_ascx’ does not contain a definition for ‘ddlFirst_SelectedIndexChanged’ and no extension method ‘ddlFirst_SelectedIndexChanged’ accepting a first argument of type ‘ASP.webusercontrol_project_wuc_ascx’ could be found (are you missing a using directive or an assembly reference?)

    … is a slightly long-winded way of telling you that you’re trying to call a method called ddlFirst_SelectedIndexChanged but the compiler couldn’t find a method which meets that description.

    From the name of the method that the compiler is looking for, I guess it’s probably intended to be an event handler for the user changing the selection in the first drop-down list, so let’s check that…

    <asp:DropDownList ID="ddlFirst" runat="server" AutoPostBack="True" AppendDataBoundItems="True"
        onselectedindexchanged="ddlFirst_SelectedIndexChanged">  
        <asp:ListItem Value="0">--Select Location--</asp:ListItem>
    </asp:DropDownList>
    

    Yep, this is what is causing the compiler to look for a method with that name, so let’s see what the event handler method is actually called.

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    

    And there’s the answer – the event handler is called DropDownList1_SelectedIndexChanged, not ddlFirst_SelectedIndexChanged. Renaming the method to ddlFirst_SelectedIndexChanged should make the compiler error go away.

    As an aside, concatenating user input into a SQL statement like this…

    SqlCommand cmd = new SqlCommand("select [City] from Project where Location=" +     
        ddlFirst.SelectedItem.Value, con);
    

    … isn’t a great idea, because it’s vulnerable to SQL injection attacks, which a malicious user could use to do serious damage to your database. Using stored procedures, or an object relational mapper such as Entity Framework, is a much safer way of implementing your data access.

    Login or Signup to reply.
  2. If you use parameter’s, you find this code quite a bit easier.

    Say this markup:

    <div style="float:left">
    <h4>Region</h4>
        <asp:DropDownList ID="cboRegion" runat="server"
            DataTextField="Region"
            AutoPostBack="true"
            OnSelectedIndexChanged="cboRegion_SelectedIndexChanged" >
        </asp:DropDownList>
    </div>
    
    <div style="float:left;margin-left:25px">
    <h4>Country</h4>
        <asp:DropDownList ID="cboCountry" runat="server" Width="250px"
            DataTextField="Country" 
            AutoPostBack="true"
            OnSelectedIndexChanged="cboCountry_SelectedIndexChanged" >
        </asp:DropDownList>
    </div>
    
    <div style="float:left;margin-left:25px">
    <h4>State</h4>
        <asp:DropDownList ID="cboState" runat="server" Width="200px"
            DataTextField="state" 
            AutoPostBack="true"
            OnSelectedIndexChanged="cboState_SelectedIndexChanged" >
        </asp:DropDownList>
    </div>
    
    <div style="float:left;margin-left:25px">
    <h4>City</h4>
        <asp:DropDownList ID="cboCity" runat="server" Width="220"
            DataTextField="city"
            dataValueField="ID"
            AutoPostBack="true"
            OnSelectedIndexChanged="cboCity_SelectedIndexChanged" >
        </asp:DropDownList>
    </div>
    

    Note how I "avoided" using the item list in the drop down boxes. The reason is if you re-fill, then you will have difficulty in clearing out the drop boxes.

    And now our code is this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadData();
    }
    
    
    void LoadData()
    {
        // load up first cbo box, region
    
        SqlCommand cmdSQL =
            new SqlCommand("SELECT region from vRegion ORDER BY region");
    
        cboRegion.DataSource = MyRstP(cmdSQL);
        cboRegion.DataBind();
        cboRegion.Items.Insert(0, new ListItem("Select Region", ""));
    }
    
    
    protected void cboRegion_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cboRegion.SelectedIndex > 0)
        {
            string strSQL =
                @"SELECT Country FROM vCountryRegion
                WHERE region = @region
                ORDER BY Country";
    
            SqlCommand cmdSQL = new SqlCommand(strSQL);
            cmdSQL.Parameters.Add("@region", SqlDbType.NVarChar).Value = cboRegion.Text;
            cboCountry.DataSource = MyRstP(cmdSQL);
            cboCountry.DataBind();
            cboCountry.Items.Insert(0, new ListItem("Select Country", ""));
    
        }
    }
    
    protected void cboCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cboCountry.SelectedIndex > 0)
        {
            string strSQL =
                @"SELECT state from vCountryStates
                WHERE Country = @Country
                ORDER BY state";
    
            SqlCommand cmdSQL = new SqlCommand(strSQL);
            cmdSQL.Parameters.Add("@Country", SqlDbType.NVarChar).Value = cboCountry.Text;
            cboState.DataSource = MyRstP(cmdSQL);
            cboState.DataBind();
            cboState.Items.Insert(0, new ListItem("Select State", ""));
    
        }
    
    }
    
    protected void cboState_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cboState.SelectedIndex > 0)
        {
            string strSQL =
                @"SELECT id, city from vCities
                WHERE Country_name = @Country
                AND state_name = @State
                ORDER BY city";
    
            SqlCommand cmdSQL = new SqlCommand(strSQL);
            cmdSQL.Parameters.Add("@Country", SqlDbType.NVarChar).Value = cboCountry.Text;
            cmdSQL.Parameters.Add("@State", SqlDbType.NVarChar).Value = cboState.Text;
    
            cboCity.DataSource = MyRstP(cmdSQL);
            cboCity.DataBind();
            cboCity.Items.Insert(0, new ListItem("Select City", ""));
        }
    }
    

    And the result is now this:

    enter image description here

    And in the above, I also had this little routine (no need to type over and over the code to build a connection and load the data).

    DataTable MyRstP(SqlCommand cmdSQL)
    {
        DataTable rstData = new DataTable();
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.Countries))
        {
            using (cmdSQL)
            {
                cmdSQL.Connection = conn;
                conn.Open();
                rstData.Load(cmdSQL.ExecuteReader());
            }
        }
        return rstData;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search