Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Adapter.InsertCommand.Parameters.AddWithValue NullReferenceException

Posted on 2012-03-25
6
Medium Priority
?
817 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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 1500 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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

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!
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

885 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