将数据从sql加载到下拉列表

时间:2016-06-09 07:53:49

标签: c# sql asp.net

我想从dropdownlist加载我的数据。数据形式是Mysql 最初来自dropdownlist。我想在下拉列表中再次加载它或只加载相同的数据。

这是我的代码:

 private void ReviewPosterDetails()
    {
        con.Open();
        cmd = new SqlCommand(@"SELECT quo_JobDesc,quo_PrintProcess
                                ,quo_File,quo_Finishing
                                ,quo_Quantity
                                ,quo_Size,quo_Media,quo_PrintColor
                                FROM JobQuotations1
                                WHERE TransactionID = @id
                                AND TransactionNum = @Num", con);

        cmd.Parameters.AddWithValue("@id", GridView1.SelectedRow.Cells[2].Text);
        cmd.Parameters.AddWithValue("@Num", GridView1.SelectedRow.Cells[4].Text);

        rdr = cmd.ExecuteReader();

        if (rdr.HasRows)
        {
            while (rdr.Read())
            {
                txtJobDesc.Text = rdr["quo_JobDesc"].ToString();
                ddlPrintProc.Text = rdr["quo_PrintProcess"].ToString();

                lblFileName.Text = rdr["quo_File"].ToString();
                txtFinishing.Text = rdr["quo_Finishing"].ToString();

                txtQty.Text = rdr["quo_Quantity"].ToString();

                //Posters
                if (rdr["quo_Size"].ToString() == "Others")
                {
                    txtOthers.Text = rdr["quo_Size"].ToString();
                }
                else
                {
                   ddlSize.SelectedItem.Text = rdr["quo_Size"].ToString();
                }
                ddlMedia.SelectedIndex = rdr["quo_Media"].ToString();
                ddlPrintProc.Text = rdr["quo_PrintColor"].ToString();
                ddlColor.SelectedIndex = 0;
            }
        }
        con.Close();
        lblFileStatus.Text = "Previous File";
    }

如何将数据从Sql加载到DropdownList?还是可以?因为SqlData来自DDL所以其中一个ListItem类似于SqlData

1 个答案:

答案 0 :(得分:0)

假装你有一个下拉列表电话" ddl"。首先使用数据集将所需数据导入数据集。代码如下:

DataSet ds = new DataSe();
ds = class.getdata();  // here will be normal sql query to select data and display
ddl.DataSource = ds;
ddl.DataValueField = "xxx"; // real value of your selection in dropdownlist
ddl.DataTextField = "yyy";  // table field you wan to display on dropdownlist
ddl.DataBind();