更新gridview空值(对象引用未设置为对象的实例) [英] Updating gridview empty value (object reference not set to an instance of an object)

查看:84
本文介绍了更新gridview空值(对象引用未设置为对象的实例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All我允许用户从datagridview更新sql server数据库数据,我从GridView获取这样的值:

 Dim rphone As String = MetroGrid2。项目(5,i).Value.ToString 



但是如果用户从数据网格删除电话号码并且电话号码为空我得到此错误未设置对象引用到一个对象的实例。数据库中的数字列是否允许为空。

有什么问题?



我尝试了什么:



Dim rphone As String = MetroGrid2.Item(5,i).Value.ToString

解决方案

这是开发人员面临的常见错误之一,并经常询问此问题。

当您尝试从对象访问属性或方法时为null,则会抛出错误。 />
为了避免这个错误,你只需要验证对象的空值,然后再访问它们。

参考这个简单的例子。

 SomeClass obj = null; 
obj.SomeProperty = 25; //将抛出错误
obj.SomeMethod(); //将抛出错误


if(null!= obj)
{
obj.SomeProperty = 25; //有效
obj.SomeMethod(); //有效

}

//样本类
公共类SomeClass
{
public int SomeProperty {get;组; }
public void SomeMethod(){}
}



同样,在访问它们之前,您必须分别验证所有对象和属性及其对象。


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近了一个bug。


我是这样做的,谢谢你的帮助



  Dim  sname 作为 字符串 
如果 MetroGrid2.Item( 7 ,i).Value 没什么 然后
sname =
否则
sname = MetroGrid2.Item( 7 ,i).Value
End 如果


Hello All i'm allowing User to update sql server database data from datagridview and i get the value from the GridView like this:

Dim rphone As String = MetroGrid2.Item(5, i).Value.ToString


But if the user delete phone number from the datagrid and the phone number is empty i get this error "Object reference not set to an instance of an object." the number column in database is allow null.
what the problem??

What I have tried:

Dim rphone As String = MetroGrid2.Item(5, i).Value.ToString

解决方案

This is one of the common errors faced by the developers and this question is asked frequently.
When you try to access a property or method from an object which is null, then the error is thrown.
inorder to avoid this error, you just have to validate the object for null values, beore accessing them.
refer this simple example.

       SomeClass obj = null;
       obj.SomeProperty = 25; // error will be thrown
       obj.SomeMethod(); // errow will be thrown


       if (null != obj)
       {
           obj.SomeProperty = 25; // valid
           obj.SomeMethod();  // valid

       }

// sample class
public class SomeClass
    {
        public int SomeProperty { get; set; }
        public void SomeMethod() { }
    }      


Similarly you will have to validate all objects and properties and their objects respectively before accessing them.


You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


i done it like this, thanks for helping

Dim sname As String
           If MetroGrid2.Item(7, i).Value Is Nothing Then
               sname = ""
           Else
               sname = MetroGrid2.Item(7, i).Value
           End If


这篇关于更新gridview空值(对象引用未设置为对象的实例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