سلام
من دو تا جدول دارم که یکی استان و یکی شهر های ایران هستش و به وسیله DropDownList اول، استان فراخوانی میکنم و DropDownList دوم شهر مرتبط با استان فراخوانی میکنم ولی در هنگام ثبت نهایی زمانی که شهر های انتخاب شده داخل دیتابیس میریزم در فیلد استان به جای اسم مثلا اصفهان کد مربوط که 4 هست ثبت شده ولی در فیلد شهر همون نام شهر مثلا کاشان ثبت شده در صورتی که میخوام در هر جفتش نام شهر ثبت بشه.
حالا من سوالم اینه آیا راه حلی هست که بشه این درست کرد چون خیلی سعی کردم ولی نمیدونم مشکل کجاست و اگر نمیشه میشه موقعی که داریم توسط
گرید اطلاعات فراخوانی میکنم بیایم کدی بنویسیم که به جای 4 بیاد بنویسه اصفهان ؟؟؟؟
کد ها قرار میدم ممنون میشم مشکل پیدا کنید.
<asp:DropDownList ID="State" runat="server" AutoPostBack = "true"
OnSelectedIndexChanged="ddlContinents_SelectedInde xChanged">
<asp:ListItem Text = "--Select Country--" Value = ""></asp:ListItem>
</asp:DropDownList><asp:DropDownList ID="city" runat="server" >
<asp:ListItem Text = "--Select city--" Value = ""></asp:ListItem>
</asp:DropDownList>
if (!IsPostBack)
{
State.AppendDataBoundItems = true;
String strCoString = ConfigurationManager
.CoectionStrings["CoectionStr"].CoectionString;
String strQuery = "select ID, Name from State";
SqlCoection con = new SqlCoection(strCoString);
SqlCommand cmd = new SqlCommand();cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Coection = con;
try
{
con.Open();
State.DataSource = cmd.ExecuteReader();
State.DataTextField = "name";
State.DataValueField = "id";
State.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
protected void ddlContinents_SelectedIndexChanged(object sender, EventArgs e)
{
city.Items.Clear();
city.Items.Add(new ListItem("--Select city--", ""));city.AppendDataBoundItems = true;
String strCoString = ConfigurationManager
.CoectionStrings["CoectionStr"].CoectionString;
String strQuery = "select ID, Name, StateId from City " +
"where StateId=@StateId";
SqlCoection con = new SqlCoection(strCoString);
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("@StateId",
State.SelectedItem.Value);
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Coection = con;
try
{
con.Open();
city.DataSource = cmd.ExecuteReader();
city.DataTextField = "Name";
city.DataValueField = "name";
city.DataBind();
if (city.Items.Count > 1)
{
city.Enabled = true;
}
else
{
city.Enabled = false;}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
State.Text = "";
city.Text = "";
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into Tbl_CV(Name,Family,Birth_Date,State,City,Education al_situation,Last_educationa_certificate) values('" + Name.Text + "','" + Family.Text + "',N'" + birthday.Text + "',N'" + State.Text + "',N'" + city.Text + "','" + RadioButtonList1.Text + "','" + Madrak.Text + "')", con);
ممنون میشم راهنمایی کنید همراه با مثال
پیشاپیش متشکرم
برچسب:
نویسنده: محمد رضا جوادیان