Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Templatefields in gridview, unable to edit or add records 1

Status
Not open for further replies.

DCSage

Programmer
Mar 5, 2010
74
US
I have a gridview in which I want my client to be able to edit and add information. I am getting an error on updates as well as adding the records.

I am getting the following error for edit:

Code:
There was an error updating the information for record
224
System.NullReferenceException: Object reference not set to an instance of an object. at InventoryGrid.showVal(Object sender, GridViewCommandEventArgs e) in c:\Inetpub\[URL unfurl="true"]wwwroot\BETInventoryTest\InventoryGrid.aspx.cs:line[/URL] 266

I am getting the id, but not the control, or the information from the control. The controls are templatefields, how can I pull the value from the template fields?

I have the following (my delete statement is fine:

Code:
protected void showVal(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteRow")
        {
            string index = e.CommandArgument.ToString();

            lblNotify.Text = "You have selected Record" + index + "to delete.  This is a permanent action.";
            int rowtoDelete = Convert.ToInt32(index);

            //begin process to delete

            try
            {
                //delete inventory based on stored procedure             
                lblNotify.Text = "You are about to Delete " + rowtoDelete + ".";

                string dc = ConfigurationManager.ConnectionStrings["SiteDB"].ConnectionString;
                SqlConnection dconn = new SqlConnection(dc);
                SqlCommand dcomm = new SqlCommand("testDelete", dconn);

                dcomm.CommandType = CommandType.StoredProcedure;
                dcomm.Parameters.AddWithValue("@RemoveID", rowtoDelete);

                dcomm.Connection.Open();
                dcomm.ExecuteNonQuery();

                lblNotify.Text = "You selected successfully deleted record " + rowtoDelete;

                dcomm.Connection.Close();

                iGrid.EditIndex = -1;
                iGrid.DataBind();
            }
            catch (Exception deleteError)
            {
                lblNotify.Text = "Error on deleting record" + deleteError;
            }
        }

        else if (e.CommandName == "editRec")
        {

            string eindex = e.CommandArgument.ToString();
            int rowtoEdit = Convert.ToInt32(eindex);

            lblNotify.Text = "You have selected Record" + eindex +  " to edit.";           

            //update Inventory
            try
            {
                int intbuilding;
                int intCubicle;
                int intOffice;
                int intCafe;
                int intConferencerm;
                int intLobby;
                int intFloor;
                int intDepart;
                int intEquipment;
                int intEquipType;
                string strModel;
                string strSerial;
                string strCubicleN;
                string strOfficeN;
                int locationtypes;
                string roomnum;
                string comments;

                string otherLocation;

                //use the following to test;

                foreach (GridViewRow row in iGrid.Rows)
                {
                    DropDownList buil = (DropDownList)row.FindControl("drpBuilding");

                    lblNotify.Text = buil.SelectedValue;
                }

//original
//DropDownList ubldg = (DropDownList)iGrid.FindControl("drpBuilding");
 
the error begins within the edit section:

Code:
 else if (e.CommandName == "editRec")
string eindex = e.CommandArgument.ToString();
            int rowtoEdit = Convert.ToInt32(eindex);

            lblNotify.Text = "You have selected Record" + eindex +  " to edit.";           


foreach (GridViewRow row in iGrid.Rows)
                {
                    DropDownList buil = (DropDownList)row.FindControl("drpBuilding");

                    lblNotify.Text = buil.SelectedValue;
                }
 
You don't seem to understand me. Trace through your code and find the exact line that throws the error.
 
the exception stack trace is a great place to start. it tells you exactly where the problem is, and what the problem is.
on line 266 you are referencing a null object.

I would guess this is causing the error.
Code:
foreach (GridViewRow row in iGrid.Rows)
{
   DropDownList buil = (DropDownList)row.FindControl("drpBuilding");
   lblNotify.Text = buil.SelectedValue;
}
it's not entirely clear why you would set the label value this way... in any case ensure buil is not null before continuing.
Code:
foreach (GridViewRow row in iGrid.Rows)
{
   DropDownList buil = row.FindControl("drpBuilding") as DropDownList;
   if(buil == null) continue;
   lblNotify.Text = buil.SelectedValue;
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thank you! I was definitely referencing a null value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top