1

2つのドロップダウンリストを使用しています。国の場合は1番目、州の場合は2番目です。1フィートのドロップダウンリストからインドを選択すると、2番目のドロップダウンリストはデータベースからインドのすべての州を自動的にバインドします。

私は国に使用し、これらの国に関する州を拘束するために、最初のcountry_tblドロップダウンリストとindia_tbl、を拘束しました。us_tblsri_tbl

私を助けてください。私は何をすべきか?

私のコードは次のとおりです。

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

protected void method1()
{
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
    SqlConnection con = new SqlConnection(s1);
    string s2 = "select * from country";
    SqlCommand cmd = new SqlCommand(s2, con);
    con.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    DropDownList1.DataTextField = "name";
    DropDownList1.DataValueField = "name";
    DropDownList1.DataSource = dr;
    DropDownList1.DataBind();
    con.Close();
    dr.Close();
}

protected void methodInd()
{
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
    SqlConnection con = new SqlConnection(s1);
    string s2 = "select * from india";
    SqlCommand cmd = new SqlCommand(s2, con);
    con.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    DropDownList2.DataTextField = "name";
    DropDownList2.DataValueField = "name";
    DropDownList2.DataSource = dr;
    DropDownList2.DataBind();
    con.Close();
    dr.Close();
}

protected void methodpak()
{
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
    SqlConnection con = new SqlConnection(s1);
    string s2 = "select * from pakistan";
    SqlCommand cmd = new SqlCommand(s2, con);
    con.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    DropDownList2.DataTextField = "name";
    DropDownList2.DataValueField = "name";
    DropDownList2.DataSource = dr;
    DropDownList2.DataBind();
    con.Close();
    dr.Close();
}

protected void methodsri()
{
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
    SqlConnection con = new SqlConnection(s1);
    string s2 = "select * from srilanka";
    SqlCommand cmd = new SqlCommand(s2, con);
    con.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    DropDownList2.DataTextField = "name";
    DropDownList2.DataValueField = "name";
    DropDownList2.DataSource = dr;
    DropDownList2.DataBind();
    con.Close();
    dr.Close();
}

protected void submit_Click(object sender, EventArgs e)
{

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (DropDownList1.SelectedItem.Text=="india")
    {
        methodInd();
    }
    else if (DropDownList1.SelectedItem.Text=="pakistan")
    {
        methodpak();
    }
    else if (DropDownList1.SelectedItem.Text=="srilanka")
    {
        methodsri();
    }
}
4

2 に答える 2

1

Your approach is wrong, you should not have separate tables for states of each country so you can have one query and one method to bind to your state list

Change your schema to

CREATE TABLE Country 
(
  id int,  
  country_name,
  primary key(id)
)

Country_State 
(
  id  int, 
  state_name, 
  country_id,
  primary key(id)
)

The country_id is the Foreign key that links back to the country

country
------------------------
id  Name
------------------------
1   India
2   Pakistan


country_state
-----------------------------------
id  Name              country_id
------------------------------------
1   Delhi             1
2   Bangladesh        1
3   Some Indians      1
4   S1_Pakistan       2
5   S2_Pakistan       2

Your queries

Select Id, Name from Country

Select Id, Name From country_states Where country_id = @id

Call just one method to bind all states

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
     string country_id =  DropDownList1.SelectedValue;
     BindStatesByCountry(country_id);
}

protected void methodsri(string countryId) //change this to BindStatesByCountry
{
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
    SqlConnection con = new SqlConnection(s1);
    con.Open();

    string s2 = "select * from country_states where country_id=@countryId";
    SqlCommand cmd = new SqlCommand(s2, con);
    cmd.Parameters.AddWithValue("@countryId", countryId);
    SqlDataReader dr = cmd.ExecuteReader();
    DropDownList2.DataTextField = "name";
    DropDownList2.DataValueField = "name";
    DropDownList2.DataSource = dr;
    DropDownList2.DataBind();
    con.Close();
    dr.Close();
}

Hope this helps

于 2012-07-21T13:24:47.420 に答える
0

マスター詳細パターンを実装したいので、次の URL を参照してください: http://msdn.microsoft.com/en-us/library/aa581789.aspx SQL インジェクションの代わりにストアド プロシージャを優先してください

于 2012-07-21T16:40:00.033 に答える