下拉列表从MYSQL填充

时间:2013-09-14 13:31:59

标签: c# mysql

我使用这些语法填充两个dropdwon列表,第二个下拉列表必须填写后我选择第一个但第二个是embty,在mysql表中我有两个表来填充下拉列表,第一个表包含( gov_name)和(gove_id),第二个包含(area_name)和(gov_id)。

{
    DataSet ds_1;
    ListItem item_1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fill_gov();
        }
    }

    private void fill_gov()
    {
        //Session["conection"] = "Data Source=MEDICONSULT;Initial Catalog=test1;Integrated Security=True";
        Session["conection"] = "Data Source=MEDICONSULT;Initial Catalog=test1;Integrated Security=True";

        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
        connection.Open();
        SqlCommand command = new SqlCommand();
        connection = new SqlConnection((string)Session["conection"]);
        connection.Open();
        SqlDataAdapter da_1 = new SqlDataAdapter(command);
        da_1 = new SqlDataAdapter();
        command = new SqlCommand();
        command.Connection = connection;
        command.CommandText = "select gov_name from gov";
        da_1.SelectCommand = command;
        ds_1 = new DataSet();
        da_1.Fill(ds_1, "gov");
        Session["Hospital"] = DropDownList1.SelectedIndex;
        DropDownList1.Items.Clear();
        item_1 = new ListItem();
        item_1.Value = "0";
        item_1.Text = "select governement";
        DropDownList1.Items.Add(item_1);
        for (int i = 0; i <= ds_1.Tables[0].Rows.Count - 1; i++)
        {
            item_1 = new ListItem();
            item_1.Value = (ds_1.Tables[0].Rows[i]["gov_name"].ToString());
            item_1.Text = ds_1.Tables[0].Rows[i]["gov_name"].ToString();
            DropDownList1.Items.Add(item_1);
        }

    }

    private void fill_area()
    {
       // Session["conection"] = "Data Source=MEDICONSULT;Initial Catalog=test1;Integrated Security=True";
        Session["conection"] = "Data Source=MEDICONSULT;Initial Catalog=test1;Integrated Security=True";

        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
        connection.Open();
        SqlCommand command = new SqlCommand();
        connection = new SqlConnection((string)Session["conection"]);
        connection.Open();
        SqlDataAdapter da_1 = new SqlDataAdapter(command);
        da_1 = new SqlDataAdapter();
        command = new SqlCommand();
        command.Connection = connection;
        command.CommandText = "select area_name from area where area_name !='' and cast (gov_id as varchar) ='" + DropDownList1.SelectedValue + "'";
        da_1.SelectCommand = command;
        ds_1 = new DataSet();
        da_1.Fill(ds_1, "area");
        Session["Hospital"] = DropDownList2.SelectedIndex;
        DropDownList2.Items.Clear();
        item_1 = new ListItem();
        item_1.Value = "0";
        item_1.Text = "select area";
        DropDownList2.Items.Add(item_1);
        for (int i = 0; i <= ds_1.Tables[0].Rows.Count - 1; i++)
        {
            item_1 = new ListItem();
            item_1.Value = (ds_1.Tables[0].Rows[i]["area_name"].ToString());
            item_1.Text = ds_1.Tables[0].Rows[i]["area_name"].ToString();
            DropDownList2.Items.Add(item_1);
        }

    }

1 个答案:

答案 0 :(得分:0)

fill_gov()

您需要同时选择gov_idarea_name

 command.CommandText = "select gov_id, gov_name from gov";

gov_id

设置值
item_1.Value = (ds_1.Tables[0].Rows[i]["gov_id"].ToString());
item_1.Text = ds_1.Tables[0].Rows[i]["gov_name"].ToString();

并更改fill_area()选择sql,如下所示

command.CommandText = "select area_name from area where area_name !='' and gov_id =" + DropDownList1.SelectedValue ;

you better use parameterized SQL