简体   繁体   中英

ASPX dropdownlist selected item

I have this code to show items in a dropdown list from DB.

 string query = "SELECT ID, Firstname, Lastname FROM Crew";
            string constr = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            ListItem item = new ListItem();
                            item.Text = sdr["Lastname"].ToString() + ' ' + sdr["Firstname"].ToString();
                            item.Value = sdr["ID"].ToString();
                            item.Selected = ""
                            ddlCrew.Items.Add(item);
                        }
                    }
                    con.Close();
                }
            }
            ddlCrew.Items.Insert(0, new ListItem("--Select Crew--", "0"));

How could I have item selected depending on value?

table Crew: CrewID, Firstname, Lastname, IsActive

table Report: ReportID, Crew

So I need, to be selected Firstname + Lastname from table Crew depending on the ID which is entered in column Crew in table Report.

EDITED:

As Tony adviced I now have this code:

            string query = "SELECT RepCrewID, Firstname, Lastname FROM tblRepCrew; SELECT ServiceTicketReportID, InspectedBy FROM tblServiceTicketReport WHERE ServiceTicketID = 123";
            string constr = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {

                            ListItem item = new ListItem();
                            item.Text = sdr["Lastname"].ToString() + ' ' + sdr["Firstname"].ToString();
                            item.Value = sdr["RepCrewID"].ToString();   

                      if ( sdr["InspectedBy"].ToString() == sdr["RepCrewID"].ToString() )
                            {                        
                                item.Selected = true;
                            }

                            ddlCrew.Items.Add(item);
                        }
                    }
                    con.Close();
                }
            }
            ddlCrew.Items.Insert(0, new ListItem("-- Select Crew --", "0"));

The only thing I need to make is the IF statment. As far as I understand I have to check if the ID in the column InspectedBy (table tblServiceTicketReport) is the same as RepCrewID. If its the same select it, if not or its black, to be able to select new crew member. Something like this:

             if ( sdr["InspectedBy"].ToString() == sdr["RepCrewID"].ToString() )
                            {                        
                                item.Selected = true;
                            }

EDITED 2

So far I have:

string query = "SELECT ServiceTicketReportID, InspectedBy FROM tblServiceTicketReport WHERE ServiceTicketID = 21336; SELECT RepCrewID, Firstname, Lastname FROM tblRepCrew";
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                      while (sdr.Read())
                        {
                           something = sdr["InspectedBy"].ToString(); 
                        }
                            if (sdr.NextResult())
                                {
                                    while (sdr.Read())
                                    {
                                        ListItem item = new ListItem();
                                        item.Text = sdr["Lastname"].ToString() + ' ' + sdr["Firstname"].ToString();
                                        item.Value = sdr["RepCrewID"].ToString();   

                  if ( something == sdr["RepCrewID"].ToString())
                        {                        
                            item.Selected = true;
                        }

                        ddlCrew.Items.Add(item);

                            }
                        }
                    }
                    con.Close();
                }
            }
            ddlCrew.Items.Insert(0, new ListItem("-- Select Crew --", "0"));

But im getting here this error:

>>> Error executing child request for ServiceTicketReportEdit.aspx.
>>> ServiceTicketReportEdit.aspx.cs(58): error CS0103: The name 'something' does not exist in the current context

Tables:

tblServiceTicketReport: ServiceTicketReportID, ServiceTicketID, InspectedBy

tblRepCrew: RepCrewID, Firstname, Lastname

dropdownlist.ClearSelection(); //for clearing previous selection
dropdownlist.Items.FindByValue(value).Selected = true; //select item by value

You can check the condition inside the while loop and make default selected

        string query = "SELECT ServiceTicketReportID, InspectedBy FROM tblServiceTicketReport WHERE ServiceTicketID = 123; SELECT RepCrewID, Firstname, Lastname FROM tblRepCrew";
        string constr = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
        string something = null; 
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                       something = sdr["InspectedBy"].ToString(); 
                    }
                    if (sdr.NextResult())
                    {
                    while (sdr.Read())
                    {

                        ListItem item = new ListItem();
                        item.Text = sdr["Lastname"].ToString() + ' ' + sdr["Firstname"].ToString();
                        item.Value = sdr["RepCrewID"].ToString();   

                  if ( something == sdr["RepCrewID"].ToString())
                        {                        
                            item.Selected = true;
                        }

                        ddlCrew.Items.Add(item);
                    }
                  }
                }
                //using auto close the connection
                //con.Close();

            }
        }
        ddlCrew.Items

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM