473,322 Members | 1,755 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.

Object reference not set to an instance of an object

I am experiencing an error "Object reference not set to an instance of an object."

Here is my code:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.Data.Sql;
  14. using System.Data.SqlClient;
  15.  
  16.  
  17. namespace Gids_Recritment
  18. {
  19.     public partial class _Default : System.Web.UI.Page
  20.     {
  21.         SqlDataAdapter da = new SqlDataAdapter();
  22.         SqlConnection sqlcon = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\My Documents\Visual Studio 2010\Projects\Gids\Gids\App_Data\Database1.mdf;Integrated Security=True;User Instance=True");
  23.         protected void Page_Load(object sender, EventArgs e)
  24.         {
  25.             txt_date.Text = DateTime.Today.ToString("dd/MM/yyyy");
  26.         }
  27.         protected void txt_resume_TextChanged(object sender, EventArgs e)
  28.         {
  29.  
  30.         }
  31.         protected void txt_date_TextChanged(object sender, EventArgs e)
  32.         {
  33.         }
  34.         protected void ddl_cancity_SelectedIndexChanged(object sender, EventArgs e)
  35.         {
  36.  
  37.         }
  38.         protected void ddl_canstate_SelectedIndexChanged(object sender, EventArgs e)
  39.         {
  40.  
  41.         }
  42.         protected void Button1_Click(object sender, EventArgs e)
  43.         {
  44.             DateTime Date = new DateTime();
  45.             Date = System.DateTime.Now.Date;
  46.  
  47.             if (txt_date.Visible)
  48.             {
  49.                 if (txt_date.Text != null && txt_date.Text != string.Empty)
  50.                 {
  51.                     txt_date.Text = DateTime.Today.ToString("dd/MM/yyyy");
  52.                     //Date = Convert.ToDateTime(txt_date.Text);
  53.                     //Date = Convert.ToDateTime(Date.ToString("dd-MMM-yyyy"));
  54.                 }
  55.             }
  56.  
  57.               DataBinder ds = new DataBinder();
  58.             sqlcon.Open();
  59.             SqlCommand sqlcmd = new SqlCommand("INSERT INTO AddCandidate VALUES ('" + txt_date.Text + "," + txt_rcname.Text + "," + txt_vendor.Text + "," + txt_clname.Text + "," +
  60.     txt_clnumber.Text + "," + txt_canname.Text + "," + ddl_canstate.SelectedItem.Text + "," + ddl_cancity.SelectedItem.Text + "," + txt_canrate.Text + "," + txt_phnumber.Text + "," +
  61.     txt_email.Text + "," + txt_canskills.Text + "," + ddl_visastatus.SelectedItem.Text + "," +
  62.     ddl_availibilty.SelectedItem.Text + "," + txt_canempname.Text + "," + txt_canempsign.Text + "," +
  63.     txt_resume.Text + "')");
  64.             sqlcmd.Parameters.AddWithValue("@Date", txt_date.Text);
  65.             sqlcmd.Parameters.AddWithValue("@RecruiterName", txt_rcname.Text);
  66.             sqlcmd.Parameters.AddWithValue("@Vendor", txt_vendor.Text);
  67.             sqlcmd.Parameters.AddWithValue("@ClientName", txt_clname.Text);
  68.             sqlcmd.Parameters.AddWithValue("@ClientNumber", txt_clnumber.Text);
  69.             sqlcmd.Parameters.AddWithValue("@CanditateName", txt_canname.Text);
  70.             sqlcmd.Parameters.AddWithValue("@CandidateState", ddl_canstate.SelectedItem.Text);
  71.             sqlcmd.Parameters.AddWithValue("@CandidateCity", ddl_cancity.SelectedItem.Text);
  72.             sqlcmd.Parameters.AddWithValue("@CandidateRate", txt_canrate.Text);
  73.             sqlcmd.Parameters.AddWithValue("@PhoneNumber", txt_phnumber.Text);
  74.             sqlcmd.Parameters.AddWithValue("@Email", txt_email.Text);
  75.             sqlcmd.Parameters.AddWithValue("@CandidateSkills", txt_canskills.Text);
  76.             sqlcmd.Parameters.AddWithValue("@VisaStatus", ddl_visastatus.SelectedItem.Text);
  77.             sqlcmd.Parameters.AddWithValue("@Availability", ddl_availibilty.SelectedItem.Text);
  78.             sqlcmd.Parameters.AddWithValue("@CandidateEmployerName", txt_canempname.Text);
  79.             sqlcmd.Parameters.AddWithValue("@CandidateEmployerSignature", txt_canempsign.Text);
  80.             sqlcmd.Parameters.AddWithValue("@Resume", txt_resume.Text);
  81.             sqlcmd.ExecuteNonQuery();
  82.             sqlcon.Close();
  83.             if (fu_resume.HasFile)
  84.             {
  85.                 fu_resume.SaveAs("D:\\My Documents\\Visual Studio 2010\\Projects\\Resumes\\" + fu_resume.FileName);
  86.             }
  87.             else
  88.             {
  89.  
  90.             }
  91.         }
  92.         private void VALUES(string p)
  93.         {
  94.             throw new NotImplementedException();
  95.         }
  96.         public string filename { get; set; }
  97.     }
  98. }
