在查询中遇到此错误的问题 [英] facing problem with this error in query

查看:68
本文介绍了在查询中遇到此错误的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家!



我遇到此错误的问题,请有人解决此问题。



这是错误



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



- -------------------------------------------------- ----------



这是我的代码

 使用系统; 
使用 System.Configuration;
使用 System.Data;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;
使用 System.Data.SqlClient;
使用 System.Web.Configuration;
使用 System.Drawing;

public partial class InventoryHome:System.Web.UI.Page
{
private static readonly string _connString = String .Empty ;
SqlConnection con = new SqlConnection(_connString);

受保护 void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{
LoadInventoryData() ;

}
}


// 使用FOOTER添加记录
protected void GridView1_RowCommand( object sender,GridViewCommandEventArgs e)
{
if (e.CommandName.Equals( AddNew))
{
DropDownList ddlEquipmenttype =(DropDownList)GridView1.FooterRow.FindControl ( ddlftrEquipment_type);
DropDownList ddlMaker =(DropDownList)GridView1.FooterRow.FindControl( ddlftrMaker);
TextBox txtModel =(TextBox)GridView1.FooterRow.FindControl( txtftrModel);
TextBox txtSerialno =(TextBox)GridView1.FooterRow.FindControl( txtftrSerialno);
TextBox txtKKIATagNo =(TextBox)GridView1.FooterRow.FindControl( txtftrKKIATagNo);
TextBox txtGACATagNo =(TextBox)GridView1.FooterRow.FindControl( GACATagNo);
TextBox txtAssignedto =(TextBox)GridView1.FooterRow.FindControl( txtftrAssignedto);
TextBox txtLocation =(TextBox)GridView1.FooterRow.FindControl( txtftrLocation);


con.Open();


SqlCommand cmd = new SqlCommand( 插入到Equipment_Inventory(Equipment_type,Maker,Model,Serialno,KKIATagNo,GACATagNo,Assignedto,Location)值(' + ddlEquipmenttype.SelectedItem.Text + ',' + ddlMaker.SelectedItem.Text + ',' + txtModel.Text + ',' + txtSerialno.Text + ',' + txtKKIATagNo.Text + ',' + txtGACATagNo.Text + < span class =code-string>',' + txtAssignedto.Text + ',' + txtLocation.Text + '),con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1
{
LoadInventoryData( );
/// // lblresult.ForeColor = Color.Green; ///////// /
/// // lblresult.Text = txtUsrname.Text +详细信息已成功插入; /////////
ScriptManager.RegisterStartupScript( this this .GetType(), RunCode javascript:alert('库存已成功添加'); true );
}
其他
{
/// // lblresult.ForeColor = Color.Red; ///////////
/ / lblresult.Text = txtFlightNo.Text +未插入详细信息; ////////////
}


}

}


// LOAD INVENTORY DATA
private void LoadInventoryData()
{
SqlConnection con = new SqlConnection(_connString);


SqlCommand cmd = new SqlCommand( 从Equipment_Inventory中选择* ,con);
// SqlCommand cmd = new SqlCommand(选择Dept_code为DeptCode,Dept_desc为Description,Dept_telno为TelephoneNo, Dept_dirmgr为DirectorManager,Equipment_type为EquipmentType,Maker为Maker,Model为SerialNo,TagNo为TagNo,Assignedto为AssignedTo,Location as Location from Equipment_Inventory,con);
SqlDataAdapter da = < span class =code-keyword> new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();


}


// 提交清单
受保护 void BtnSubmitInventory_Click(对象发​​件人,EventArgs e)
{
SqlConnection con = new SqlConnection(_connString);
con.Open();
SqlDataAdapter da = new SqlDataAdapter( 选择*来自Equipment_Inventory,con);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
DataSet ds = new DataSet( Equipment_Inventory );
da.Fill(ds, Equipment_Inventory);

DataTable Equipment_InventoryTable = ds.Tables [ Equipment_Inventory];
DataRow row = Equipment_InventoryTable.NewRow();

row [ Dept_code] = TxtDepartmentCode.Text;
row [ Dept_desc] = TxtDescription.Text;
row [ Dept_telno] = TxtTelephoneNo.Text;
row [ Dept_dirmgr] = TxtDirectorManager.Text;

row [ Dentry] = DateTime.Now.ToString() ;



Equipment_InventoryTable.Rows.Add(row);
da.Update(ds, Equipment_Inventory);


con.Close();


TxtDepartmentCode.Text = ;
TxtDescription.Text = ;
TxtTelephoneNo.Text = ;
TxtDirectorManager.Text = ;

ScriptManager.RegisterStartupScript( this this .GetType(), RunCode javascript:alert('库存已成功添加'); true );

}


静态 InventoryHome()
{
_connString = WebConfigurationManager.ConnectionStrings [ Inventory_DBConnectionString]。ConnectionString;
}


}





