Skip to content

Storing SQL Select Results into Variable

Keith Gardner

Asked Feb 7, 2011, 9:51 AM EST2 answers
Share
LinkedInX
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.

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

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Suthish Nair
    0
    Java
    List<int> i = new List<int>();
    using (SqlDataReader myReader = myCmd.ExecuteReader())
    {    
     while (myReader.Read())
     {   
      i.Add = myReader[3];
      i.Add = Convert.ToInt16(myReader[2]);
      DropDownListRemote.DataSource = myReader;  
      DropDownListRemote.DataTextField = "MODEL";  
      DropDownListRemote.DataValueField = "ID";  
      DropDownListRemote.DataBind();  
      DropDownListRemote.Items.Insert(0, new ListItem("--Select a Model--", "0"));  
      DropDownListRemote.SelectedIndex = 0;
     } 
    }
    0
  • 1 short reply
    • Suthish Nair
      0
      query resolved?
      0