Click here to Skip to main content
15,942,718 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
C#
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
C#
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
Posted
Updated 14-Apr-13 21:58pm
v2
Comments
Software Engineer 892 15-Apr-13 3:57am    
am inserting this data using gridview footer add button
Pheonyx 15-Apr-13 4:01am    
When you step through and get to this line, can you check that all of your objects are populated?
e.g. 'ddlEquipmenttype.SelectedItem', 'ddlMaker.SelectedItem' and 'con'
Dont forget to check all your text boxes as well.

Also, I would recommend restructuring your command you use parameters.
Software Engineer 892 15-Apr-13 4:03am    
please can you show me how to do that, am trying a lot. please help thanks
Software Engineer 892 15-Apr-13 4:12am    
please help me frnds. thanks in advance.
Pheonyx 15-Apr-13 4:24am    
Now you have solution to the problem, I would also suggest reading this link:

http://csharp-station.com/Tutorial/AdoDotNet/Lesson06

It is an explanation of how to use parameters in an SQL query instead of the approach you have used. It is much safer!

Please Check these lines of your codes :-

C#
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.
 
Share this answer
 
v2
Comments
Software Engineer 892 15-Apr-13 4:19am    
Thanks boss, i got it, the error was in line 6...which u have posted.
Now its working fine.

Thank u very much Raje...
Raje_ 15-Apr-13 4:25am    
Welcome!
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900