[VB.NET]大家看看,这段代码出错了,要怎么改,谢谢,,在线等,当天结贴!!!!!!

本文探讨了一段用于向购物车添加商品的VB.NET代码出现的NullReferenceException异常。主要分析了Session变量未正确初始化的问题,并提出了相应的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VB.NET源码-156个实用实例哦…… <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
大家看看,这段代码出错了,要怎么改,谢谢,,在线等,当天结贴!!!!!!
Private Sub addnewitem()
Dim nowTable As DataTable = New DataTable( nowCartTable )
nowTable = Session( myCartTable )
Dim data As DataSet
With New BusinessRule.Ecom

data = .GetPro(ID.Text)

End With

Dim newDR As DataRow

newDR = nowTable.NewRow


newDR.Item(0) = ID.Text
newDR.Item(1) = data.Tables(0).Rows(0).Item(2)
newDR.Item(2) = data.Tables(0).Rows(0).Item(5)
newDR.Item(3) = 1
newDR.Item(4) = newDR.Item(2)
nowTable.Rows.Add(newDR)

Session( myCartTable ) = nowTable

End Sub
-----------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 147: Dim newDR As DataRow
Line 148:
Line 149: newDR = nowTable.NewRow
Line 150:
Line 151:

__________________________________________________________________________
nowTable = Session( myCartTable )??
Session是不是一个空值?
__________________________________________________________________________
http://msdn2.microsoft.com/zh-cn/library/system.data.datatable.newrow(VS.80).aspx
__________________________________________________________________________
Session( myCartTable ) 的值为空

Dim nowTable As DataTable
nowTable =(DataTable)Session( myCartTable )
if(nowTable is not nothing)
{
...........................
}
__________________________________________________________________________
应该是这行出错吧: newDR = nowTable.NewRow
是nowTable为空时出错,这里应该加判断,当Session( myCartTable )
为空时,如何作,否则你第一次运行肯定会为空,那你这个代码一定会抛这个异常
__________________________________________________________________________
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值