-2

I have the code below

It throws an error "Object reference not set to an instance of object"

TextBox rateCenterName = (TextBox)row.FindControl("txtRateCenterName");
    cmd.Parameters.Add("@RateCenterName", OleDbType.VarChar).Value = rateCenterName.Text;
AlfeG
  • 1,475
  • 4
  • 18
  • 33
Arjun Babu
  • 219
  • 4
  • 10
  • 19
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Feb 24 '12 at 13:30
  • debug it in VS, what is the problem? Either row or cmd is null. you can see it in debugger. – Alexey Raga Feb 24 '12 at 13:31

5 Answers5

1

Well, you haven't said which line throws the exception. Options:

  • row is null
  • rateCenterName is null, indicating that the control "txtRateCenterName" couldn't be found within row
  • cmd is null

That's pretty much all it can be, but we can't possibly say which it is from that snippet of code. Put a breakpoint in the debugger and work out which it is...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Either row or rateCenterName or cmd is null. You haven't given us enough information to know which. You need to work with the debugger to find out on which line the exception is thrown, and then you can investigate from there. You can use the Local or Watch feature in the debugger to see the value of each of these variables when the exception is thrown.

If I had to guess, rateCenterName is null because a control with the name txtRateCenterName couldn't be found. Check that you have the name right. If that's not it, check row and cmd too.

jason
  • 236,483
  • 35
  • 423
  • 525
0

For some reason, (TextBox)row.FindControl("txtRateCenterName"); returns null (best guess) The control probably does not exist.

Jan Kratochvil
  • 2,307
  • 1
  • 21
  • 39
0

The error is telling you exactly what the problem is: you are trying to work with an object reference that is null.

Mostly likely the culprit is row.FindControl("txtRateCenterName") as FindControl will return null if it did not find the given control.

Make sure you've spelled it right, and that it actually exists on the page.

Also it could be your cmd object, there's no way to tell from here. Use your debugger.

asawyer
  • 17,642
  • 8
  • 59
  • 87
0

It might be in another control hence it could not able to find

Prabhavith
  • 458
  • 1
  • 4
  • 15