0
我有一個問題,當我運行我的代碼時發生錯誤,「對象引用未設置爲對象的實例」。錯誤是「對象引用未設置爲對象的實例」
PLZ建議我關於這一點。
代碼
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();
}
「謝謝」
使用「調試器」,在「btn_Save_Click」中設置「斷點」,並檢查哪個「變量」爲空。 – Vlad 2012-04-26 11:28:16
順便說一句,你的代碼可能不是SQL注入安全的。 (有關更多詳細信息,請參閱http://xkcd.com/327/。) – Vlad 2012-04-26 11:30:44