Datagridview对象引用未设置为对象的实例 [英] Datagridview object reference not set to an instance of an object

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

问题描述

亲爱的大师,



我正在尝试使用vb.net中的以下代码将datagridview行插入mysql表但是得到了遭遇错误,不知道我在哪里我弄错了?请帮助



我的尝试:



Dear gurus,

I am trying to insert datagridview rows to mysql table using the below code in vb.net but getting the subjected error, dont know where I am making mistake? pls help

What I have tried:

Try
           Dim product, qty, price, total, desc, billno As String
           For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
               If DataGridView1.RowCount > 0 AndAlso DataGridView1.CurrentRow IsNot Nothing Then
                   'datatype integer
                   product = Convert.ToInt32(Me.DataGridView1.Rows(i).Cells(1).Value.ToString())
                   'datatype double
                   qty = Convert.ToDouble(Me.DataGridView1.Rows(i).Cells(2).Value.ToString())
                   price = Convert.ToDouble(Me.DataGridView1.Rows(i).Cells(3).Value.ToString())
                   total = Convert.ToDouble(Me.DataGridView1.Rows(i).Cells(4).Value.ToString())
                   'datatype varchar
                   desc = Me.DataGridView1.Rows(i).Cells(5).Value.ToString()
               End If
               billno = txtbillno.Text
               sqL = "INSERT INTO `dailysale`(`billno`,`customer_id`, `product_id`, `qty`, `price`, `total`, `description`) VALUES('" & billno & ",''" & txtCustomer.Tag & "', '" & product & "', '" & qty & "', '" & price & "', '" & total & "', '" & desc & "')"
           Next
           sqL &= "INSERT INTO `customer_payments`(`customer_id`, `billno`, `cdate`, `bill_amount`, `paid_amount`, `discount`, `ret_amount`, `balance`) VALUES ('" &
           txtCustomer.Tag & "','" & txtbillno.Text & "','" & DateTimePicker1.Value & "','" & Convert.ToDouble(txtbill_amount.Text) & "','" & Convert.ToDouble(txtpaid_amount.Text) & "','" & Convert.ToDouble(txtdiscount.Text) & "','" & Convert.ToDouble(txtret_amount.Text) & "','" & Convert.ToDouble(txttotalbalance.Text) & "')"
           ConnDB()
           cmd = New MySqlCommand(sqL, conn)
           cmd.ExecuteNonQuery()

           MsgBox("New Bill successfully created.", MsgBoxStyle.Information, "Create Bill")
       Catch ex As Exception
           MsgBox(ex.ToString)
       Finally
           cmd.Dispose()
           conn.Close()
       End Try

推荐答案

这是我们遇到的最常见问题之一,也是我们最不能回答的问题,但你最有能力回答呃你自己。



让我解释一下错误的含义:你试图使用变量,属性或方法返回值,但它包含null - 这意味着变量中没有类的实例。

它有点像一个口袋:你的衬衫上有一个口袋,用来握笔。如果你进入口袋并发现那里没有笔,你就不能在一张纸上签名 - 如果你尝试的话,你会得到非常有趣的外观!空口袋给你一个空值(这里没有笔!)所以你不能做任何你检索笔通常做的事情。它为什么空?这就是问题 - 可能是你今天早上离开家时忘了拿起你的笔,或者你昨晚把它拿到昨天的衬衫口袋里时可能会把笔留下来。



我们无法分辨,因为我们不在那里,更重要的是,我们甚至看不到你的衬衫,更不用说口袋里的东西了!



回到计算机,你做了同样的事情,不知何故 - 我们看不到你的代码,更不用说运行它了,找不到包含null的东西。

但是你可以 - 而Visual Studio将在这里帮助你。在调试器中运行您的程序,当它失败时,VS将向您显示它发现问题的行。然后,您可以开始查看它的各个部分,以查看哪个值为null,并开始回顾代码以找出原因。因此,在包含错误行的方法的开头放置一个断点,然后从头再次运行程序。这一次,VS会在错误发生前停止,让你通过查看代码来查看你的价值来检查发生了什么。



但我们做不到那 - 我们没有您的代码,如果我们拥有它,我们不知道如何使用它,我们没有您的数据。所以尝试一下 - 看看你能找到多少信息!
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!


不是你问题的解决方案,而是解决你遇到的另一个问题。

你永远不应该通过连接用户输入来构建SQL查询,它被命名为SQL注入,它对您的数据库很危险并且容易出错。

名称中的单引号和程序崩溃。如果像Brian O'Conner这样的用户输入可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

Not a solution to your question, but a solution to another problem you have.
You should never build an SQL query by concatenating with user inputs, it is named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability.
SQL injection - Wikipedia[^]
SQL Injection[^]
Quote:

Datagridview对象引用未设置为对象的实例

Datagridview object reference not set to an instance of an object


由于某种原因,您的代码无法创建变量中的Datagridview对象的实例。在尝试使用该变量之前,这不是问题。

使用调试器,您将看到代码的执行情况,它有助于查找问题及其原因。


当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器看看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



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

Visual Basic / Visual Studio视频教程 - 基本调试 - YouTube [ ^ ]

初学者的Visual Basic .NET编程 - 断点和调试工具 [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


For some reason, your code fail create to an instance of a Datagridview object in a variable. This is not a problem until you try to use that variable.
Use the debugger and you will see how your code perform, it helps to locate problems and their reasons.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


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

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