对象引用未设置为对象C#的实例变量 [英] Object reference not set to an instance variable of an object C#

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

问题描述

我不知道这有什么问题。



当我尝试运行代码时,它运行顺畅。但是,当我第二次尝试时,它告诉我对象引用未设置为对象的实例变量,第105行是imgID.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);



这是我的代码。



我的尝试:



I don't know what is wrong with this.

When I try to run the codes, it works smooth. But when I try for the second time it tells me "Object reference not set to an instance variable of an object" in line 105 which is "imgID.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);"

Here's my code.

What I have tried:

private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtLRN.Text == "" && txtFirst.Text == "" && txtMid.Text == "" &&
                    txtLast.Text == "" && cbGL.Text == "" && txtSec.Text == ""
                    && txtNo.Text == "" && txtAdd.Text == "" && imgID.Image == null)
                {
                    enabled();
                }
                else
                {
                    con.Open();

                    cmd = new SqlCommand("insert into tblBorrower (LRN, FirstName, MiddleName, LastName, GradeLevel, Section, ContactNo, Address, IDPhoto) values (@lrn, @fn, @mn, @ln, @gl, @sec, @cn, @add, @id)", con);

                    MemoryStream stream = new MemoryStream();
                    imgID.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    byte[] pic = stream.ToArray();

                    cmd.Parameters.AddWithValue("@lrn", txtLRN.Text);
                    cmd.Parameters.AddWithValue("@fn", txtFirst.Text);
                    cmd.Parameters.AddWithValue("@mn", txtMid.Text);
                    cmd.Parameters.AddWithValue("@ln", txtLast.Text);
                    cmd.Parameters.AddWithValue("@gl", cbGL.Text);
                    cmd.Parameters.AddWithValue("@sec", txtSec.Text);
                    cmd.Parameters.AddWithValue("@cn", txtNo.Text);
                    cmd.Parameters.AddWithValue("@add", txtAdd.Text);
                    cmd.Parameters.AddWithValue("@id", pic);

                    cmd.ExecuteNonQuery();

                    con.Close();

                    MessageBox.Show("Record Inserted Succesfully!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    load();
                    clearTxts();
                    disabled();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }

推荐答案





由于PictureImage控件(ieimgID)没有任何图片你得到的错误。



你需要选择/为imgID contrl分配图像,之后你不会得到对象引用错误。



请再次检查。



问候,

Sarita Singh
Hi,

the error you are getting due to PictureImage control (i.e.imgID ) does not have any picture.

you need to choose/assign image for imgID contrl ,after that you will not get the "object reference error".

please check again.

Regards,
Sarita Singh


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

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