0

I need some help. I have a gridview with some info inside from a database and a delete button that should delete the information, by sending 2 parameters to a stored procedure -member ID, that I take from a session and (in my case) activity ID that I'm trying to take from the first cell in the selected row of my grodview. By some reason my second parameter is always "null", empty

Here is the code I'm trying:

    protected void GridViewSignUps_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlCommand cmd = null;
        cmd = conn.CreateCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "DeleteSignUp";

        try
        {
            conn.Open();

            SqlParameter pmMemberId = cmd.Parameters.Add("@MemberId", SqlDbType.Int);
            pmMemberId.Direction = ParameterDirection.Input;
            pmMemberId.Value = Session["UserID"];

            SqlParameter pmActId = cmd.Parameters.Add("@ActivityId", SqlDbType.Int);
            pmActId.Direction = ParameterDirection.Input;
            pmActId.Value = Convert.ToInt32(GridViewSignUps.SelectedRow.Cells[1].Text);

            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            LabelMessage.Text = ex.Message;
        }
        finally
        {
            conn.Close();
            UpdateGridView();
        }
    }

And my stored procedure:

CREATE PROCEDURE [dbo].[DeleteSignUp]
   @MemberId int,
   @ActivityId int
AS
begin
   delete from Sign_Ups 
   where @MemberId = Sign_Ups.Member_number 
     and @ActivityId = Sign_Ups.Activity_Number
end
RETURN

I'm getting

Object reference not set to an instance of an object.

What is the right way to send the ID from the cell as parameter to my procedure?

Thanks in advance :)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Rahul Singh Dec 02 '15 at 14:07
  • I understand what the error means, what I don't understand is what is wrong with my code ... – BlondGirl87 Dec 02 '15 at 14:16
  • Try: delete from Sign_Ups where MemberId = @ MemberId and ActivityId = @ ActivityId...... Here MemberId and ActivityId are column names of your table. – Aayushi Jain Dec 02 '15 at 14:30

1 Answers1

0

To get the ID of the current user accessing your site, you can use

HttpContext.Current.User.Identity.Name

However, this is a string representing the username, not an integer, so you would likely have to adjust your stored procedure to take a character datatype and then query for the integer value you need based on that before doing the delete.

Also, you are attempting to delete on SelectedIndexChanged, which means that you'll be deleting whenever the user selects a different gridview row. That may not be what you want. Instead, you could include a delete link for each row, map your gridview OnRowDeleting property (something like OnRowDeleting="GridViewSignUps_RowDeleting"), and then delete in that procedure. If you choose to have a single delete button outside the grid, you probably want to handle the delete in the click event for that button.

morgb
  • 2,252
  • 2
  • 14
  • 14