Solved

Adapter.InsertCommand.Parameters.AddWithValue NullReferenceException

Posted on 2012-03-25
6
786 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

628 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