Solved

Adapter.InsertCommand.Parameters.AddWithValue NullReferenceException

Posted on 2012-03-25
6
739 Views
Last Modified: 2012-03-28
Ok, I have an issue that I cannot seem to grasp. As per the title of the post, I am receiving a NullReferenceException when I access a stored procedure from a DataGridView in a C# application. The line of code that keeps throwing the exception is:
this.spPullAssignedAssetsTableAdapter.Adapter.InsertCommand.Parameters.AddWithValue("@aUser", Variables.userName);

Open in new window


userName is coming from a class called variables. The call for that variable is:
public class Variables
    {
        public static string userName;
    }

Open in new window


The ultimate conclusion to this task is to pass the userName variable to the stored procedure connected in the data adapter.

I have no idea what I am doing wrong. Following the debugger, I can see that the variable is being passed through correctly, but i have no idea why the NullReferenceException is being thrown.

Thank you for your time and any help is greatly appreciated.
0
Comment
Question by:Sirjacksinurbox
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:sachitjain
ID: 37764825
How is this.spPullAssignedAssetsTableAdapter.Adapter.InsertCommand? How are you setting it in your code?
0
 
LVL 2

Author Comment

by:Sirjacksinurbox
ID: 37769605
Do you mean how is the above line of code being called?

If so, its being called in the frmAssetsAssigned.Designer.cs code.

Nothing I have (any variable associated with this line of code, including the variables in the stored procedure) have any null reference associated with them.

If I need to move it away from the Designer.cs , any direction would be greatly appreciated.

Thanks.
0
 
LVL 12

Expert Comment

by:sachitjain
ID: 37769954
I mean you are appending parameter for this.spPullAssignedAssetsTableAdapter.Adapter.InsertCommand object but before that have you checked nullability of this object? In other words, this object (InsertCommand) for desired Adapter object should be set to some not null reference before we set its parameters.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:Sirjacksinurbox
ID: 37771160
Oh, I see.

Forgive me for sounding like a complete novice (for which I pretty much am), but how do you set that variable to something else before you assign the variable to it? (Especially since it takes two parameters)

Also, would it make more sense to place this line of code in the form load method of that form, instead of the Designer.cs?
0
 
LVL 12

Accepted Solution

by:
sachitjain earned 500 total points
ID: 37771200
I would suggest check its nullability in your form load event like
if(this.spPullAssignedAssetsTableAdapter != null)
{
      if(this.spPullAssignedAssetsTableAdapter.Adapter != null)
      {
            if(this.spPullAssignedAssetsTableAdapter.Adapter.InsertCommand == null)
            {
                  this.spPullAssignedAssetsTableAdapter.Adapter.InsertCommand = <<Creation logic>>
            }
            Next steps..
      }
}
0
 
LVL 2

Author Closing Comment

by:Sirjacksinurbox
ID: 37779777
It wasn't exactly what I was looking for, but it gave me the idea to look to LINQ instead.

Thank you.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now