Jan 22 '13 #1
1 10948
Frinavale
9,735 Expert Mod 8TB
What line is this exception being thrown on in the above code?

We can't really help you unless we know at least that much.

When you see the error message "Object reference not set to an instance of an object." it means that your application is throwing a NullReference Exception.

This type of exception is thrown when you have declared something but have not instantiated it.

In other words, if you declare a variable but don't call "new" to "instantiate" it.

For example, this code will throw an exception because you are using p before it is instantiated:
Expand|Select|Wrap|Line Numbers
  1. Person p;
  2. p.EyeColor = "Green";
To fix the problem, instantiate p.

For example, this code will not throw an exception because you instantiated p before you used it:
Expand|Select|Wrap|Line Numbers
  1. Person p;
  2. p = new Person("John Doe");
  3. p.EyeColor = "Green";


If you are retrieving an object that is returned by a function, you need to check if that object is instantiated first.

To do this, simply check if the object is null

Expand|Select|Wrap|Line Numbers
  1. if !(p == null){
  2.   p.EyeColor = "Green";
  3. }


-Frinny
Jan 22 '13 #2

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

Similar topics

2
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server...
6
by: NewToDotNet | last post by:
I am getting "Object reference not set to an instance of an object. " when I attempt to open a C# windows service class in design view, although I was able to initially create the service and open...
4
by: Frawls | last post by:
Hi, I get the following error when trying to run a search on my aspx site, this error only occours if the product im searching for does not exist. Can anybody explain this please and help me...
2
by: jw56578 | last post by:
why am i getting the "Object reference not set to an instance of an object" error on all page registeration tags in my project. <%@ Page Language="vb" AutoEventWireup="false"...
3
by: nemo | last post by:
Hi, My application works fine on the localhost but spits this error as soon as I put it on the server. I know this error occurs when an object has not been instantiated prior to a reference, but...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
7
by: Brett | last post by:
I'm not sure why I keep getting this error, "Object reference not set to an instance of an object". Private Function somefunction() as string Dim MyCurrentClass As New Class1 Try For i As...
6
by: kalaivanan | last post by:
hi, i am a beginner in c#. i have theoretical knowledge about object, reference and instance. but i want to know clearly about what is an object, reference and instance. can any one help me? or...
1
by: vishnu | last post by:
Hi, I am working on asp.net project which I converted the code fron VB to C# and instead of RaiseEvent in VB code I used the following code. using System; using System.Data; using...
4
by: livmacca | last post by:
Hi, I am new to VB .Net programming and is trying to create a webpage. I encountered the following error and is totally clueless on how to make it work: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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...
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.