object无法从dbnull转换为其他类型。 C# [英] object cannot be cast from dbnull to other types. c#

查看:58
本文介绍了object无法从dbnull转换为其他类型。 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void dgv_Add_job_card_DoubleClick(object sender, EventArgs e)
       {
           txt_Items_No.Text = this.dgv_Add_job_card.CurrentRow.Cells[0].Value.ToString();
           txt_Job_card.Text = this.dgv_Add_job_card.CurrentRow.Cells[1].Value.ToString();
           txt_Value_Part.Text = this.dgv_Add_job_card.CurrentRow.Cells[2].Value.ToString();
           CB_Auto_parts.Text = this.dgv_Add_job_card.CurrentRow.Cells[3].Value.ToString();
           if (Convert.ToInt32(this.dgv_Add_job_card.CurrentRow.Cells[4].Value) == 0)
           {

               radioButton_New.Checked = true;

           }
           else
           {

               radioButton_old.Checked = true;
           }
           CB_Providers_desc.Text = this.dgv_Add_job_card.CurrentRow.Cells[5].Value.ToString();
           CB_Hand_installation.Text = this.dgv_Add_job_card.CurrentRow.Cells[6].Value.ToString();
           txt_Notes.Text = this.dgv_Add_job_card.CurrentRow.Cells[7].Value.ToString();
           CB_Repair_type.Text = this.dgv_Add_job_card.CurrentRow.Cells[10].Value.ToString();
           dgv_Add_job_card.Rows.RemoveAt(dgv_Add_job_card.CurrentRow.Index);
           txt_Value_Part.Focus();
       }

推荐答案

确实,它不能。



我怀疑你得到的原因是:

Convert.ToInt32(this.dgv_Add_job_card.CurrentRow.Cells [4] .Value)



首先,请勿使用 Convert.ToInt32



接下来,试试这个(如果值是整数):

Indeed, it can't.

I suspect you're getting that due to:
Convert.ToInt32(this.dgv_Add_job_card.CurrentRow.Cells[4].Value)

First off, do NOT use Convert.ToInt32 .

Next, try this instead (if the value is an integer):
if ((this.dgv_Add_job_card.CurrentRow.Cells[4].Value is int) && ((int)this.dgv_Add_job_card.CurrentRow.Cells[4].Value == 0))





或者这个(如果值是位/布尔值):



Or this (if the value is a bit/boolean):

if ((this.dgv_Add_job_card.CurrentRow.Cells[4].Value is bool) && (bool)this.dgv_Add_job_card.CurrentRow.Cells[4].Value))







此外,如果其他值是字符串,请执行不要在它们上使用ToString;只是施放它们。




Furthermore, if the other values are strings, do NOT use ToString on them; just cast them.


这里:

Here:
if (Convert.ToInt32(this.dgv_Add_job_card.CurrentRow.Cells[4].Value) == 0)



..该单元格保存一个DBNull值(确切地说是DBNull的单个实例),它可能来自从您的数据库中读取。它不能转换或转换为任何东西(除了Object)。在尝试检查等于0之前,您必须检查它。可能是这样的:


..that cell holds a DBNull-value (the single instance of DBNull to be exact) which probably originates from reading from your database. It can't be converted or cast to anything (other than Object). You have to check for it before attempting to check for being equal to 0. Probably like this:

if (this.dgv_Add_job_card.CurrentRow.Cells[4].Value == DBNull.Value || (int)this.dgv_Add_job_card.CurrentRow.Cells[4].Value == 0)



(可能因为我不知道你的意图在这里。)



如果不是<,则用​​该列的实际类型替换强制转换(int) code> INT 。你知道列类型,如果你可以只使用它就不需要使用Convert.ToXXX。



如果你不希望它能够成为DBNull 但只是0而不是你必须将你的数据库表列更改为非可空,并且在插入该表时,插入0而不是null。


("Probably" because I don't know your intention here.)

Replace the cast (int) by the actual type of that column if it's not int. You know the column types, no need to use Convert.ToXXX if you can just cast it.

If you don't want it to "be able to be DBNull" but just 0 instead you have to change your database-table-column to be non-nullable and when inserting into that table, insert 0 instead of null.


这篇关于object无法从dbnull转换为其他类型。 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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