Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Here I have tried to write a code for deleting a row from gridview but when I clicked on delete button, I get an error: input string is not in correct format.

Here is my delete button code.
C#
protected void gridItem_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
                                   
            SqlCommand sqlDelete = new SqlCommand("delete from product_detail where Product_Id=@Product_Id", con);
            sqlDelete.Parameters.Add("@Product_Id", SqlDbType.Int).Value = Convert.ToInt32(gridItem.Rows[e.RowIndex].Cells[1].Text.Trim());
            sqlDelete.Connection = con;
            con.Open();
            sqlDelete.ExecuteNonQuery();
            con.Dispose();
            con.Close();
            fillgrid();
            gridItem.DataBind();
        }

Please tell me where am I wrong in this code?
Posted
Updated 28-Sep-10 20:13pm
v2
Comments
Sandeep Mewara 29-Sep-10 2:13am    
Formatted and corrected tags.

I think the probable error is here,
C#
sqlDelete.Parameters.Add("@Product_Id", SqlDbType.Int).Value = Convert.ToInt32(gridItem.Rows[e.RowIndex].Cells[1].Text.Trim());

Have you checked that the Cell value is convertible to Integer ?

or Please specify the line where you are getting error otherwise we can't assume.

Please vote and Accept Answer if it Helped.
 
Share this answer
 
Comments
honeyashu 29-Sep-10 1:48am    
Add '' in your SqlCommand ("delete from product_detail where Product_Id=@Product_Id", con) if your Product ID is of alphanumeric type.
call to .net 29-Sep-10 1:54am    
I am getting error on same line but i am not recognizing because product_id is int field in database and also here i have tried to convert same.
but i am getting error.
Add '' in your SqlCommand ("delete from product_detail where Product_Id=@Product_Id", con) if your Product ID is of alphanumeric type.



It is an Integer in db but while getting from grid what it represents: before assigning it try this

int prod_id = Convert.ToInt32(gridItem.Rows[e.RowIndex].Cells[1].Text.Trim().ToString()); 

sqlDelete.Parameters.Add("@Product_Id", prod_id);


Also put a space in the sqlCommand
SqlCommand sqlDelete = new SqlCommand("delete from product_detail where Product_Id = @Product_Id", con);
 
Share this answer
 
v3
put a breakpoint and check the line getting error..

i prefer using DataKeyNames property of GridView for getting selected row value instead of gridItem.Rows[e.RowIndex].Cells[1].

 
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