2017-06-19 76 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進行連接。 –

相關問題