用于在删除按钮上获取实例对象错误的对象引用 [英] Object reference to get an instance an object error on delete button

查看:88
本文介绍了用于在删除按钮上获取实例对象错误的对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,伙计们,我希望你会没事的。我在我的项目中面临另一个问题,现在就是我删除了值,所以我得到一个错误我正在使用数据网格视图和删除所选多行的复选框所有选定的行,但是当我点击按钮时有时会删除,但大多数情况下我会收到错误。



我尝试了什么: < br $>


  foreach (DataGridViewRow item  in   this  .dataGridView1.Rows)
{
if bool .Parse(item.Cells [ 1 ]。Value.ToString()))
{
con_string.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = con_string;
command.CommandText = 从[运行时]删除其中[值] = +项目.Cells [ 0 ]。Value.ToString()+ ;
command.ExecuteNonQuery();
con_string.Close();
}
}

解决方案

一个问题是您将值直接连接到SQL语句。这会引入转换错误,让您打开SQL注入等等。



修复方法是使用 OleDbParameter 对象。虽然文章使用的是SqlParamater,但想法是一样的,所以看看正确执行数据库操作 [ ^ ]

Hello, Guys, I hope you will be fine.I am facing another issue in my project and now that is when I delete values so I get an error I am using a checkbox for selected multiple rows by data grid view and delete all selected rows but when I click the button so sometimes it delete but mostly I will get an error.

What I have tried:

foreach (DataGridViewRow item in this.dataGridView1.Rows)
            {
                if (bool.Parse(item.Cells[1].Value.ToString()))
                {
                    con_string.Open();
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = con_string;
                    command.CommandText = "Delete from [Runtime] Where [Values] = " + item.Cells[0].Value.ToString() + "";
                    command.ExecuteNonQuery();
                    con_string.Close();
                }
            }

解决方案

One problem is that you concatenate the values directly to the SQL statement. This introduces conversion errors, leaves you open to SQL injections and so on.

The fix would be to use OleDbParameter objects. While the article is using SqlParamater, the idea is the same so have a look at Properly executing database operations[^]


这篇关于用于在删除按钮上获取实例对象错误的对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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