0

I'm having issues with a piece of code for a website in C# and when I debug it, it opens the site (localhost in Chrome. Every page works except the one I'm debugging, regardless of whether or not that's the page I'm debugging. Whenever I try to open it, it gives me an Object Reference error at the line I've marked below. This happens no matter which browser I use.

    Protected void page_load (object sender, EventArgs e)
    {
DateTime dteNow = SI.Getsource.DAL.Time.Now.Eastern();

If(!IsPostBack)
{
txtStartDate.SelectDate = dteNow.AddDays(-2)
txtEndDate.SelectedDate = dtenow.AddDays(1)

txtStartDate.Visible = False
txtEndDate.Visible = False

Shipping.DataTable.dt = SI.Getsource.DAL.EquipmentDB.getPrinter();


ddlPrinter.DataSource = dt.Result
ddlPrinter.DataTextField = dt.Result.Columns["Name"] //Error is here
ddlPrinter.DataValueField = dt.Result.Columns["PrinterID"]
ddlPrinter.DataBind();

Try
{
ddlPrinter.SelectedValue = System.Web.Configuration.WebConfigurationManager.AppSettings["DefaultPrinterID"]
}
Catch
}

}

I've tried running it in IE and Firefox. I also tried adding a try-catch for "Name" similar to the one for "Printer" but that didn't do anything. Any suggestions would be greatly appreciated, since this error prevents me from debugging the actual issue.

Edit: I'm using Visual Studio 2015. Also, this only happens when I run the page locally, ie. When debugging.

AxxieD
  • 614
  • 1
  • 7
  • 28
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Lews Therin May 27 '16 at 12:42
  • What language is that??? – llouk May 27 '16 at 17:40

2 Answers2

1

Set a breakpoint on the line giving you the error, then add dt.Result to the Watch window to examine its value when the execution breaks on this line. It could be that dt.Result is null.

edit: Also, I think you only need to specify the field name for .DataTextField and .DataValueField.

  • When I did that, it is saying that 'dt.result' is null. – AxxieD May 27 '16 at 12:41
  • ... which explains the `NullReferenceException` that you reported. Now... Stop. Think. –  May 27 '16 at 13:59
  • I really can't figure out what to do from here. I'm not really a programmer and this is my first time working with C# and Visual Studio. – AxxieD May 27 '16 at 17:12
  • 1
    This condition prevents the code that populates the drop-down list from executing, giving the impression that nothing is happening. I'm sorry to say this, but if you are not a programmer and are not [yet] capable of solving simple logic like this, then you need to stop and reconsider your next move. I would suggest investing in training if you want to become professional, otherwise get someone who is to do the job for you. In software development, trial-and-error does not work. –  May 27 '16 at 19:39
0

As it turns out, it was an issue with memory. Once I took care of that, it worked.

AxxieD
  • 614
  • 1
  • 7
  • 28