I have been learning ASP.Net since sometime. Recently I was trying to make 2 Drop Down List controls. One was to be for the Country List and the other for corresponding the State List. I used the following code:
Front-end ASP Code
<tr>
<td>
<asp:Label ID="regcon" runat="server" Text="Country"></asp:Label>
</td>
<td>
<asp:DropDownList ID="Cntrylst" runat="server" AutoPostBack="True">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="regvalcnt" runat="server"
ControlToValidate="Cntrylst" Display="Dynamic" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="State"></asp:Label>
</td>
<td>
<asp:DropDownList ID="stlst" runat="server"
onload="Cntrylst_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="Cntrylst" Display="Dynamic" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
back-end C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Default4 : System.Web.UI.Page
{
SqlConnection obj_con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Users.mdf;Integrated Security=True;User Instance=True");
SqlDataReader obj_dr;
SqlCommand obj_com;
protected void Page_Load(object sender, EventArgs e)
{
statelist();
countrylist();
}
protected void countrylist()
{
string cntstr = "select * from CountryList";
obj_com = new SqlCommand(cntstr, obj_con);
obj_con.Open();
obj_dr = obj_com.ExecuteReader();
Cntrylst.DataTextField = "Countries";
Cntrylst.DataValueField = "Countries";
Cntrylst.DataSource = obj_dr;
Cntrylst.DataBind();
obj_con.Close();
}
protected void Cntrylst_SelectedIndexChanged(object sender, EventArgs e)
{
string statestr = "select '" + Cntrylst.SelectedValue.ToString() + "' from StateList";
// string statestr = "select Afghanistan from StateList"; //if i use this instead it works!
obj_com = new SqlCommand(statestr, obj_con);
obj_con.Open();
obj_dr = obj_com.ExecuteReader();
stlst.DataTextField = Cntrylst.SelectedValue.ToString();
//stlst.DataTextField = "Afghanistan"; //if i use this instead it works!
stlst.DataValueField = Cntrylst.SelectedValue.ToString();
//stlst.DataValueField = "Afghanistan"; //if i use this instead it works!
stlst.DataSource = obj_dr;
stlst.DataBind();
obj_con.Close();
}
}
I have made 2 tables inside my database named Users.mdf. One named "CountryList" and other named "StateList". In CountryList there is just one Column named "Countries" and in the StateList I have made one column for each Country, like one column named Afghanistan for it's states; one for USA for it's states; and so on. I don't wish to change the structure of my tables. With the current tables that I have made, can anyone help me solve the problem? When I execute the above code, an exception is thrown :
"DataBinding: 'System.Data.Common.DataRecordInternal' does not contain a property with the name 'Afghanistan'." I know I haven't handeled the events properly and they are being thrown at wrong time, but the problem is this exception if you can help me deal with this without having me to change my tables!
This post has been edited by toxifier: 11 July 2012 - 09:10 AM

New Topic/Question
Reply




MultiQuote





|