Aktham Mahmoud

Aktham Mahmoud

  • NA
  • 720
  • 37.8k

Problem in Null object

Apr 30 2021 6:24 PM
Greeting
I used gridview in my system to  review users, anyway when I need to delete user, I need to deleted all rows in many tables depends on UserId. Actually I created a stored procedure  take an input parameter (UserId) from gridview.
See gridview code herer
  1.       <asp:GridView ID="gvbremployee" runat="server" AllowSorting="True" AutoGenerateColumns="False" Font-Size="Medium" Width="60%" DataKeyNames="UserId" DataSourceID="SDS_BR_DLL" EmptyDataText="Sorry! No staff in selected branch." OnSelectedIndexChanged="gvbremployee_SelectedIndexChanged" OnRowDeleting="gvbremployee_RowDeleting">    
  2.     
  3.           <Columns>    
  4.               <asp:TemplateField ShowHeader="False">    
  5.                   <ItemTemplate>    
  6.                       <asp:LinkButton ID="LnkBtn_Sel" runat="server" CausesValidation="False" CommandName="Select" class="btn btn-outline-primary"><i class="fa fa-folder-open"></i></asp:LinkButton>    
  7.                       <asp:LinkButton ID="LnkBtn_del" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete Employee row?')" class="btn btn-outline-danger"><i class="fa fa-trash" aria-hidden="true"></i></asp:LinkButton>    
  8.                   </ItemTemplate>    
  9.               </asp:TemplateField>    
  10.               <asp:BoundField DataField="UserId" Visible="False" HeaderText="UserId" />    
  11.     
  12.               <asp:BoundField DataField="UserName" HeaderText="UserName"/>    
  13.                 
  14.               <asp:BoundField DataField="S_Gender" HeaderText="Gender" SortExpression="S_Gender" />    
  15.               <asp:BoundField DataField="S_Salary" HeaderText="Salary" SortExpression="S_Salary" />    
  16. <asp:CheckBoxField DataField="S_Status" HeaderText="Status" SortExpression="S_Status" />    
  17.           </Columns>    
  18.       </asp:GridView> 
Code behide for delete user event in gridview 
  1. protected void gvbremployee_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  2. {  
  3.       
  4.     try  
  5.     {  
  6.         GridViewRow row = (GridViewRow)gvbremployee.Rows[e.RowIndex];  
  7.     string idvalue = gvbremployee.DataKeys[row.RowIndex].Values["UserId"].ToString();  
  8.           
  9.         using (SqlConnection sqlcon = new SqlConnection(Constr))  
  10.         {  
  11.         
  12.             SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Constr"].ConnectionString);  
  13.               
  14.             SqlCommand comm = new SqlCommand();  
  15.             comm.CommandText = "del_user"//this stored procedure and has an ID called UserId  
  16.             comm.CommandType = CommandType.StoredProcedure;  
  17.             //Pass the value here  
  18.             comm.Parameters.AddWithValue("@UserId", idvalue);  
  19.             comm.Connection = conn;  
  20.   
  21.             conn.Open();  
  22.             comm.ExecuteNonQuery();  
  23.             conn.Close();  
  24.         }  
  25.     }  
  26.     catch (Exception ex)  
  27.     {  
  28.          Lbinfo.Text = ex.Message;  
  29.         Lbinfo.ForeColor = System.Drawing.Color.Red;  
  30.          
  31.     }  
  32.   
  33. }  
 Stored Procedure code
 
  1. CREATE PROCEDURE [dbo].[del_user]  
  2.       
  3. AS  
  4.         DECLARE @UserId uniqueidentifier  
  5.     SET @UserId = 'THE GUID OF THE USER HERE'  
  6.        
  7.    DELETE FROM Profiles WHERE UserId = @UserId  
  8.    DELETE FROM UsersInRoles WHERE UserId = @UserId  
  9.    DELETE FROM Memberships WHERE UserId = @UserId  
  10.    DELETE FROM Users WHERE UserId = @UserId  
  11.    DELETE FROM Staff WHERE UserId = @UserId  
  12. RETURN 0  
When I execute app. and delete user an error messge is:
 
Object reference not set to an instance of an object.
 
That's mean a null in send parameter to stored procedure
 
A first chance exception of type 'System.NullReferenceException' occurred in WebApplication1.dll , can not able to catch (UserId) Valye
 
I change method
string idvalue = gvbremployee.DataKeys[row.RowIndex].Values["UserId"].ToString();
to
object idvalue = gvbremployee.Rows[e.RowIndex].Cells[0].UniqueID;
 
So what a problem, please.
Many thanks 

Answers (5)