Error: System.IndexOutOfRangeException
I need your help. When I enter something in the textbox, it sends me the following error: "System.IndexOutOfRangeException" and More info: "B.Nombre" . I do not know what it might be, I am looking for an error, I will even try with parameters and nothing. Thank!
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();
}
}
source to share
B.Nombre
, G.Nombre
, D.Nombre
All have the same column name, because you do not specify aliases for them. Prefixes B
, G
, D
- it is just an alias for the table name, but they are not part of the column name. Therefore you cannot use myReader["B.Nombre"]
(for example) because this column does not exist (reason for reason).
Instead, use column aliases for those columns, or int
-indexer with the column index.
SELECT B.Nombre As B_Nombre,G.Nombre As G_Nombre, D.Nombre As D_Nombre
...
and then you can use these names, fe:
label3.Text = myReader["B_Nombre"].ToString();
You can also use int
-indexer:
label3.Text = myReader[0].ToString(); // first column in the select
Instead of concatenating strings to create your sql query, you should use parameterized queries , i.e. to avoid sql injection.
source to share
You need to provide the column name aliases.
Therefore, your request should look something like this:
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();
}
}
source to share