Solved

Adapter.InsertCommand.Parameters.AddWithValue NullReferenceException

Posted on 2012-03-25
6
755 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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