15
I am 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
.
Follow 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 list the corresponding data as the author name, price to load mine textbox
with that information ?
Like create a list and load mine with it combobox
, and when the user selects an item I load the textbox
with the information of that item.
What is the behavior of this code? Displays error? Executes, but the values do not appear? What would be?
– Leonel Sanches da Silva
What is your main problem in this code?
– CypherPotato
@Ciganomorrisonmendez, I made an issue in the question , I believe this is what she needed...
– Marco Souza
I’m having some difficulty to make the data that are linked to combobox appear in the textbox, for example: I have a table books in the database and in my combobox appear the names of the books registered and I wanted the author of that book selected in the combobox to appear in the textbox
– Natalia Souza
@Marconciliosouza The correct would be to edit itself, but approving the edition we can reopen.
– Leonel Sanches da Silva
@Gypsy omorrisonmendez, perfect
– Marco Souza
I want help to solve my problem, not to edit my question. Sorry if I was rude, but in the textbox appears datareader gridview instead of the requested data.
– Natalia Souza
@Nataliasouza, The point is that it was not clear your question, See what you need explain if I understood, in the first method your
preencherCBDescricao
you carry yourcbocompra
with the result of your select and your methodcbocompra_SelectedIndexChanged
You want to carry your ownTextBox
according to what you have in thecbocompra
for this you have to keep a list of the select you did in the first method or go again to the bank to fetch this information, ... if this is it edits your question explaining better...– Marco Souza
@Nataliasouza, I think we don’t always express ourselves in the best way, I think the well-meaning editions are welcome. Back to your problem, it’s okay to upload a project to Git with the complete solution to your problem ? The answers below are satisfactory ?
– Luã Govinda Mendes Souza