如何给null的字符串赋值

billlyh 2010-04-21 01:40:56
NowTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
For Each dr As DataGridViewRow In Me.DataGridView1.Rows
Dim cbx As DataGridViewCheckBoxCell = DirectCast(dr.Cells(0), DataGridViewCheckBoxCell)
If CBool(cbx.FormattedValue) Then
'MessageBox.Show(dr.Cells(1).Value.ToString())

Dim sGUID2 As String = System.Guid.NewGuid.ToString()
Dim insert_DataGridView1 As String
insert_DataGridView1 = "insert into PUB.UD33(Key1,Number01,Character01,Character02,Character03,Character04,Character05,Character06," + _
"Character07,Character08,Date01,Character09,Character10,Character11,Character12,Character13,Character14,Character15" + _
") values('" + sGUID2.ToString() + "','" + int_Number01.ToString() + "','" + dr.Cells(1).Value.ToString() + "'," + _
"'" + dr.Cells(2).Value.ToString() + "','" + dr.Cells(3).Value.ToString() + "','" + dr.Cells(4).Value.ToString() + "'," + _
"'" + dr.Cells(5).Value.ToString() + "','" + dr.Cells(6).Value.ToString() + "','" + dr.Cells(7).Value.ToString() + "'," + _
"'" + dr.Cells(8).Value.ToString() + "','" + NowTime + "','" + dr.Cells(9).Value.ToString() + "','" + dr.Cells(10).Value.ToString() + "',"

+ _
"'" + dr.Cells(11).Value.ToString() + "','" + dr.Cells(12).Value.ToString() + "','" + dr.Cells(13).Value.ToString() + "'," + _
"'" + dr.Cells(14).Value.ToString() + "','" + dr.Cells(15).Value.ToString() + "')"
'MessageBox.Show(NowTime)
Me.TextBox1.Text = insert_DataGridView1
'lyh_odbc1.Get_DataTable(insert_DataGridView1)


End If
运行时报错,指向insert_DataGridView1 =...........这一句,
提示“未将对象引用设置到对象的实例。”


仔细检查,是有几个dr.Cells为null的原因,

1-5列是通过
datagridview/属性/杂项/Columns/Collection 添加的,

想给这几列赋值的,但却没成功
If dr.Cells(1).Value.ToString() Is Nothing Then
dr.Cells(1).Value = "未输入 "
End If
系统还是提示dr.Cells(1).Value未将对象引用调协到对象的实例

哪位帮帮我呀
...全文
269 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuyileneal 2010-04-21
  • 打赏
  • 举报
回复
dr.Cells(1).Value
因为它是空的,所以用它去调用函数,就会造成空引用
billlyh 2010-04-21
  • 打赏
  • 举报
回复
1、2楼,你们是对的,为什么要去了tostring()
harry999 2010-04-21
  • 打赏
  • 举报
回复
判断HasValue属性
  • 打赏
  • 举报
回复
If dr.Cells(1).Value Is Nothing Then
liuyileneal 2010-04-21
  • 打赏
  • 举报
回复

If dr.Cells(1).Value Is Nothing Then
dr.Cells(1).Value = "未输入 "
End If


试一试

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