当插入数据显示错误时,如下对象引用未设置为对象的实例 [英] When insert data show error as follows object reference not set to an instance of object

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

问题描述

全球功能代码如下



Global function code as follows

public class GlobalFunction
{
    public SqlConnection con = null;
    private string connectionstring = "Data Source=(local);connect timeout = 120; Initial Catalog=CRMS;Trusted_Connection=True";
    private SqlCommand cmd;
    private SqlDataReader dr;
   
	public GlobalFunction()
	{
		
	}

    public void BindConn()
    {
        con = new SqlConnection();
        con.ConnectionString = connectionstring;
        con.Open();
    }

    public void InsertData(string SQL)
    {
        try
        {
            BindConn();
            cmd = new SqlCommand(SQL, con);
            cmd.ExecuteReader();

        }
        catch (Exception e1)
        {
            Error = e1.Message.ToString();
            
        }
    }

    public string CntString()
    {
        return connectionstring;
    }
}





然后我有一个名为客户注册的屏幕。



客户注册保存按钮代码如下





Then i have one screen called customer registration.

customer registration save button code as follows

private GlobalFunction GFun = new GlobalFunction();
private string sql;

 protected void btnsave_Click(object sender, EventArgs e)
    {
   
sql = "insert into Customer_Reigsitration values('" + ddlCompanyid.SelectedItem.Text.ToString() + "','" + txtCompanyname.Text + "','" + Txtaddress1.Text + "','" + txtaddress2.Text + "','" + ddlarea.SelectedItem.Text.ToString() + "','" + Txtcity.Text + "','" + Txtstate.Text + "','" + Txttele.Text + "','" + Txtmobile.Text + "'," + Txtpincode.Text + ",'" + Txtemail.Text + "','" + Txtcontact.Text + "','A')"; 
        try
        {
            GFun.InsertData(sql);
        }
        catch (Exception ex)
        {
            lblmessage.Text = ex.ToString();
            return;
        }
}





在运行模式下,当我输入所有字段并单击保存按钮时,出错显示如下





In the run mode when i type all the fields and click the save button, error shows as follows

object reference not set to an instance of object.





此错误显示在 sqi =插入Customer_Reigsitration值;
上述行中的




请帮帮我。



我的上述代码有什么问题。



问候,

Narasiman P.



this error shows in sqi = "insert into Customer_Reigsitration values";
in the above line.

please help me.

what is the problem in my above code.

Regards,
Narasiman P.

推荐答案

当你收到如下错误:

When you get an error like:
object reference not set to an instance of object.

要做的第一件事就是在调试器中查看代码,并找出确切的部分它抱怨的代码行是 null

在你的情况下,它可以是以下任何一个:

The first thing to do is look at your code in the debugger, and find out exactly which part of the line of code it is complaining about is null
In your case, it could be any of:

ddlCompanyid
ddlCompanyid.SelectedItem
txtCompanyname
Txtaddress1
txtaddress2
ddlarea
ddlarea.SelectedItem
Txtcity
Txtstate
Txttele
Txtmobile
Txtpincode
Txtemail
Txtcontact

我们无法帮你决定哪一个。

当你知道它们中的哪一个为空时,你可以看看为什么你的代码让它变为空 - 但直到你知道,没有人可以帮助你。



BTW:不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。

And we cannot help you decide which.
When you know which one of them is null, you can look at why your code has let it become null - but until you know, nobody can help you.

BTW: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.


ddlCompanyid.SelectedItem ddlarea.SelectedItem null ,无论如何在调试器中运行代码并将鼠标移到该行上,直到看到监视工具提示并查看哪个部分是 null
Either ddlCompanyid.SelectedItem or ddlarea.SelectedItem is null, in any case run your code in the debugger and move you mouse over the line until you see the watch tooltip and see which part is null.


除了解决方案1之外,构建sql的方式不仅使您难以找到错误,而且让您对SQL注入开放攻击。



有关更多信息,请参阅此文章

http://www.dotnetperls.com/sqlparameter [ ^ ]。



如果您使用过SqlParameter,那么您会立即发现哪个对象未实例化
Further to solution 1 the way you are constructing your sql not only makes it difficult for you to find what is wrong, it leaves you open to SQL Injection attacks.

See this article for more information
http://www.dotnetperls.com/sqlparameter[^].

If you had used SqlParameter then you would have discovered immediately which object was not instantiated


这篇关于当插入数据显示错误时,如下对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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