I need your help, When I type something in the text box, it sends me the following error: "System.IndexOutOfRangeException" and Additional Information: "B.Nombre" . I don't know what it could be, I'm looking for the error even try with parameters and nothing. Thanks!
var strcn = System.Configuration.ConfigurationManager.ConnectionStrings["ConexionDB"].ToString();
using (SqlConnection con = new SqlConnection(strcn))
{
con.Open();
string commandString = @"SELECT
B.Nombre,G.Nombre,D.Nombre,B.Precio,G.Precio,D.Precio,
B.Gramos,G.Gramos,D.Gramos,B.Tabletas,G.Tabletas,D.Tabletas
FROM TblBenavides B INNER JOIN TblGuadalajara G ON B.Nombre = G.Nombre
INNER JOIN TblDelAhorro D ON G.Nombre = D.Nombre
WHERE B.Nombre='" + TxtMedicamento.Text + "'" +
"AND G.Nombre='" + TxtMedicamento.Text +
"' AND D.Nombre='" + TxtMedicamento.Text + "'";
SqlCommand cmd = new SqlCommand(commandString, con);
SqlDataReader myReader = cmd.ExecuteReader();
if (myReader.Read())
{
label3.Text = myReader["B.Nombre"].ToString();
label4.Text = myReader["G.Nombre"].ToString();
label5.Text = myReader["D.Nombre"].ToString();
this.label8.Text = myReader["B.Precio"].ToString();
this.TxtGprecio.Text = myReader["G.Precio"].ToString();
this.TxtDprecio.Text = myReader["D.Precio"].ToString();
this.label6.Text = myReader["B.Gramos"].ToString();
this.TxtGgramos.Text = myReader["G.Gramos"].ToString();
this.TxtDgramos.Text = myReader["D.Gramos"].ToString();
this.label7.Text = myReader["B.Tabletas"].ToString();
this.TxtGtabletas.Text = myReader["G.Tabletas"].ToString();
this.TxtDtabletas.Text = myReader["D.Tabletas"].ToString();
}
}
B.Nombre,G.Nombre,D.Nombre all have the same column name because you don't specify aliases for them. The B,G,D prefixes are just the alias for the table-names but they are not part of the column-name. So you can't use myReader["B.Nombre"](for example) because that column doesn't exist(that's the reason for the exception).
Instead use column aliases for those columns or the int-indexer with the index of the column.
SELECT B.Nombre As B_Nombre,G.Nombre As G_Nombre, D.Nombre As D_Nombre
...
and then you can use these names, f.e.:
label3.Text = myReader["B_Nombre"].ToString();
You can also use the the int-indexer:
label3.Text = myReader[0].ToString(); // first column in the select
Instead of concatenating strings to build your sql query you should use parameterized queries, f.e. to avoid sql-injection.
You need to alias your column names.
So your query should be something like this for example:
var strcn = System.Configuration.ConfigurationManager.ConnectionStrings["ConexionDB"].ToString();
using (SqlConnection con = new SqlConnection(strcn))
{
con.Open();
string commandString = @"SELECT
B.Nombre as BNombre,G.Nombre as GNombre,D.Nombre as DNombre,B.Precio as BPrecio ,G.Precio as GPrecio,D.Precio as DPrecio,
B.Gramos as BGramos,G.Gramos as GGramos,D.Gramos as DGramos,B.Tabletas as BTabletas,G.Tabletas as GTabletas,D.Tabletas as DTabletas
FROM TblBenavides B INNER JOIN TblGuadalajara G ON B.Nombre = G.Nombre
INNER JOIN TblDelAhorro D ON G.Nombre = D.Nombre
WHERE B.Nombre='" + TxtMedicamento.Text + "'" +
"AND G.Nombre='" + TxtMedicamento.Text +
"' AND D.Nombre='" + TxtMedicamento.Text + "'";
SqlCommand cmd = new SqlCommand(commandString, con);
SqlDataReader myReader = cmd.ExecuteReader();
if (myReader.Read())
{
label3.Text = myReader["BNombre"].ToString();
label4.Text = myReader["GNombre"].ToString();
label5.Text = myReader["DNombre"].ToString();
this.label8.Text = myReader["BPrecio"].ToString();
this.TxtGprecio.Text = myReader["GPrecio"].ToString();
this.TxtDprecio.Text = myReader["DPrecio"].ToString();
this.label6.Text = myReader["BGramos"].ToString();
this.TxtGgramos.Text = myReader["GGramos"].ToString();
this.TxtDgramos.Text = myReader["DGramos"].ToString();
this.label7.Text = myReader["BTabletas"].ToString();
this.TxtGtabletas.Text = myReader["GTabletas"].ToString();
this.TxtDtabletas.Text = myReader["DTabletas"].ToString();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With