user6558380
user6558380

Reputation:

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

Upvotes: 0

Views: 111

Answers (2)

Tony Dong
Tony Dong

Reputation: 3313

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

Upvotes: 1

Moshe D
Moshe D

Reputation: 778

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

Upvotes: 1

Related Questions