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.
- private void Form1_Load(object sender, EventArgs e)
- {
- using (SqlConnection conn = new SqlConnection(@"Data Source=######;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"))
- {
- try
- {
- string query = "SELECT DISTINCT direction + ' ' + street + ' ' + city + ', ' + state + ' ' + zip as 'Address' FROM ###.DBO.#### order by address";
- SqlCommand cmd = new SqlCommand(query,conn);
- SqlDataAdapter da = new SqlDataAdapter(query, conn);
- conn.Open();
- DataTable dt = new DataTable();
- da.Fill(dt);
- cboAddress.DataSource = dt;
- cboAddress.ValueMember = "Address";
- cboAddress.DisplayMember = "Address";
- conn.Close();
- }
- catch (Exception)
- {
- MessageBox.Show("Error loading addresses");
- }
- }
- }
- private void cboAddress_SelectionChangeCommitted(object sender, EventArgs e)
- {
- using (SqlConnection conn = new SqlConnection(@"Data Source=scsqllistener;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"))
- {
- try
- {
- conn.Open();
- 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);
- cmd.Parameters.AddWithValue("@addr", cboAddress.DisplayMember);
- SqlDataReader myreader = cmd.ExecuteReader();
- while (myreader.Read())
- {
- textBox1.Text = myreader["geo_primary_area"].ToString();
- textBox2.Text = myreader["geo_law_sub1"].ToString();
- textBox3.Text = myreader["geo_law_sub2"].ToString();
- }
- myreader.Close();
- myreader.Dispose();
- conn.Close();
- }
- catch (Exception)
- {
- MessageBox.Show("Error retrieving values");
- }
- }
- }