Solved

Adapter.InsertCommand.Parameters.AddWithValue NullReferenceException

Posted on 2012-03-25
6
765 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
While in ##Table - Help 4 19
bound data table problem 2 34
how to remove duplicate code from my project 5 37
Why use this lambda? 12 41
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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