我需要你們的幫助,當我在文本框中鍵入的東西,它向我發送了以下錯誤:「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();
}
}
當您從閱讀器獲取數據時,請確保您使用的是正確的列名稱。 「B.Nombre」在您的數據集中不存在。 爲列名使用別名。 更改您的選擇查詢 SELECT B.Nombre as B_Nombre,G.Nombre as G_Nombre –
添加列別名(_B.Nombre as NombreB,..._)後,請花點時間瞭解Sql注入以及爲什麼你的代碼是一個等待發生的災難 – Steve