Thursday, March 22, 2012

Object reference not set to an instance of an object ERROR.

Hi there,

I have a DataList with multiple images in which I want to toggle on and off with a button using the visible property. However I get an error when clicking the button.

Here is my button code:


Private Sub btnImage1_Click(sender As Object, e As EventArgs)

Dim imgPropertyListing1 As Image = Ctype(Findcontrol("imgPropertyListing1"), Image)

Dim imgPropertyListing2 As Image = Ctype(Findcontrol("imgPropertyListing2"), Image)

Dim imgPropertyListing3 As Image = Ctype(Findcontrol("imgPropertyListing3"), Image)

imgPropertyListing1.Visible = True

imgPropertyListing2.Visible = False

imgPropertyListing3.Visible = False

End Sub

Here is the error:


Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 105: Dim imgPropertyListing3 As Image = Ctype(Findcontrol("imgPropertyListing3"), Image)

Line 106:

Line 107: imgPropertyListing1.Visible = True

Line 108: imgPropertyListing2.Visible = False

Line 109: imgPropertyListing3.Visible = False

Source File: K:\details\fullpropertylisting.aspx Line: 107

Any suggestions would be greatly appreciated.

Regards and thanks in advance,

TCMUse the ItemCommand Method. Set your Button Command , e.g. to 'Hide'. The ItemCommand Method is extremely versatile. You may use it for many different purposes. here is one little example. I'm sure u understand:


private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.CommandName == "newmodel")
{
TextBox tb = (TextBox)e.Item.FindControl("txtModel");

if (tb.Text.Trim() == string.Empty)
{
this.trMessage.Visible = true;
this.lblMessage.ForeColor = Color.Red;
this.lblMessage.Text = "** You cannot register an empty string **";
return;
}

int id = (int)this.DataGrid1.DataKeys[e.Item.ItemIndex];
string model = Utilities.HandleRestrictedChars(tb.Text, CharCheck.FromWeb);

int chk = SqlHelper.ExecuteNonQuery(DbConn.Con, CommandType.StoredProcedure,
"AV_InsertModel",
new SqlParameter("@.model", model),
new SqlParameter("@.id", id));

if (chk > 0)
{
this.trMessage.Visible = true;
this.lblMessage.ForeColor = Color.Green;
this.lblMessage.Text = "** The model is registered! **";
tb.Text = "";

DataGrid dg = (DataGrid)e.Item.FindControl("DataGrid2");

this.BindInnerGrid(dg, id);
}
else
{
this.trMessage.Visible = true;
this.lblMessage.ForeColor = Color.Red;
this.lblMessage.Text = "** An error occured! (nothing was registered) **";
}

}
elseif(e.CommandName == "whatever"))
{

// Do something to handle the Commandname 'WhatEver'
}
}
}


You get the point.

0 comments:

Post a Comment