Object reference not set to an instance of an object.

I am receiving the "Object reference not set to an instance of an object. " on my production web site, but am not receiving it on my developement web site.   I have attached the code it is error out on.  Any ideas why this is happening.

thank you
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click

        Dim i As Integer = 0
        'Dim myConn As New SqlConnection(myConnStr)
        Dim myRow As DataRow
        Dim mUser As MembershipUser = (Session("myUser"))
        On Error GoTo Problem

        For i = 0 To myDS.Tables(1).Rows.Count - 1
            myRow = myDS.Tables(1).Rows(i)
            Dim myCMD As New SqlCommand
            With myCMD
                .Connection = myConn
                .CommandType = CommandType.StoredProcedure
                .CommandText = "TAG_UpdateMOInspectionDetail_Web"
                .Parameters.Add(New SqlParameter("@ItemQty", myRow.Item(5).ToString))
                .Parameters.Add(New SqlParameter("@ItemCost", myRow.Item(6)))
                .Parameters.Add(New SqlParameter("@ItemPrice", myRow.Item(7)))
                .Parameters.Add(New SqlParameter("@Charge", myRow.Item(8)))
                .Parameters.Add(New SqlParameter("@LastEditDate", FormatDateTime(Now, DateFormat.GeneralDate)))
                .Parameters.Add(New SqlParameter("LastEditBy", mUser.UserName))
                .Parameters.Add(New SqlParameter("@ID", myRow.Item(0)))
            End With
        lblStatus.Text = "Changes Saved"
        lblStatus.Visible = True
        Exit Sub
        Dim myMessage As String = Err.Description
        lblStatus.Text = "Problem Saving Changes" & Err.Description
        lblStatus.Visible = True
    End Sub

Open in new window

Who is Participating?
CodeCruiserConnect With a Mentor Commented:
>On Error Goto Problem

That is not used in .NET. Use Try catch blocks instead

I suspect the error is in
>For i = 0 To myDS.Tables(1).Rows.Count - 1

What is MyDS? Dataset? Did you save it to session and restore it?

Also, do you have two tables in dataset? Index starts at 0 in .NET so if you have only one table then use

Wouter BoevinkMasterCommented:
On which line does the error occur?
mgmhicksAuthor Commented:
Since it only does it on Production not really sure.  Put the code looks good no?   Any ideas on how to find the exact line that it is erroring?

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Wouter BoevinkMasterCommented:
My best guess is that the user is impersonted bu not authenticated.

Please read the following article: http://support.microsoft.com/kb/306158

Especially the part about 'Impersonate a Specific User in Code'.

<%@ Page Language="C#"%>
<%@ Import Namespace = "System.Web" %>
<%@ Import Namespace = "System.Web.Security" %>
<%@ Import Namespace = "System.Security.Principal" %>
<%@ Import Namespace = "System.Runtime.InteropServices" %>

<script runat=server>
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;

WindowsImpersonationContext impersonationContext; 

public static extern int LogonUserA(String lpszUserName, 
	String lpszDomain,
	String lpszPassword,
	int dwLogonType, 
	int dwLogonProvider,
	ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int DuplicateToken(IntPtr hToken, 
	int impersonationLevel,  
	ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool RevertToSelf();

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern  bool CloseHandle(IntPtr handle);

public void Page_Load(Object s, EventArgs e)
	if(impersonateValidUser("username", "domain", "password"))
		//Insert your code that runs under the security context of a specific user here.
		//Your impersonation failed. Therefore, include a fail-safe mechanism here.

private bool impersonateValidUser(String userName, String domain, String password)
	WindowsIdentity tempWindowsIdentity;
	IntPtr token = IntPtr.Zero;
	IntPtr tokenDuplicate = IntPtr.Zero;

		if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, 
			LOGON32_PROVIDER_DEFAULT, ref token) != 0)
			if(DuplicateToken(token, 2, ref tokenDuplicate) != 0) 
				tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
				impersonationContext = tempWindowsIdentity.Impersonate();
				if (impersonationContext != null)
					return true;
	if(token!= IntPtr.Zero)
	return false;

private void undoImpersonation()

Open in new window

Wouter BoevinkMasterCommented:
Sorry, the right answer to the wrong question :-)
mgmhicksAuthor Commented:
The error ended up being on line
  .Parameters.Add(New SqlParameter("LastEditBy", mUser.UserName))
muser.username was nothing.  Probably because of authenication.  Changed code to

 Dim mCurrUser As String = Page.User.Identity.Name

and then

 .Parameters.Add(New SqlParameter("LastEditBy", mcurrUser))
and it works fine.

Not sure why it would error on production but not developement machine but thats what fixed it

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.