0

i have a problem, when i m run my code then error is occured that "Object reference not set to an instance of an object."

plz suggest me regarding that.

Code

protected void btn_Save_Click(object sender, EventArgs e)
    {
        string str = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/Geeta/Desktop/eTimeTrackLite1.mdb;Persist Security Info=False;");
        OleDbConnection conn = new OleDbConnection(str);
        conn.Open();
        string query = "insert into Employees ([EmployeeName],EmployeeCode,DeviceCode,Company,Department,Designation,Grade,Team,Location,EmploymentType,Category,HolidayGroup,ShiftGroup,ShiftRoster,Dateofjoining,Dateofconfirmation,Status,DateofResigning,[Sex]) values ('" + txt_empname.Text + "','" + txt_code.Text + "', '" + txt_dcode.Text + "', '" + dp_company.SelectedItem.ToString() + "', '" + dp_department.SelectedItem.ToString() + "', '"+dp_designation.SelectedItem.ToString()+"', '"+dp_grade.SelectedItem.ToString()+"', '"+dp_team.SelectedItem.ToString()+"', '"+dp_location.SelectedItem.ToString()+"', '"+dp_emptype.SelectedItem.ToString()+"', '"+dp_category.SelectedItem.ToString()+"', '"+dp_holigroup.SelectedItem.ToString()+"', '"+dp_shiftgroup.SelectedItem.ToString()+"', '"+dp_shiftroster.SelectedItem.ToString()+"', '"+dp_day.SelectedItem.ToString()+"', '"+dp_month.SelectedItem.ToString()+"', '"+dp_year.SelectedItem.ToString()+"', '"+dp_cday.SelectedItem.ToString()+"', '"+dp_cmonth.SelectedItem.ToString()+"', '"+dp_cyear.SelectedItem.ToString()+"', '"+dp_status.SelectedItem.ToString()+"', '"+dp_rday.SelectedItem.ToString()+"', '"+dp_rmonth.SelectedItem.ToString()+"', '"+dp_ryear.SelectedItem.ToString()+"', '"+rdbtn_male.Checked.ToString()+"', '"+rdbtn_female.Checked.ToString()+"')";
        OleDbCommand cmd = new OleDbCommand(query, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        BindGridData();
    }

"Thanks"

corn3lius
  • 4,857
  • 2
  • 31
  • 36
Sanat Pandey
  • 4,081
  • 17
  • 75
  • 132
  • 1
    Use a "debugger", set a "breakpoint" in your "btn_Save_Click", and check which "variable" is null. – Vlad Apr 26 '12 at 11:28
  • 3
    BTW your code is perhaps not SQL injection-safe. (See http://xkcd.com/327/ for more details.) – Vlad Apr 26 '12 at 11:30

1 Answers1

1

In order to avoid this kind of problems, don't use ToString is there is a risk of the calling object to be null, use Convert.ToString() instead. http://msdn.microsoft.com/en-us/library/system.convert.tostring.aspx