Hi,
I have two dropdown list.
One for State and other for City. When I select the State, It shows me the respective Cities in the other dropdown list. I also have one option to add the extra city. i.e. from When User selects OTHER option from City DDL, One textbox becomes visible and user can add the city on button click. All code is working apart the textbox value is not getting in the dropdown list. I want that textbox value to be added in DDLCity as well as the table. Whenever I add the value from the textbox and search that city in the dropdownlist(ddlCity). It gives me error like this:-
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'StateName'.
Please see the code, I am using iniside update panel for the Autopostback
<asp:UpdatePanel ID="updPanelDDLState" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<tr>
<td class="td">Location/State</td>
<td>
<asp:DropDownList CssClass="txtfld-popup" ID="ddlState" OnSelectedIndexChanged="ddlState_SelectedIndexChanged" runat="server" AutoPostBack="true"></asp:DropDownList>
<asp:RequiredFieldValidator CssClass="error_msg" ID="RequiredFieldValidator1" ControlToValidate="ddlState" runat="server" ErrorMessage="Please enter location" InitialValue="--Select--" SetFocusOnError="true"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="td">Location/City</td>
<td>
<asp:DropDownList CssClass="txtfld-popup" ID="ddlCity" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCity_SelectedIndexChanged"></asp:DropDownList>
<asp:RequiredFieldValidator CssClass="error_msg" ID="reqLocation" ControlToValidate="ddlCity" runat="server" ErrorMessage="Please enter location" InitialValue="--Select--" SetFocusOnError="true"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtOtherCity" runat="server" Visible="false" CssClass="txtfld-popup"></asp:TextBox>
<asp:Button ID="btnAddDropDown" runat="server" Width="63" Text="Add" CausesValidation="false" OnClick="btnAddDropDown_Click1"/>
</td>
</tr>
<tr>
</ContentTemplate>
.cs code
protected void BindContrydropdown()
{
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select Id,StateName From Career.Location", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlState.DataSource = ds;
ddlState.DataTextField = "StateName";
ddlState.DataValueField = "Id";
ddlState.DataBind();
ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
ddlState.Items.Insert(1, new ListItem("Other", "0"));
con.Close();
}
}
protected void BindCitydropdown()
{
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
//Query to fetch the city name for your dropdownlist
SqlCommand cmd = new SqlCommand("Select Id,CityName From Career.Location", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlCity.DataSource = ds;
//loaded your dropdownlist with CityNames
ddlCity.DataTextField = "StateName";
ddlCity.DataValueField = "Id";
ddlCity.DataBind();
con.Close();
}
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
string country = "India";
var cities = _helper.GetLocations(country, ddlState.SelectedValue);
cities.Insert(0, "--Select--");
cities.Insert(1, "Other");
ddlCity.DataSource = cities;
ddlCity.DataBind();
}
protected void btnAddDropDown_Click1(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Add_CityforLocation";
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = 0;
cmd.Parameters.Add("@CountryName", SqlDbType.VarChar).Value = "India";
cmd.Parameters.Add("@StateName", SqlDbType.VarChar).Value = ddlState.SelectedItem.ToString();
cmd.Parameters.Add("@CityName", SqlDbType.VarChar).Value = txtOtherCity.Text.Trim();
cmd.Connection = con;
try
{
cmd.ExecuteNonQuery();
BindCitydropdown();
// BindContrydropdown();
}
catch (Exception ex)
{
Response.Write(ex.Message);//You Can Haave Messagebox here
}
finally
{
con.Close();
}
}
}
Also see the SP for inserting the city in the table:-
ALTER PROCEDURE [dbo].[Add_CityforLocation]
@ID int,
@CountryName nvarchar(100),
@StateName nvarchar(100),
@CityName varchar(100)
AS
BEGIN
INSERT INTO Career.Location(CountryName, StateName, CityName) values (@CountryName,@StateName,@CityName)
END
Please let me know where I am mistaking.