Selecionar item no Combobox e automaticamente preencher um textbox
tenho um método que popular o meu combobox (de filiais de uma empresa), ele é carregado no \"load\", gostaria que assim que o usuario
selecionasse uma filial o textbox do CNPF fosse preenchido automaticamente, segue código:
selecionasse uma filial o textbox do CNPF fosse preenchido automaticamente, segue código:
private void cmbFilial_SelectedIndexChanged(object sender, EventArgs e) { SqlConnection cn = new SqlConnection(); cn = conexao.ConectarSqlServer(); try { if (cmbFilial.SelectedIndex >= 0 ) { int valor = Convert.ToInt32(cmbFilial.SelectedValue.ToString()); txtFilial.Text = Convert.ToString(valor); if (cn.State == 0) cn.Open(); SqlCommand cmd = new SqlCommand(\"select CNPFFilial from FILIAL where IdFilial= @idfilial\", cn); cmd.Parameters.Add(\"@idfilial\", SqlDbType.Int).Value = Convert.ToInt32(txtFilial.Text); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { txtCnpfFilial.Text = dr[\"CNPFFilial\"].ToString(); } } } catch (Exception i) { MessageBox.Show(i.Message); } finally { cn.Close(); } }
Jhonatas
Curtidas 0