此行命令中的FACING问题查询

 SqlCommand cmd =  new  SqlCommand( 插入到Equipment_Inventory(Equipment_type,Maker,Model,Serialno,KKIATagNo,GACATagNo,Assignedto,Location)值(' + ddlEquipmenttype.SelectedItem.Text +  ',' + ddlMaker.SelectedItem.Text +  ',' + txtModel.Text +  ', ' + txtSerialno.Text +  ',' + txtKKIATagNo.Text + < span class =code-string> ',' + txtGACATagNo.Text +  ',' + txtAssignedto.Text +  ',' + txtLocation.Text +  '),con); 



错误在查询中显示为

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



请帮助谢谢

解决方案

请检查代码的这些行: -



 DropDownList ddlEquipmenttype =(DropDownList)GridView1.FooterRow.FindControl(  ddlftrEquipment_type); 
DropDownList ddlMaker =(DropDownList)GridView1.FooterRow.FindControl( ddlftrMaker);
TextBox txtModel =(TextBox)GridView1.FooterRow.FindControl( txtftrModel);
TextBox txtSerialno =(TextBox)GridView1.FooterRow.FindControl( txtftrSerialno);
TextBox txtKKIATagNo =(TextBox)GridView1.FooterRow.FindControl( txtftrKKIATagNo);
TextBox txtGACATagNo =(TextBox)GridView1.FooterRow.FindControl( GACATagNo);
TextBox txtAssignedto =(TextBox)GridView1.FooterRow.FindControl( txtftrAssignedto);
TextBox txtLocation =(TextBox)GridView1.FooterRow.FindControl( txtftrLocation);





检查您是否正确找到所有控件。检查您正在查找的控件的ID。可能是这些控制中的任何一个都找不到。





祝你好运。


你没有显示带有对象引用未设置为对象实例消息的异常位置。



不用担心。这是检测和修复的最简单的案例之一。它只是意味着某些引用类型的某个成员/变量通过使用和它的实例(非静态)成员解除引用,这要求此成员/变量为非null,但实际上它似乎为null。只需在调试器下执行它,它将停止抛出异常的执行。在该行上设置一个断点,重新启动应用程序并再次到达这一点。评估下一行中涉及的所有引用,并查看哪一个为null,而不需要为null。解决这个问题之后,修复代码:要么确保将成员/变量正确初始化为非空引用,要么将其检查为null,如果为null,则执行其他操作。



另请参阅:想要在按钮点击时显示下一条记录。但是如果下一条记录功能的条件对象引用没有设置为对象的实例 [ ^ ]。



祝你好运,

-SA

Dear Experts !

am facing problem with this error, please can anybody solve this.

This is the error

Object reference not set to an instance of an object.

--------------------------------------------------------------

this is my code

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Drawing;

public partial class InventoryHome : System.Web.UI.Page
{
    private static readonly string _connString = String.Empty;
    SqlConnection con = new SqlConnection(_connString);

    protected void Page_Load(object sender, EventArgs e)
    {
         if (!IsPostBack)
        {
            LoadInventoryData();

        }
    }


    // ADD RECORD USING FOOTER
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            DropDownList ddlEquipmenttype = (DropDownList)GridView1.FooterRow.FindControl("ddlftrEquipment_type");
            DropDownList ddlMaker = (DropDownList)GridView1.FooterRow.FindControl("ddlftrMaker");
            TextBox txtModel = (TextBox)GridView1.FooterRow.FindControl("txtftrModel");
            TextBox txtSerialno = (TextBox)GridView1.FooterRow.FindControl("txtftrSerialno");
            TextBox txtKKIATagNo = (TextBox)GridView1.FooterRow.FindControl("txtftrKKIATagNo");
            TextBox txtGACATagNo = (TextBox)GridView1.FooterRow.FindControl("GACATagNo");
            TextBox txtAssignedto = (TextBox)GridView1.FooterRow.FindControl("txtftrAssignedto");
            TextBox txtLocation = (TextBox)GridView1.FooterRow.FindControl("txtftrLocation");


            con.Open();


