0

I wanted to load my data to from dropdownlist.The data form Mysql originally came from dropdownlist.I wanted to load it again in dropdownlist or just load the same data.

here is my code:

 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";
    }

How could I load the data from Sql to DropdownList?or is it possible?Because the SqlData came from the DDL so one of the ListItem is similar to SqlData

rai nalasa
  • 849
  • 1
  • 12
  • 32
  • 3
    So, what's your question? – MusicLovingIndianGirl Jun 09 '16 at 07:55
  • @MusicLovingIndianGirl updated it.I apologize for the poor english. – rai nalasa Jun 09 '16 at 08:00
  • As far as i see, you load the data, and set it within the drop down box, but which part does not work? – Thomas Raffelsieper Jun 09 '16 at 08:02
  • @ThomasRaffelsieper I can't set it to the dropdownlist.The code does not work. – rai nalasa Jun 09 '16 at 08:04
  • 1
    Use DataTable binding (http://stackoverflow.com/questions/7227510/what-is-the-right-way-to-populate-a-dropdownlist-from-a-database) or SqlDataSource control (http://stackoverflow.com/questions/26513434/populate-a-dropdown-list-with-sql). Make sure you set DataSource, DataTextField, DataValueField on the dropdownlist and also adding DataBind() method to bind the data with proper MySQL connection string. – Tetsuya Yamamoto Jun 09 '16 at 08:08
  • @TetsuyaYamamoto I tried this one `ddlMedia.DataTextField = rdr["quo_Media"].ToString();` This is the one I need just to load the selected data on DDL textfield.It's not working atm. – rai nalasa Jun 09 '16 at 08:17
  • @rainalasa Your `rdr["quo_Media"].ToString();` statement potentially throwing NRE (NullReferenceException) if the DataReader state has been closed by previous reading. Use a persisted DataTable while reading DataReader first (see http://stackoverflow.com/questions/18961938/populate-data-table-from-data-reader), then you should place `ddlMedia.DataTextField = dt.Rows[0]["quo_Media"].ToString();` – Tetsuya Yamamoto Jun 09 '16 at 08:29
  • you want to copy one dropdowns datasource to another..? – KanisXXX Jun 09 '16 at 08:54

1 Answers1

0

Pretend you have a dropdownlist call "ddl". First using a dataset to get the data you want into a dataset. The code will be like:

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();
120196
  • 283
  • 6
  • 14
  • DataValue is The sql column name right?I'm not using it.What I did is the Items in dropdownlist manually.It's not coming form the database.But I am saving the ListItems to database though. – rai nalasa Jun 09 '16 at 08:46
  • Sorry I tot you wan put "quo_Size" as the value of dropdownlist, but you can try the DataBind for your case – 120196 Jun 09 '16 at 08:51