Kevin Fralick

Kevin Fralick

  • NA
  • 21
  • 11.3k

Displaying queried fields based on combobox selection

Mar 29 2021 5:46 PM
Hi all, I'm trying to populate 3 textboxes based on the combobox value.  As you can see, it's not that straight forward. I first popuate the combobox with contacenated values to form an address which is distinct.  Then, upon button click, I need to query for the value of 3 other fields based on this selection.  The SQL gets kind of interesting because I have to concatenate the fields.  The combobox is populating fine, but nothing is displayed in the textboxes when changing its value.  I am a novice with c# so your help is greatly appreciated.
  1. private void Form1_Load(object sender, EventArgs e)    
  2. {   
  3.    using (SqlConnection conn = new SqlConnection(@"Data Source=######;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"))    
  4.    {    
  5.        try    
  6.        {    
  7.            string query = "SELECT DISTINCT direction + ' ' + street + ' ' + city + ', ' + state + ' ' + zip as 'Address' FROM ###.DBO.#### order by address";    
  8.            SqlCommand cmd = new SqlCommand(query,conn);    
  9.            SqlDataAdapter da = new SqlDataAdapter(query, conn);    
  10.            conn.Open();    
  11.            DataTable dt = new DataTable();    
  12.            da.Fill(dt);    
  13.            cboAddress.DataSource = dt;    
  14.            cboAddress.ValueMember = "Address";    
  15.            cboAddress.DisplayMember = "Address";  
  16.            conn.Close();  
  17.        }    
  18.        catch (Exception)    
  19.        {  
  20.            MessageBox.Show("Error loading addresses");    
  21.        }  
  22.    }  
  23. }  
  24. private void cboAddress_SelectionChangeCommitted(object sender, EventArgs e)    
  25. {    
  26.     using (SqlConnection conn = new SqlConnection(@"Data Source=scsqllistener;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"))    
  27.     {    
  28.         try    
  29.         {    
  30.             conn.Open();    
  31.             SqlCommand cmd = new SqlCommand("select geo_primary_area, geo_law_sub1, geo_law_sub2 from cad.dbo.cadgeo where direction + ' ' + street + ' ' + city + ', ' + state + ' ' + zip = @addr", conn);               
  32.             cmd.Parameters.AddWithValue("@addr", cboAddress.DisplayMember);  
  33.             SqlDataReader myreader = cmd.ExecuteReader();    
  34.             while (myreader.Read())    
  35.             {    
  36.                 textBox1.Text = myreader["geo_primary_area"].ToString();    
  37.                 textBox2.Text = myreader["geo_law_sub1"].ToString();    
  38.                 textBox3.Text = myreader["geo_law_sub2"].ToString();  
  39.             }    
  40.             myreader.Close();    
  41.             myreader.Dispose();  
  42.             conn.Close();                      
  43.         }    
  44.         catch (Exception)    
  45.         {  
  46.             MessageBox.Show("Error retrieving values");    
  47.         }   
  48.     }    
  49. }

Answers (2)