空引用错误(对象引用未设置为对象的实例.) [英] Nullreference error (Object reference not set to an instance of an object.)

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

问题描述

大家好,我在这里遇到疯狂的问题...我有一个datagrid,我添加了两列未绑定数据的新列.我想使其工作,以便在仅具有ID和其他信息的表中显示给定名称和姓氏,以便查看者知道哪些信息属于谁.
这是有效的事件的原始代码

Hi all, im having mad problems here...i have a datagrid that i have added two new columns of unbound data. I want to make it work so that it displays given name and last name in a table that only has ID and other information so that the viewer knows what information belongs to who.
this is the original code for events which works

private void addColumnDataToEventInvitees()
        {
            // Load in custom columns for event invitees fNames and lNames
            if (dgvEvents.Rows.Count > 0 && dgvEventInvitees.Rows.Count > 0)
            {
                for (int i = 0; i < dgvEventInvitees.Rows.Count; i++)
                {
                    DataGridViewRow selectedRow = dgvEventInvitees.Rows[i];
                    int personID = Convert.ToInt16(selectedRow.Cells[0].Value);

                    DataRow findRow = data.Tables["People"].Rows.Find(personID);

                    dgvEventInvitees["GivenNames", i].Value = findRow["GivenNames"].ToString();
                    dgvEventInvitees["LastName", i].Value = findRow["LastName"].ToString();
                }
                peopleEventTableAdapter.Fill(data.PeopleEvent);
            }
        }




这是我非常相似的代码,但是用于医疗表格.我看过博客,听说有关于添加对象的




this is my very similar code but it is for the medical forms. I have looked at blogs and i hear talk of adding a

new

实例的讨论

我只是不确定如何实现这一目标...任何帮助都会很棒!
错误在这里弹出.它说:已经有一个与此命令关联的打开的数据读取器,必须先关闭"

它告诉我关闭数据读取器,但我不知道如何关闭它:(

instance of the object

I am just not sure how to implement this...Any help would be great!!
the error pops up in here. It says "there is already an open datareader associated with this command which must be closed first"

it tells me to close the datareader but i can''t figure out how to close it :(

private void personnameToDataGrid()
        {
            if (dgvMedical.Rows.Count > 0)
            {
                for (int i = 0; i < dgvMedical.Rows.Count; i++)
                {
                    DataGridViewRow selectedRow = dgvMedical.Rows[i];
                    int personID = Convert.ToInt16(selectedRow.Cells[0].Value);

                    DataRow findRow = data.Tables["People"].Rows.Find(personID);

                    dgvMedical["MDGivenNames", i].Value = findRow["GivenNames"].ToString();
                    dgvMedical["MDLastName", i].Value = findRow["LastName"].ToString(); //my error was appearing on this //line
                }
                medicalTableAdapter.Fill(data.Medical);

            }
        }

推荐答案

为解决您的错误,请执行此操作...


1)在这里放置行的断点...

For your solution of your error please do this...


1) put a break point of your line at this...

DataRow findRow = data.Tables["People"].Rows.Find(personID);



并按F10键,然后浏览您的findRow对象是否包含某些东西...

然后在用户即时解决方案窗口中将您的代码行粘贴到该行中
检查一下...



and press F10 then browse your findRow object if it will contain some thing or not...

then user immediate solution window and paste your line of code inside that
check this...

findRow["LastName"].ToString()


它会给你什么.....

如果它将为null,那么您在创建数据行时的代码有错误,请在此处进行更正.....


what it will give you.....

if it will give null then your code at datarow creation have something wrong and correct it there.....


谢谢Tejas!
问题是

Thank you Tejas!
The issue was that

int personID = Convert.ToInt16(selectedRow.Cells[0].Value);



在第一列中寻找东西.要做的是查看ID所在的数字列.因此,如果id在第三列中,则选定的行单元格值将为3


全部修复



was looking for stuff in the first column. What it was meant to be doing is looking in the number column where the ID was. so if id was in the third column then the selected row cells value would be 3


All fixed now


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

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