I’m trying to pass a value from DetailsView to FormView but I always get an error: Object reference is not set to an instance of an object. Somehow it did pass a value and I could still perform an insert. Its my first time programming in C#. Any hep would be much appreciated!
Thanks in advance
Here I’ve attached a code behind. I think the prob here is that I dont have a null check so how can I add a null check?
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class BookLending : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void FormView1_DataBound(object sender, EventArgs e)
{
if(FormView1.CurrentMode == FormViewMode.Insert && FormView1.Fin != null)
{
TextBox bookid = FormView1.FindControl("bookidTextBox") as TextBox;
bookid.Text = ((DataRowView)DetailsView1.DataItem)["bookid"].ToString();
}
{
TextBox employee = FormView1.FindControl("EmployeeIDTextBox") as TextBox;
employee.Text = ((DataRowView)DetailsView1.DataItem)["EmployeeID"].ToString();
}
}
}
}
There are a few places you can check for null.
1) If your cast is incorrect:
2) If the controls don’t have values,
ToString()will also throw the object null reference exception:In which line of your code is the exception thrown?