2
Answers

Storing SQL Select Results into Variable

Ask a question
Keith Gardner

Keith Gardner

13y
13.3k
1
I have a SQL Connection setup to select ID, Model and Family. The Model and ID are stored into a Dropdownlist. I want to store the Family which is an INT into a variable that can be referenced later. I can't seem to figure out how to store the single value of FAMILY into a variable.

using (SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ToString()))
            {
                using (SqlCommand myCmd = new SqlCommand())
                {

                    myConn.Open();
                    myCmd.Connection = myConn;
                    myCmd.CommandText = "SELECT ID, MODEL, FAMILY FROM Table ORDER BY SORT_ORDER ASC";
                    myCmd.CommandType = System.Data.CommandType.Text;
                    using (SqlDataReader myReader = myCmd.ExecuteReader())
                    {
                        DropDownListRemote.DataSource = myReader;
                        DropDownListRemote.DataTextField = "MODEL";
                        DropDownListRemote.DataValueField = "ID";
                        DropDownListRemote.DataBind();
                        DropDownListRemote.Items.Insert(0, new ListItem("--Select a Model--", "0"));
                        DropDownListRemote.SelectedIndex = 0;
                    }
                }
            } 



Answers (2)