2013-07-29 77 views
0

我得到拋出了以下錯誤:指數在asp.net數組的邊界之外用vb

index was outside the bounds of the array 

當使用此代碼:

con.Open() 
      qur = "select Username,password from registration where Username='" + TextBox1.Text + "'" 
      cmd = New SqlCommand(qur, con) 
      dr = cmd.ExecuteReader() 
      If dr.HasRows() Then 
       dr.Read() 
       Session("us1") = dr.GetValue(11).ToString() 
       Session("ps1") = dr.GetValue(12).ToString() 
       If Session("us1") = TextBox1.Text And Session("ps1") = TextBox2.Text Then 
        Response.Redirect("APP.aspx") 
       End If 
      End If 
     End Sub 

可能有人請指出哪裏/爲什麼會出錯?

+0

把斷點,看看dr有這些11和12項 你得到這個錯誤,因爲沒有什麼在11或可能是第12的位置。 – Rex

+0

確保'dr'有13個項目。 –

+3

請注意,您的代碼是廣泛開放的SQL注入 - 儘可能使用參數化查詢 –

回答

1

檢查你的序號。您可能會提供一個不存在的索引。

指數從0而不是11,12開始,你可能想嘗試與10,11的GetValue

1

我會懷疑問題在於:

Session("us1") = dr.GetValue(11).ToString() 
Session("ps1") = dr.GetValue(12).ToString() 

但是,不要使用序號使用列名:

Session("us1") = dr("column1").ToString() 
Session("ps1") = dr("column2").ToString() 

排序可以很容易地改變炸燬你的序號。通過使用列名稱,您不依賴訂單,因此您不會收到索引錯誤。

如果該列不存在,您將得到另一種錯誤。

+0

非常感謝你...現在我得到了答案謝謝 – gomathi

+0

不要忘記接受。 – Lloyd

1

你試圖獲得字段不存在值:

dr.GetValue(11) 

dr.GetValue(12) 

請注意,您的查詢只選擇兩列(用戶名和密碼)

select Username,password 

可以使用GetValue(0)GetValue(1)或使用列名稱dr("Username")dr("password")

相關問題