I am working on a project on c # and sql server. When selecting an item from a comboboxcolumn, you must bring me the description and price from the database, if it does, but it marks the following error.
I have these events
private void dgtDVenta_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
dgvCombo = e.Control as DataGridViewComboBoxEditingControl;
if (dgvCombo != null)
{
dgvCombo.SelectedIndexChanged += new EventHandler(dgtDVenta_SelectedIndexChanged);
}
}
private void dgtDVenta_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (dgvCombo != null)
dgvCombo.SelectedIndexChanged -= new EventHandler(dgtDVenta_SelectedIndexChanged);
}
private void dgtDVenta_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox combo = sender as ComboBox;
cadena = "Server= " + serverName + ";Database= " + dataBase + " ;Trusted_Connection=True";
string des;
string precio;
using (SqlConnection conn = new SqlConnection(cadena))
{
conn.Open();
string sql = @"SELECT Descripcion_Prod, Precio FROM Producto WHERE IdProducto = @IdProducto";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@IdProducto", combo.Text);
adaptador = new SqlDataAdapter(cmd);
tabla = new DataTable();
adaptador.Fill(tabla);
//des = Convert.ToString(cmd.ExecuteScalar());
des = tabla.Rows[0]["Descripcion_Prod"].ToString();
precio = tabla.Rows[0]["Precio"].ToString();
}
DataGridViewRow row = dgtDVenta.CurrentRow;
row.Cells["Descripcion"].Value = des;
row.Cells["Precio"].Value = precio;
}