2013-10-31 37 views
2

我刚刚遇到了我的代码问题。它引发了一个异常。这是代码。索引超出范围未处理的异常vb.net

conn = oSubPayItemDescription.GetDbConnection() 
md = New OleDbCommand("SELECT [sub_pay_item_quantity].[quantity],[sub_pay_item_unit_rate].[rate] FROM " & 
    "[sub_pay_item_quantity],[sub_pay_item_unit_rate] WHERE [sub_pay_item_quantity].[sub item]=[sub_pay_item_unit_rate].[sub item] AND " & 
    "[sub_pay_item_quantity].[sub item]='" & subItem & "' AND [sub_pay_item_quantity].[bridge type]='" & bridgeType & "' " & 
    "AND [sub_pay_item_quantity].[span]='" & span & "'", conn) 

data_reader = cmd.ExecuteReader() 

If data_reader.HasRows = True Then 
    Do While data_reader.Read() 
     payItem = CDbl(data_reader.Item("pay item")) 
     subpayItem = CDbl(data_reader.Item("sub pay item")) 
     subItem = data_reader.Item("sub item") 
     unit = data_reader.Item("unit") 
     bridgeType = data_reader.Item("bridge type") 
     span = (data_reader.Item("span").ToString()) 
     quantity = CDbl(data_reader.Item("quantity")) 
     rate = CDbl(data_reader.Item("rate")) 

     RichTextBox1.AppendText(payItem & vbTab & vbTab & " " & " " & subpayItem & vbTab & vbTab & subItem & vbTab & vbTab & " " & _ 
      unit & vbTab & vbTab & " " & " " & " " & " " & bridgeType & vbTab & vbTab & " " & span & vbTab & _ 
      "" & vbTab & " " & quantity & vbTab & rate & vbNewLine) 
    Loop 
Else 
    MsgBox("Unit rate does not exist", vbCritical, "Bridge Construction Cost Estimate") 
End If 

它在到达do while循环时指示付款项目时引发异常。

+0

您只选择查询中的数量和范围,难道不是问题吗? – Abbas

回答

2

因此指定的名称不会在DataReaderthe documentation表明存在:

IndexOutOfRangeException:具有指定名称的列没有发现

,我不能在查询中看到此列。您只选择quantityrate

+0

谢谢蒂姆,我只是修好了它。我没有意识到这一点。 –