0

I'm using a repeater whose item has a button in it. For each button, want it to run the SaveToDatabase() function of the item in that repeater. Is it possible to bind a lambda to the Command event of that button?

Here's what my code looks like:

var subRole = item.DataItem as SubRole;
btn_EditRole.Command += (btnSender, args) => subRole.SaveToDatabase();

Trying it out, it throws an exception, but IDK if that's because I'm doing something stupid syntatically or because binding lambdas like this isn't possible.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request.

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

John Saunders
  • 160,644
  • 26
  • 247
  • 397
khalid13
  • 2,767
  • 2
  • 30
  • 48
  • Might help if you told us the error? – Simon Whitehead Oct 07 '13 at 21:48
  • Added, sorry. I thought I cp'd it in, but apparently I forgot. – khalid13 Oct 07 '13 at 21:50
  • Your cast is incorrect. The `as` operator returns `null` if the cast fails. That means you're essentially calling `null.SaveToDatabase()`.. which is obviously wrong. You should look at how you're actually filling the template and if each item is actually what you are casting to (`SubRole`). – Simon Whitehead Oct 07 '13 at 22:00
  • I don't think that's it. When Visual Studio breaks at the exception, it correctly shows subRole as the appropriate SubRole. – khalid13 Oct 07 '13 at 22:06
  • You need to provide more code then as I think @SimonWhitehead made a very good suggestion. If that's wrong then you must provide more code. – Enigmativity Oct 07 '13 at 22:22
  • I ended up skipping the lambdas and delegates route and using the command event, but I wish I could have figured this out. If anyone has any insight, please post it. – khalid13 Oct 10 '13 at 13:57
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Oct 13 '13 at 02:41

0 Answers0