You are here:

C#/Object reference not set to an instance of an object

Advertisement


Question
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;


namespace Gids_Recritment
{
   public partial class _Default : System.Web.UI.Page
   {
       SqlDataAdapter da = new SqlDataAdapter();
       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");
       protected void Page_Load(object sender, EventArgs e)
       {
         txt_date.Text = DateTime.Today.ToString("dd/MM/yyyy");
       }
       protected void txt_resume_TextChanged(object sender, EventArgs e)
       {

       }
       protected void txt_date_TextChanged(object sender, EventArgs e)
       {
       }
       protected void ddl_cancity_SelectedIndexChanged(object sender, EventArgs e)
       {

       }
       protected void ddl_canstate_SelectedIndexChanged(object sender, EventArgs e)
       {

       }
       protected void Button1_Click(object sender, EventArgs e)
       {
         DateTime Date = new DateTime();
         Date = System.DateTime.Now.Date;

         if (txt_date.Visible)
         {
         if (txt_date.Text != null && txt_date.Text != string.Empty)
         {
         txt_date.Text = DateTime.Today.ToString("dd/MM/yyyy");
         //Date = Convert.ToDateTime(txt_date.Text);
         //Date = Convert.ToDateTime(Date.ToString("dd-MMM-yyyy"));
         }
         }

         DataBinder ds = new DataBinder();
         sqlcon.Open();
         SqlCommand sqlcmd = new SqlCommand("INSERT INTO AddCandidate VALUES ('" + txt_date.Text + "," + txt_rcname.Text + "," + txt_vendor.Text + "," + txt_clname.Text + "," +
   txt_clnumber.Text + "," + txt_canname.Text + "," + ddl_canstate.SelectedItem.Text + "," + ddl_cancity.SelectedItem.Text + "," + txt_canrate.Text + "," + txt_phnumber.Text + "," +
   txt_email.Text + "," + txt_canskills.Text + "," + ddl_visastatus.SelectedItem.Text + "," +
   ddl_availibilty.SelectedItem.Text + "," + txt_canempname.Text + "," + txt_canempsign.Text + "," +
   txt_resume.Text + "')");
         sqlcmd.Parameters.AddWithValue("@Date", txt_date.Text);
         sqlcmd.Parameters.AddWithValue("@RecruiterName", txt_rcname.Text);
         sqlcmd.Parameters.AddWithValue("@Vendor", txt_vendor.Text);
         sqlcmd.Parameters.AddWithValue("@ClientName", txt_clname.Text);
         sqlcmd.Parameters.AddWithValue("@ClientNumber", txt_clnumber.Text);
         sqlcmd.Parameters.AddWithValue("@CanditateName", txt_canname.Text);
         sqlcmd.Parameters.AddWithValue("@CandidateState", ddl_canstate.SelectedItem.Text);
         sqlcmd.Parameters.AddWithValue("@CandidateCity", ddl_cancity.SelectedItem.Text);
         sqlcmd.Parameters.AddWithValue("@CandidateRate", txt_canrate.Text);
         sqlcmd.Parameters.AddWithValue("@PhoneNumber", txt_phnumber.Text);
         sqlcmd.Parameters.AddWithValue("@Email", txt_email.Text);
         sqlcmd.Parameters.AddWithValue("@CandidateSkills", txt_canskills.Text);
         sqlcmd.Parameters.AddWithValue("@VisaStatus", ddl_visastatus.SelectedItem.Text);
         sqlcmd.Parameters.AddWithValue("@Availability", ddl_availibilty.SelectedItem.Text);
         sqlcmd.Parameters.AddWithValue("@CandidateEmployerName", txt_canempname.Text);
         sqlcmd.Parameters.AddWithValue("@CandidateEmployerSignature", txt_canempsign.Text);
         sqlcmd.Parameters.AddWithValue("@Resume", txt_resume.Text);
         sqlcmd.ExecuteNonQuery();
         sqlcon.Close();
         if (fu_resume.HasFile)
         {
         fu_resume.SaveAs("D:\\My Documents\\Visual Studio 2010\\Projects\\Resumes\\" + fu_resume.FileName);
         }
         else
         {

         }
       }
       private void VALUES(string p)
       {
         throw new NotImplementedException();
       }
       public string filename { get; set; }
   }
}

Answer
Hi there,

You can learn exact line on which error occurs. That could help much more.

However if this is the whole code, there's high possibility that one of your DropDownList is empty and SelectedItem property is null.

Hope that helps,

Murat

C#

All Answers


Answers by Expert:


Ask Experts

Volunteer


Murat Mehmet

Expertise

I can help with questions about desktop and web programming in C#, including SOAP, XML, database managing, custom controls, security etc.

Experience

I have been developing web and especially desktop applications in C# and VB.Net for almost 5 years. My programming life has begun with VB6 long time ago, so its about 8 years that I am in this business.

Organizations
Was in R & D for 2 years in a popular Turkish technology website: cyber-warrior.com

Education/Credentials
2011 Computer Engineering graduation in University of Trakia in Turkey.

©2016 About.com. All rights reserved.