            SqlCommand cmd = new SqlCommand("Insert into Equipment_Inventory (Equipment_type,Maker,Model,Serialno,KKIATagNo,GACATagNo,Assignedto,Location) Values ('" + ddlEquipmenttype.SelectedItem.Text + "','" + ddlMaker.SelectedItem.Text + "','" + txtModel.Text + "', '" + txtSerialno.Text + "','" + txtKKIATagNo.Text + "','" + txtGACATagNo.Text + "','" + txtAssignedto.Text + "','" + txtLocation.Text + "')", con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                LoadInventoryData();
                /////lblresult.ForeColor = Color.Green;//////////
                /////lblresult.Text = txtUsrname.Text + " Details inserted successfully";/////////
                ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Inventory added Successfully');", true);
            }
            else
            {
                /////lblresult.ForeColor = Color.Red;///////////
                // lblresult.Text = txtFlightNo.Text + " Details not inserted";////////////
            }


        }

    }


    // LOAD INVENTORY DATA
    private void LoadInventoryData()
    {
        SqlConnection con = new SqlConnection(_connString);


        SqlCommand cmd = new SqlCommand("Select * from Equipment_Inventory", con);
        //SqlCommand cmd = new SqlCommand("Select Dept_code as DeptCode, Dept_desc as Description, Dept_telno as TelephoneNo, Dept_dirmgr as DirectorManager, Equipment_type as EquipmentType, Maker as Maker, Model as Model, Serialno as SerialNo, TagNo as TagNo, Assignedto as AssignedTo, Location as Location from Equipment_Inventory", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();


    }


    // SUBMIT INVENTORY
    protected void BtnSubmitInventory_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(_connString);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("Select * from Equipment_Inventory", con);
        SqlCommandBuilder builder = new SqlCommandBuilder(da);
        DataSet ds = new DataSet("Equipment_Inventory");
        da.Fill(ds, "Equipment_Inventory");

        DataTable Equipment_InventoryTable = ds.Tables["Equipment_Inventory"];
        DataRow row = Equipment_InventoryTable.NewRow();

        row["Dept_code"] = TxtDepartmentCode.Text;
        row["Dept_desc"] = TxtDescription.Text;
        row["Dept_telno"] = TxtTelephoneNo.Text;
        row["Dept_dirmgr"] = TxtDirectorManager.Text;

        row["Dentry"] = DateTime.Now.ToString();


       
        Equipment_InventoryTable.Rows.Add(row);
        da.Update(ds, "Equipment_Inventory");

        
        con.Close();

       
        TxtDepartmentCode.Text = "";
        TxtDescription.Text = "";
        TxtTelephoneNo.Text = "";
        TxtDirectorManager.Text = "";

        ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Inventory added Successfully');", true);

    }


    static InventoryHome()
    {
        _connString = WebConfigurationManager.ConnectionStrings["Inventory_DBConnectionString"].ConnectionString;
    }

   
}



AM FACING problem in this row command Query

SqlCommand cmd = new SqlCommand("Insert into Equipment_Inventory (Equipment_type,Maker,Model,Serialno,KKIATagNo,GACATagNo,Assignedto,Location) Values ('" + ddlEquipmenttype.SelectedItem.Text + "','" + ddlMaker.SelectedItem.Text + "','" + txtModel.Text + "', '" + txtSerialno.Text + "','" + txtKKIATagNo.Text + "','" + txtGACATagNo.Text + "','" + txtAssignedto.Text + "','" + txtLocation.Text + "')", con);


error is showing in query as
Object reference not set to an instance of an object.

Please help thanks

解决方案

Please Check these lines of your codes :-

DropDownList ddlEquipmenttype = (DropDownList)GridView1.FooterRow.FindControl("ddlftrEquipment_type");
            DropDownList ddlMaker = (DropDownList)GridView1.FooterRow.FindControl("ddlftrMaker");
            TextBox txtModel = (TextBox)GridView1.FooterRow.FindControl("txtftrModel");
            TextBox txtSerialno = (TextBox)GridView1.FooterRow.FindControl("txtftrSerialno");
            TextBox txtKKIATagNo = (TextBox)GridView1.FooterRow.FindControl("txtftrKKIATagNo");
            TextBox txtGACATagNo = (TextBox)GridView1.FooterRow.FindControl("GACATagNo");
            TextBox txtAssignedto = (TextBox)GridView1.FooterRow.FindControl("txtftrAssignedto");
            TextBox txtLocation = (TextBox)GridView1.FooterRow.FindControl("txtftrLocation");



Check whether you are finding all controls properly. check the Id of your control which you are finding. May be any of these control is not getting found.


Good luck.


You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Good luck,
—SA


这篇关于在查询中遇到此错误的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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