Object Reference Error

Object Reference Not Set To Instance of Object

Page 1 of 1

6 Replies - 2870 Views - Last Post: 13 November 2008 - 01:01 PM Rate Topic: -----

#1 Dizdain  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 13-November 08

Object Reference Error

Posted 13 November 2008 - 12:20 PM

Hello. I've been having some trouble with this code and it's driving me insane. I've been coding for some time now and it may all just be a big brain-fart not being able to find the problem but anyhow. I keep getting the error, "Object Reference Not Set To Instance of Object" on the line which instantiates my connection. I am getting a NULL value for the object and the line of code getting the error is the line that gives the connection variable it's value. I'm quite confused. I'm unable to test any code I have created thus far because the error occurs early in the program (login stage) and prevents me from testing anything else until I get past it. I hope you can help me. The following is the code from the .cs file I am having trouble with.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace Artifact_X
{
	public partial class LoginForm : Form
	{
		public LoginForm()
		{
			InitializeComponent();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btnLogin_Click(object sender, EventArgs e)
		{
			bool blnFound = false;
			SqlConnection UserInfoConnection = null;
			UserInfoConnection = new SqlConnection(ConfigurationManager.AppSettings["userInfoConnectionString"].ToString());
			UserInfoConnection.Open();
			SqlCommand cmd = new SqlCommand("select * from userInfo",UserInfoConnection);
			SqlDataReader dr = cmd.ExecuteReader();
   
			while(dr.Read())
			{
				if(dr["user_name"].ToString() == txtUsername.Text)
				{
					if (dr["user_pass"].ToString().ToString() == txtPassword.Text) 
					{
						blnFound = true;
					}
				}
			}
			if(!blnFound)
			MessageBox.Show("The login credentials you entered do not match a record in our database.  Please try again or use the \"Forgot Password\" utility to recover a lost password.");
			dr.Close();
			UserInfoConnection.Close();

			if (blnFound == true)
			{
				MotherForm motherForm = new MotherForm();
				motherForm.Show();
				this.Close();
			}
		}

		private void LoginForm_Load(object sender, EventArgs e)
		{
			this.userInfoTableAdapter.Fill(this.logindbDataSet1.userInfo);
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Object Reference Error

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Object Reference Error

Posted 13 November 2008 - 12:26 PM

why not just do this?

SqlConnection UserInfoConnection = new SqlConnection(ConfigurationManager.AppSettings["userInfoConnectionString"].ToString());



also, are you sure there is a connection string with that name?
Was This Post Helpful? 0
  • +
  • -

#3 Dizdain  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 13-November 08

Re: Object Reference Error

Posted 13 November 2008 - 12:38 PM

I found the real name of the connection string, logindbConnectionString, and replaced a snippet of my code to match the code you provided. The result was: the error went away...kinda. Now the error dropped by (1) line and now flags the error on the line, "UserInfoConnection.Open();". Sorry if these are noobish questions, I haven't coded C# in quite some time. Thx

private void btnLogin_Click(object sender, EventArgs e)
		{
			bool blnFound = false;
			SqlConnection UserInfoConnection = null;
			SqlConnection UserInfoConnection = new SqlConnection(ConfigurationManager.AppSettings["logindbConnectionString"].ToString());
			UserInfoConnection.Open();
			SqlCommand cmd = new SqlCommand("select * from userInfo",UserInfoConnection);
			SqlDataReader dr = cmd.ExecuteReader();
   
			while(dr.Read())
			{
				if(dr["user_name"].ToString() == txtUsername.Text)
				{
					if (dr["user_pass"].ToString().ToString() == txtPassword.Text) 
					{
						blnFound = true;
					}
				}
			}
			if(!blnFound)
			MessageBox.Show("The login credentials you entered do not match a record in our database.  Please try again or use the \"Forgot Password\" utility to recover a lost password.");
			dr.Close();
			UserInfoConnection.Close();

			if (blnFound == true)
			{
				MotherForm motherForm = new MotherForm();
				motherForm.Show();
				this.Close();
			}
		}


Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Object Reference Error

Posted 13 November 2008 - 12:43 PM

Depending on how you have your connection in the config file (where it's in the <connectionStrings> section or not) try this

SqlConnection UserInfoConnection = null;
SqlConnection UserInfoConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnName"].ConnectionString);
 UserInfoConnection.Open();


Was This Post Helpful? 0
  • +
  • -

#5 Dizdain  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 13-November 08

Re: Object Reference Error

Posted 13 November 2008 - 12:54 PM

It's still throwing that exception but back on the line it was at initially. Also, it is declaring the same variable twice (UserInfoConnection = null & UserInfoConnection = SqlConnection(...). So, in effect, when I commented out the first declaration of UserInfoConnection, the error reverted back to it's original line. I'm trying to make a login form for my application that, upon successful login, transfers the user to the form, MotherForm.cs. Unfortunately, I can't get the login to work so I can't test out everything else. If we can't resolve this error, do you know of a good tutorial for making a login using Windows Forms? Thx
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Object Reference Error

Posted 13 November 2008 - 12:56 PM

I didnt even notice the dual declarations (sorry), first get rid of the first one, second make sure your connection string is in the <sonnectionString> section of your app.config file (or web.config if this is a web application)

Make sure to replace "YourConnName" with the name of your connection string

SqlConnection UserInfoConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnName"].ConnectionString);
UserInfoConnection.Open();


Was This Post Helpful? 0
  • +
  • -

#7 Dizdain  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 13-November 08

Re: Object Reference Error

Posted 13 November 2008 - 01:01 PM

Thx so much for your help guys. This place is awesome. Anyways, I found what the problem was and I will share it here for anyone else who may have a similar problem.

My connection string, logindbConnectionString was correct, kinda. When looking closer at my app.config file, the full connection string name was "Artifact_X.Properties.Settings.logindbConnectionString". I replaced logindbConnectionString in the code to the full connection string, "Artifact_X.Properties.Settings.logindbConnectionString", and the code executed properly.

As we all know, programming doesn't know the concept of "kinda" correct. Either the code works or it doesn't. Thanks a lot for your help.

:D :D :D

~Dizdain
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1