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();
}
}
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();
ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Inventory added Successfully');", true);
}
else
{
}
}
}
private void LoadInventoryData()
{
SqlConnection con = new SqlConnection(_connString);
SqlCommand cmd = new SqlCommand("Select * from Equipment_Inventory", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
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