2017-06-19 72 views
3

我需要你们的帮助,当我在文本框中键入的东西,它向我发送了以下错误:“System.IndexOutOfRangeException”其他信息:“B.Nombre”。 我不知道它可能是什么,我正在寻找错误,甚至尝试使用参数,没有任何东西。 谢谢!错误:System.IndexOutOfRangeException

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(); 
    } 
} 
+0

当您从阅读器获取数据时,请确保您使用的是正确的列名称。 “B.Nombre”在您的数据集中不存在。 为列名使用别名。 更改您的选择查询 SELECT B.Nombre as B_Nombre,G.Nombre as G_Nombre –

+2

添加列别名(_B.Nombre as NombreB,..._)后,请花点时间了解Sql注入以及为什么你的代码是一个等待发生的灾难 – Steve

回答

4

B.NombreG.NombreD.Nombre都具有相同的列名,因为你不为他们指定的别名。 B,G,D前缀只是表名的别名,但它们不是列名的一部分。所以你不能使用myReader["B.Nombre"](例如),因为该列不存在(这是异常的原因)。

而是使用这些列的列别名或int -indexer作为列的索引。

SELECT B.Nombre As B_Nombre,G.Nombre As G_Nombre, D.Nombre As D_Nombre 
... 

,然后你可以使用这些名称,FE:

label3.Text = myReader["B_Nombre"].ToString(); 

您还可以使用的int -indexer:

label3.Text = myReader[0].ToString(); // first column in the select 

而是连接字符串打造你的sql查询应该是use parameterized queries,fe避免sql注入。

2

您需要别名列名称。
所以,你的查询应该是这样的,例如:

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(); 
    } 
} 
+0

当然,还要注意别人在评论中说的话以及@TimSchmelter关于sql注入的出色答案。不建议直接从UI进行连接。 –

相关问题