TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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
<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"
>
<Columns>
<asp:TemplateField ShowHeader=
"False"
>
<ItemTemplate>
<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>
<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>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField=
"UserId"
Visible=
"False"
HeaderText=
"UserId"
/>
<asp:BoundField DataField=
"UserName"
HeaderText=
"UserName"
/>
<asp:BoundField DataField=
"S_Gender"
HeaderText=
"Gender"
SortExpression=
"S_Gender"
/>
<asp:BoundField DataField=
"S_Salary"
HeaderText=
"Salary"
SortExpression=
"S_Salary"
/>
<asp:CheckBoxField DataField=
"S_Status"
HeaderText=
"Status"
SortExpression=
"S_Status"
/>
</Columns>
</asp:GridView>
Code behide for delete user event in gridview
protected
void
gvbremployee_RowDeleting(
object
sender, GridViewDeleteEventArgs e)
{
try
{
GridViewRow row = (GridViewRow)gvbremployee.Rows[e.RowIndex];
string
idvalue = gvbremployee.DataKeys[row.RowIndex].Values[
"UserId"
].ToString();
using
(SqlConnection sqlcon =
new
SqlConnection(Constr))
{
SqlConnection conn =
new
SqlConnection(ConfigurationManager.ConnectionStrings[
"Constr"
].ConnectionString);
SqlCommand comm =
new
SqlCommand();
comm.CommandText =
"del_user"
;
//this stored procedure and has an ID called UserId
comm.CommandType = CommandType.StoredProcedure;
//Pass the value here
comm.Parameters.AddWithValue(
"@UserId"
, idvalue);
comm.Connection = conn;
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
}
catch
(Exception ex)
{
Lbinfo.Text = ex.Message;
Lbinfo.ForeColor = System.Drawing.Color.Red;
}
}
Stored Procedure code
CREATE
PROCEDURE
[dbo].[del_user]
AS
DECLARE
@UserId uniqueidentifier
SET
@UserId =
'THE GUID OF THE USER HERE'
DELETE
FROM
Profiles
WHERE
UserId = @UserId
DELETE
FROM
UsersInRoles
WHERE
UserId = @UserId
DELETE
FROM
Memberships
WHERE
UserId = @UserId
DELETE
FROM
Users
WHERE
UserId = @UserId
DELETE
FROM
Staff
WHERE
UserId = @UserId
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
Reply
Answers (
5
)
How to create label printing in SSRS
Am getting error while connecting by db with visual studio 2019