There is no row at position 0,郁闷一个月了,找不到人解答,跪求解决方法!
我做的是添加商品到购物车,可是不知道那里出了错,就是打不开页面,每次都给我同样的错误信息:System.IndexOutOfRangeException: There is no row at position 0Source Error:
Line 27: dt = Cpdat.Tables("pdtt")
Line 28: Dim dr As DataRow = Cpbt.NewRow()
Line 29: dr(0) = dt.Rows(0)("pdid")
Line 30: dr(1) = dt.Rows(0)("pdnom")
Line 31: dr(2) = dt.Rows(0)("pdpxsl")
Source File: C:\tresorexotique\pdacheter6.aspx Line: 29
以下是我的源代码:
<%@ Page Language="VB" ContentType="text/html" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>
<script runat="server">
Dim pdtb As DataTable
Dim pdview As DataView
Sub pddatatable()
pdtb = New DataTable()
pdtb.Columns.Add(new DataColumn("pdid",GetType(String)))
pdtb.Columns.Add(new DataColumn("pdnom",GetType(String)))
pdtb.Columns.Add(new DataColumn("pdpxsl",GetType(single)))
pdtb.Columns.Add(new DataColumn("pdnb",GetType(Int32)))
pdtb.Columns.Add(new DataColumn("pdsum",GetType(double)))
End Sub
Sub Addpdtb()
Dim pdConn,pdstring AS String
Dim conn AS OleDbConnection
Dim pdadapter AS OleDbDataAdapter
Dim pddat AS DataSet
Dim dt AS DataTable
pdConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.MapPath ("database/infopd.mdb")
conn = New OledbConnection(pdConn)
pdstring = "SELECT pdid,pdnom,pdpxsl from pdtt WHERE pdid='" &Trim(Request.QueryString("pdid")) & "'"
pdadapter = new OleDbDataAdapter(pdstring,conn)
pddat = New DataSet()
pdadapter.Fill(pddat,"pdtt")
dt = pddat.Tables("pdtt")
Dim dr As DataRow = pdtb.NewRow()
dr(0) = dt.Rows(0)("pdid")
dr(1) = dt.Rows(0)("pdnom")
dr(2) = dt.Rows(0)("pdpxsl")
dr(3) = 1
dr(4) = dr(2) * dr(3)
pdtb.Rows.Add(dr)
End Sub
Sub pdTotal()
Dim i As Integer
Dim pdto As single=0
For i=0 to pdtb.Rows.Count-1
pdto=pdto+pdtb.Rows(i)(4)
Next
pdcount.text=pdto
End Sub
Sub AchGrid_Delete(sender As Object, e As DataGridCommandEventArgs)
Dim dr As DataRow = pdtb.NewRow()
Dim itemCell As TableCell = e.Item.Cells(0)
Dim item As String = ItemCell.Text
pdview.RowFilter = "pdid='" & item & "'"
If pdview.Count > 0 Then
pdview.Delete(0)
End If
pdview.RowFilter = ""
pdTotal
AchGrid.DataSource = pdview
AchGrid.DataBind()
End Sub
Sub pdUpdate_Click(sender As object, e As EventArgs)
Dim i,j As Integer
Dim _item As DataGridItem
j=0
For i=0 To AchGrid.Items.Count-1
_item = AchGrid.Items(i)
Dim CountText As TextBox = _item.FindControl("pdnbtxt")
pdtb.Rows(i)(3) = CountText.Text
pdtb.Rows(i)(4) = pdtb.Rows(i)(2) * pdtb.Rows(i)(3)
Next
pdTotal
AchGrid.DataSource = New DataView(pdtb)
AchGrid.DataBind()
End Sub
Sub ClearBy_Click(sender As object, e As EventArgs)
Dim i As Integer
For i=0 to pdview.Count-1
pdview.Delete(i)
Next
pdcount.text=0
pdview.RowFilter = ""
AchGrid.DataSource = pdview
AchGrid.DataBind()
End Sub
Sub Page_Load(Sender As Object,e As EventArgs)
If Session("pdshop") Is Nothing Then
pddatatable
Session("pdshop") = pdtb
Else
pdtb = Session("pdshop")
End If
pdview = New DataView(pdtb)
If (Not IsPostBack) Then
If Session("USENAME")="" or Session("USENAME")=nothing Then
response.Redirect("noninscri.htm")
End If
Addpdtb
pdTotal
AchGrid.DataSource = pdview
AchGrid.DataBind()
End If
End Sub
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
。。。。。接下来的我想就省略了
我都郁闷了一个月了,也找不到懂行的给我解答。那位好心人行行好一定要帮帮我啊!很急用!!!拜托拜托了!
或者也可以直接email我:mirelley@hotmail.com
在线等,谢谢了!!! --------------------编程问答-------------------- IndexOutOfRangeException是说索引错误,不应该从0开始
你的pdtb表中无任何数据你成就从它里面取数据当然会出错!
Line 27: dt = Cpdat.Tables("pdtt")
Line 28: Dim dr As DataRow = Cpbt.NewRow()
Line 29: dr(0) = dt.Rows(0)("pdid")
Line 30: dr(1) = dt.Rows(0)("pdnom")
Line 31: dr(2) = dt.Rows(0)("pdpxsl")
在pdtb表的第1行(索引为0)没有任何数据,就是这个原因!!! --------------------编程问答-------------------- 樓上說的對,你可以這樣處理一下:
Line 29: dr(0) = dt.Rows(0)("pdid") & ""
Line 30: dr(1) = dt.Rows(0)("pdnom") & ""
Line 31: dr(2) = dt.Rows(0)("pdpxsl") & "" --------------------编程问答-------------------- 谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢 --------------------编程问答-------------------- 第一行 数据没有值 引起的 加条件判断下 有值 赋值 没值跳过 --------------------编程问答-------------------- There is no row at position 0
字面意思很明显,居然困惑一个月,深表不解 --------------------编程问答--------------------
判断下取出的table是否有内容,没内容就不要继续了,不就OK了? --------------------编程问答-------------------- 明显就是 table 没有内容。。。
补充:.NET技术 , VB.NET