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

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

问题描述

你好

我在此代码中出现错误

Hello

I am getting error in this code

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lblID = (Label)row.FindControl("lblID");
            
            TextBox textName = (TextBox)row.FindControl("txtName");
            TextBox textDesc = (TextBox)row.FindControl("txtDesc");
            TextBox textImage = (TextBox)row.FindControl("txtImage");
            TextBox textActive = (TextBox)row.FindControl("txtActive");
            TextBox textCreatedBy = (TextBox)row.FindControl("txtCreatedBy");
            TextBox textCreatedDate = (TextBox)row.FindControl("txtCreatedDate");
            GridView1.EditIndex = -1;
            conn.Open();
            
            
            SqlCommand cmd = new SqlCommand("UPDATE tbl_SqlImage set Name=" + textName.Text + " , Description='" + textDesc.Text + " , Image='" + textImage.Text + " , Active='" + textActive.Text + " , CreatedBy='" + textCreatedBy.Text + " , CreatedDate='" +textCreatedDate.Text + "' where ID=" + lblID.Text + "", conn);
            
            
            cmd.ExecuteNonQuery();
            conn.Close();
            bind();
            //GridView1.DataBind();
            
        }


请帮助
ID是自动生成的

我的ID为空
请帮助


Please Help
ID is auto generating

I am getting null in ID
Please Help

推荐答案

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

当您尝试使用属性或调用对象的方法为null时,会发生此错误.更多详细信息:此处 [
Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.

Here, there are lots of places starting from line 1 itself! any of the explicit cast might have raised it.


您正试图访问一个为null的对象的属性.
更多详细信息:
单击此处 [
You are trying to access an attribute of an object that is null.
More details: Click here[^]

A simple use of Visual studio DEBUGGER can tell you the cause.

Just look at the stack trace and put a debugger on that line where you''re getting this error.

This will easily help you identify and solve such type of errors.


SqlCommand cmd = new SqlCommand("UPDATE tbl_SqlImage set Name='" + textName.Text + "' , Description='" + textDesc.Text + "' , Image='" + textImage.Text + "' , Active='" + textActive.Text + "' , CreatedBy='" + textCreatedBy.Text + "' , CreatedDate='" +textCreatedDate.Text + "' where ID='" + lblID.Text.Trim() + "' ", conn);


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

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