I'm doing a project in C # and SQL Server, and I need the item selected in the combo to display the corresponding values in a textbox
.
Here are the codes I've tried:
private void preencherCBDescricao()
{
SqlConnection con = new SqlConnection("Data Source=FS5;Initial Catalog=bdsi01;User ID=bdsi01;Password=*****");
try
{
con.Open();
}
catch (SqlException sqle)
{
MessageBox.Show("Falha ao efetuar a conexão. Erro: " + sqle);
}
String scom = "select titulo from Livros";
SqlDataAdapter da = new SqlDataAdapter(scom, con);
DataTable dtResultado = new DataTable();
dtResultado.Clear();//o ponto mais importante (limpa a table antes de preenche-la)
cbocompra.DataSource = null;
da.Fill(dtResultado);
cbocompra.DataSource = dtResultado;
cbocompra.DisplayMember = "titulo";
cbocompra.SelectedItem = "";
cbocompra.Refresh(); //faz uma nova busca no BD para preencher os valores da cb de livros.
}
private void cbocompra_SelectedIndexChanged(object sender, EventArgs e)
{
string stg;
stg = cbocompra.SelectedItem.ToString();
SqlConnection con = new SqlConnection("Data Source=FS5;Initial Catalog=bdsi01;User ID=bdsi01;Password=*******");
try
{
con.Open();
}
catch (SqlException sqle)
{
MessageBox.Show("Falha ao efetuar a conexão. Erro: " + sqle);
}
String scom = "select * from Livros where titulo="+ stg;
txtautor1.Text = cbocompra.SelectedItem.ToString();
txtedit1.Text = cbocompra.SelectedItem.ToString();
txtpreco.Text = cbocompra.SelectedItem.ToString();
}
The question is, how can I save the values of my select in a list and after the item is selected I use the value of that item to search in the corresponding data list as the name Author, price to load my textbox
with this information?
Type create a list and with it load my combobox
, and when the user selects an item I load the textbox
with the information of that item.