473,322 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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;
  10.  
  11. namespace InformationDesk
  12. {
  13.     public class DBHandler
  14.     {
  15.  
  16.  
  17.         public void GetAllValues(Person V)
  18.         {
  19.  
  20.             SqlConnection con = new SqlConnection();
  21.             con.ConnectionString = ConfigurationManager.ConnectionStrings["FinalProjectDB"].ConnectionString;
  22.  
  23.  
  24.             SqlCommand com = new SqlCommand();
  25.             com.CommandText = "INSERT INTO Visitors (Name, Email) VALUES (@Name, @Email);";
  26.  
  27.             com.Connection = con;
  28.  
  29.  
  30.             com.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = V.Name;
  31.             com.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = V.Email;
  32.  
  33.             con.Open();
  34.  
  35.             com.ExecuteNonQuery();
  36.  
  37.             con.Close();
  38.  
  39.         }
  40.  
  41.     }
  42.  
  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
5 3789
GaryTexmo
1,501 Expert 1GB
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
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.     {
  3.  
  4.         private const String ConnectionString = @"initial catalog=FinalProjectDB;Data Source=ALIEN\SQLEXPRESS;integrated Security=true;";
  5.  
  6.  
  7.         public void GetAllValues(Person V)
  8.         {
  9.  
  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);";
  15.  
  16.             objSql.Connection = conn;
  17.  
  18.             objSql.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = V.Name;
  19.             objSql.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = V.Email;
  20.  
  21.             conn.Open();
  22.  
  23.             objSql.ExecuteNonQuery();
  24.  
  25.             conn.Close();
  26.  
  27.         }
  28.  
  29.  
  30.  
  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
cloud255
427 Expert 256MB
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
Ok I will include Exception Handling in it... Thanks for comments...
Sep 6 '09 #5
tlhintoq
3,525 Expert 2GB
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

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

Similar topics

1
by: Paloma García | last post by:
Dear all, I have created personalized configuration sections in my web project following the instructions described in this page...
0
by: Michael Chong | last post by:
I have an (exe) executable program created in VB.NET 2003 that calls to a MFC DLL written in VC++.NET 2003. I always get an error msg "NullReferenceException: Object Reference Not Set to an...
5
by: Tee | last post by:
Hi, I having a problem with my asp.net project. The project work fine on my own PC, but when I upload it to the server, I got the following error : Object reference not set to an instance of...
18
by: Microsoft | last post by:
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object" Dim g As System.Drawing.Graphics g.DrawString("Test", New Font("Arial", 12,...
0
by: Martin Widmer | last post by:
Hello again! I have a datagridview control on my form and am using VS.Net 2005. One column is set up as combo box column, and when I try to set the datasource for that combobox column at design...
2
by: facole | last post by:
I'm new to the .net environment. What I'm trying to do is to have an html page call a .aspx application and pass certain information. I have a field in the html file <input type="text"...
2
by: =?Utf-8?B?QmFkaXM=?= | last post by:
I'm doing a server side automation(I know it's bad but..) and its working fine locally and when accessing it from a remote machine using web browser is was giving me this error"Retrieving the COM...
3
by: Sarah | last post by:
Hi - Please be gentle. I am quite new to visual basic, but I have been going through tutorials and reading up. I found a code snippet on the internet that I wanted to see if I could re-purpose...
3
by: shapper | last post by:
Hello, I have the following loop and error message: foreach (Theme theme in Profile.Collaborator.Themes) { ... And I get the following error: Object reference not set to an instance of an...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.