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
The error message…
… 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…
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.
And there’s the answer – the event handler is called
DropDownList1_SelectedIndexChanged
, notddlFirst_SelectedIndexChanged
. Renaming the method toddlFirst_SelectedIndexChanged
should make the compiler error go away.As an aside, concatenating user input into a SQL statement like this…
… 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.
If you use parameter’s, you find this code quite a bit easier.
Say this markup:
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:
And the result is now this:
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).