I have several buttons in my web app that I want to use to add/remove from one box to another. Using the following code which has worked in the past I am receiving a null reference error
The error points to the -1; in the line for (int i = lbAppsI.Items.Count - 1; i >= 0; i--) below
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = lbAppsI.Items.Count - 1; i >= 0; i--)
{
if (lbAppsI.Items[i].Selected == true)
{
lbImpactedApps.Items.Add(new ListItem(lbAppsI.Items[i].Text, lbAppsI.Items
[i].Value + ","));
}
}
}
Object reference not set to an instance of an object is the error thrown.
entire code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Configuration;
namespace LandscapeServices
{
public partial class Update : System.Web.UI.Page
{
public string query { get; set; }
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LandscapeServicesConnectionString"].ConnectionString);
DataSet dt = new DataSet();
string[] IApps;
string[] SApps;
ListBox lbImpactedApps;
ListBox lbSupportingApps;
ListBox lbAppsI;
ListBox lbAppsS;
TextBox txtPrj;
TextBox txtArt;
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
string ImpactedApp = ((System.Web.UI.WebControls.Label)(((System.Web.UI.WebControls.GridView)(sender)).Rows[e.NewEditIndex].FindControl("Label10"))).Text;
IApps = ImpactedApp.Split(',');
string SupportingApp = ((System.Web.UI.WebControls.Label)(((System.Web.UI.WebControls.GridView)(sender)).Rows[e.NewEditIndex].FindControl("Label11"))).Text;
SApps = SupportingApp.Split(',');
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
lbImpactedApps = (ListBox)e.Row.FindControl("ListBox1");
lbSupportingApps = (ListBox)e.Row.FindControl("ListBox2");
lbAppsI = (ListBox)e.Row.FindControl("ListBox3");
lbAppsS = (ListBox)e.Row.FindControl("ListBox4");
txtPrj = (TextBox)e.Row.FindControl("txtProjectName");
foreach (string lst in IApps)
{
lbImpactedApps.Items.Add(lst.ToString());
//lbImpactedApps.Items.FindByText(lst.ToString()).Selected = true;
}
lbImpactedApps.DataBind();
foreach (string lst in SApps)
{
lbSupportingApps.Items.Add(lst.ToString());
//lbSupportingApps.Items.FindByText(lst.ToString()).Selected = true;
}
lbSupportingApps.DataBind();
}
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = lbAppsI.Items.Count - 1; i >= 0; i--)
{
if (lbAppsI.Items[i].Selected == true)
{
lbImpactedApps.Items.Add(new ListItem(lbAppsI.Items[i].Text, lbAppsI.Items[i].Value + ","));
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
}
protected void Button4_Click(object sender, EventArgs e)
{
}
}
}