2 Replies - 190 Views - Last Post: 04 August 2012 - 03:43 PM Rate Topic: -----

#1 Chris.Bertsch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 12

Cannot Delete Rows

Posted 04 August 2012 - 01:55 PM

Hi,

I'm using similar code for adding and editing records in my app, but when I try to do a delete, I get the following error: "Procedure or function 'usp_Delete_Image' expects parameter '@Image_ID', which was not supplied." Also, if I try to delete the second to the last row in the datagridview, it givese me the ID for the last row...If I try to delete the last row, it gives me the ID for the second to last row...

Here is the stored procedure I'm calling in SQL Server:
ALTER PROCEDURE [dbo].[usp_Delete_Image]
	@Image_ID INT
AS
BEGIN
SET NOCOUNT ON;

    DELETE FROM dbo.Images
    WHERE Image_ID = @Image_ID
END


Here is the code that gets the selected ID from the datagridview and then calls the DataAccess class for deleting the row:
        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            int ImageID = Convert.ToInt32(dgvImages.GetData(dgvImages.Row, 1));

            DataAccess.usp_Delete_Image((int)ImageID);
            LoadGridView();
        }


And heres the DataAccess code that calls the stored procedure:
        public static int usp_Delete_Image(int Image_ID)
        {
            SqlCommand sp = new SqlCommand("usp_Delete_Image", cs);
            sp.Parameters.AddWithValue("@Image_ID", Image_ID);
            if (cs.State == ConnectionState.Closed)
                cs.Open();
                int i = sp.ExecuteNonQuery();
                cs.Close();
                return i;

        }


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot Delete Rows

#2 MrShoes  Icon User is offline

  • D.I.C Regular

Reputation: 186
  • View blog
  • Posts: 303
  • Joined: 13-June 12

Re: Cannot Delete Rows

Posted 04 August 2012 - 02:47 PM

Are you sure the line int ImageID = Convert.ToInt32(dgvImages.GetData(dgvImages.Row, 1)); is returning the value in the ImageID column? I see no column indexing.
Was This Post Helpful? 0
  • +
  • -

#3 Chris.Bertsch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 12

Re: Cannot Delete Rows

Posted 04 August 2012 - 03:43 PM

Hi,

I fixed my deleting issue by doing the following:
        public static int usp_Delete_Image(int Image_ID)
        {
            string exec = "EXEC usp_Delete_Image '" + Image_ID + "'";

            SqlCommand sp = new SqlCommand(exec, cs);
            //sp.Parameters.AddWithValue("@Image_ID", Image_ID);
            if (cs.State == ConnectionState.Closed)
                cs.Open();
            int i = sp.ExecuteNonQuery();
            cs.Close();
            return i;
        }


I'm pretty sure that line should be returning the Image_ID column...If I use:
int ImageID = Convert.ToInt32(flxgrdImages.GetData(flxgrdImages.Row, 1));
int ImageID = Convert.ToInt32(txtbxImageID.Text);

they both return the same value when calling my add/edit code....For some reason when using it on the delete, it always chooses the ID before or after depending on if I select the last or second to last row...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1