当我执行我的代码时,如何解决对象引用未设置为对象的实例? [英] How To Solve Object Reference Not Set To An Instance Of An Object When I Execute My Code?

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

问题描述

这里我的代码是

here my code is

protected void btndelete_Click(object sender, EventArgs e)
    {
        try
        {
            cmd = new SqlCommand("Delete Employee1  Where EmployeeId=@PEmployeeId", con);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@PEmployeeId", TextBox1.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            lblmessage.Text = "Record deleted";
        }
        catch (NullReferenceException ex)
        {
            lblmessage.Text = ex.Message;
        }
what is this error why i'am getting this and how to solve this please help me thanks.

推荐答案

通常,您会尝试使用其名称从Web.Config读取连接字符串:



Normally, you would try to read a connection string from Web.Config by using its name:

<connectionStrings>
  <add name="LoggingDatabase" connectionString="Database=XXX;Data Source=XXX;Initial Catalog=XXX;Integrated Security=True"/>







您似乎试图通过使用字符串本身作为名称来检索连接字符串...这就是您获得的原因错误,它不存在所以它返回null。




You appear to be trying to retrieve a connection string by using the string itself as the name...which is why you get the error, it doesn't exist so it returns null.


确保 con 对象不为null。

在使用此事件之前将 SqlConnection 初始化为 con 对象
Make sure that con object is not null.
Initialize the SqlConnection to the con object before using this event


可能是您的数据库连接未正确建立...
May be your database Connection is not established properly...


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

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