Click here to Skip to main content
14,774,648 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When I am Passing the Value of the selected in the Gridview for deletion it throws Input String was not in correct format exception My Code is,

protected void gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.ToUpper().ToString() == "DELETEREC")
    {
        try
        {
            ViewState["id"] = e.CommandArgument.ToString().Trim();
            int r = Namespace.SPs.StoredProcedure(Convert.ToInt32(ViewState["id"])).Execute();//Exception thrown Here
            if (r > 0)
            {
                ClientMessaging("Record Deleted Successfully");
                Clear();
                fillgrid();
            }
        }
        catch (SqlException ex)
        {
            ClientMessaging(ex.Message);
        }
    }
}
Posted
Comments
NarasimhaMurthy 16-May-13 7:19am
   
Whats there in Namespace.SPs.StoredProcedure?
Rajesh Kumar Sowntararajan 16-May-13 7:22am
   
It Calls the Stored Procedure and Connects with SQL Server,Mentioned in the Class File Under the Name Space
ZurdoDev 16-May-13 7:27am
   
Why? All you have to do is put a breakpoint and see what is happening.
[no name] 16-May-13 10:55am
   
perfect suggesion. :)

There can be many problems with your code.

1) ViewState["id"] can be blank as the argument you are passing may be blank.
2) If you are able to get a value for ViewState["id"], it can be non numeric.
3) If it is not blank and is a numeric value, the possible error is in your class file code or there is something wrong at database level.

Check for these things first as we can't tell from this small piece of code what can be wrong.
   
ViewState["id"] = e.CommandArgument.ToString().Trim();
           int r = Namespace.SPs.StoredProcedure(Convert.ToInt32(ViewState["id"])).Execute



Here there may be two issues with the code
1.You are Assigning the Command Argument value to the View sate in string format.
2.Again you are converting the string value to the Int32
The conversion should depend on the type of the parameter in the Stored Procedure Method.


if you can put the Method details it would be helpful.
   
The Problem is only in my Select Command in the Stored Procedure I didn't select the the Value i was Passing so I had Changed the Select Command then added Datakeynames in Gridview and It worked Perfectly

Thanks All for the Suggestions...
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900