By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,166 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,166 IT Pros & Developers. It's quick & easy.

Help on Error "Object reference not set to an instance of an object."

P: 21
Hi everybody actually i was trying to insert some records in to SQL server through C# Wndows Aplication and during that i face an error "Object reference not set to an instance of an object." and need help from senors please explain me about it and also indicate it in my code.

Here is my code in my Class Named DBHandler!!!

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.SqlClient;
  6. using Program;
  7. using CustomTypes;
  8. using System.Data;
  9. using System.Configuration;
  11. namespace InformationDesk
  12. {
  13.     public class DBHandler
  14.     {
  17.         public void GetAllValues(Person V)
  18.         {
  20.             SqlConnection con = new SqlConnection();
  21.             con.ConnectionString = ConfigurationManager.ConnectionStrings["FinalProjectDB"].ConnectionString;
  24.             SqlCommand com = new SqlCommand();
  25.             com.CommandText = "INSERT INTO Visitors (Name, Email) VALUES (@Name, @Email);";
  27.             com.Connection = con;
  30.             com.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = V.Name;
  31.             com.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = V.Email;
  33.             con.Open();
  35.             com.ExecuteNonQuery();
  37.             con.Close();
  39.         }
  41.     }
  43. }
Person is the class having attrbutes of Person such as Name and Email Address

And i am using GetAllValues function to get values from user in a windows form
after getting values from user assign them to sql command parameters to insert but fcing an error please help me as soon as possible however i am sure that the function GetAllValues need some maintenance in the above code snippte...
Sep 5 '09 #1
Share this Question
Share on Google+
5 Replies

Expert 100+
P: 1,501
Well, to be honest, your null reference error could be coming from a lot of places. Likely candidates are the parameter you're passing to the method and the connection string itself. I'd suggest throwing a breakpoint in there and stepping through to see which line fails.

In addition, when you do stuff like this it's usually a good idea to throw it in a try/catch block, that way you can handle errors more gracefully and recover, instead of taking down your whole program.
Sep 5 '09 #2

P: 21
Hi Gary thanks for your response but now i have tried another trick with the code now my code look like that.

Expand|Select|Wrap|Line Numbers
  1. public class DBHandler
  2.     {
  4.         private const String ConnectionString = @"initial catalog=FinalProjectDB;Data Source=ALIEN\SQLEXPRESS;integrated Security=true;";
  7.         public void GetAllValues(Person V)
  8.         {
  10.             SqlConnection conn = new SqlConnection(ConnectionString);
  11.             SqlCommand objSql = new SqlCommand();
  12.             objSql.Connection = conn;
  13.             objSql.CommandType = CommandType.Text;
  14.             objSql.CommandText = "INSERT INTO Visitors (Name, Email) VALUES (@Name, @Email);";
  16.             objSql.Connection = conn;
  18.             objSql.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = V.Name;
  19.             objSql.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = V.Email;
  21.             conn.Open();
  23.             objSql.ExecuteNonQuery();
  25.             conn.Close();
  27.         }
  31.     }
This code is working well but what i want is just the confirmaton. Is this code safe or not in terms of project crash situations...

i shall wait for your reply...
Sep 6 '09 #3

Expert 100+
P: 427
No, that code of yours is not safe, look into using TRY CATCH blocks around the DB access. This will allow you to handle exceptions from the DB (server may be offline, validation failure, etc) and allow the user to decide which action to take in response to the exception.
Sep 6 '09 #4

P: 21
Ok I will include Exception Handling in it... Thanks for comments...
Sep 6 '09 #5

Expert 2.5K+
P: 3,525
While its good to not crash, its better resolve the actual problem.
Put a breakpoint at line 10.
Walk through the code line by line (F-10)
Look at the values of each variable/object using the "locals" and "autos" windows. This should let you inspect each variable you are using to see which is still null when you try to use it.

Also setting Visual Studio to break when an exception is thrown, instead of when it goes unhandled will stop it at the point where you null object is trying to be used.
Debug | Exceptions
Click on the 'thrown' checkbox for "Common Language Runtime Exceptions" then run/debug your solution (F5)
Sep 6 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.