Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried to delete a row in a table.But it was saying error as in the following way.
Server Error in '/' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 74: protected void EmployeeGridView_RowDelete(object sender, GridViewDeleteEventArgs e)
Line 75: {
Line 76: int empid = Convert.ToInt32(((TextBox)EmployeeGridView.Rows[e.RowIndex].FindControl("txtempid")).Text);

in line 76 ,it was saying that error.

Please tell me solution rectify the error


I have written code for deleting was as like below
C#
protected void EmployeeGridView_RowDelete(object sender, GridViewDeleteEventArgs e)
       {
           int empid = Convert.ToInt32(((TextBox)EmployeeGridView.Rows[e.RowIndex].FindControl("txtempid")).Text);
           string connectionstring = @" Data Source=01HW235982\SQLEXPRESS2008;Initial Catalog=adarsh;Persist Security Info=True;User ID=sa;Password=Pass@123";
           SqlConnection con = new SqlConnection(connectionstring);
           con.Open();
           SqlCommand cmd = new SqlCommand("DeleteEmployee", con);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.AddWithValue("@emp_id ", empid);
           cmd.ExecuteNonQuery();
           FillGrid();
       }


Please tell me the solution
Posted
Updated 21-Nov-12 1:59am
v2
Comments
bbirajdar 21-Nov-12 7:54am    
check this value in quickwatch- EmployeeGridView.Rows[e.RowIndex].FindControl("txtempid"))

This is probably null because the id txtempid is misspelled...
vinod.sambangi 21-Nov-12 8:02am    
I have checked it.But the id was correct.The same id i have used for updation there it was working but here it was not working.
Please tell me the solution
Guptha Ch 21-Nov-12 8:08am    
Can you provide the full code.
It seems the provided code is ok.
vinod.sambangi 26-Nov-12 2:34am    
That is the code i have written there.
The error it was saying when i was tring to delete ,it's like
"Server Error in '/' Application and
Object reference not set to an instance of an object.
".

int empid = Convert.toInt32(EmployeeGridView.Rows[e.RowIndex].Cells[CellNumber].Text);
what is the error... ur code seems ok
 
Share this answer
 
int empid = Convert.ToInt32(EmployeeGridView.DataKeys[e.RowIndex].Value.ToString());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900