1

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);
        }
    }
}

Kindly help me out in this

My SP Method is,

    public static StoredProcedure StoredProcedure(int id)
      {
            SubSonic.StoredProcedure sp = new SubSonic.StoredProcedure("StoredProcedure",    DataService.GetInstance("TEST"), "");
           sp.Command.AddParameter("@Id", id, DbType.Int32);

            return sp;
       }
4
  • 2
    I bet exception is thrown here Convert.ToInt32(ViewState["id"]). Check what is the value of ViewState["id"]
    – oleksii
    May 16, 2013 at 11:02
  • 1
    What is the value of ViewState["id"] at the time the exception is thrown?
    – Oded
    May 16, 2013 at 11:02
  • @ Oleksii & Oded My First Column in the Table is ID and the Second column is MAC address of the System in Varchar format when I convert Viewstate["id"] to string and see it.It is the MAC Addres in the Second Column of the table I want the ID to be passed what can I do???
    – Rajesh
    May 16, 2013 at 11:06
  • post your gridview here where you set the command argument
    – tariq
    May 16, 2013 at 12:08

2 Answers 2

1

if the e.CommandArgument is an empty string you get that kind of exception at the Convert.ToInt32(ViewState["id"])

I will try to changing your code in this way

    try
    {
        int resultID;
        if(Int32.TryParse(e.CommandArgument.ToString().Trim(), out resultID))
        {
             ViewState["id"] = e.CommandArgument.ToString().Trim();
             .....
        }
        else
            // handle the case of not an integer value

UPDATE Seeing the code of StoredProcedure and with the above code in place then the next reason for the failure is in the StoredProcedure that expects a different type of parameter (For example a NVARCHAR instead of an INT)

9
  • @ Steve My First Column in the Table is ID and the Second column is MAC address of the System in Varchar format when I convert Viewstate["id"] to string and see it.It is the MAC Addres in the Second Column of the table I want the ID to be passed what can I do???
    – Rajesh
    May 16, 2013 at 11:10
  • If the ID is really a number then, probably, the error is inside the call to Namespace.SPs.StoredProcedure or the following Execute method. Why don't you divide these calls in separate lines. You will be able to see where the error occurs
    – Steve
    May 16, 2013 at 11:13
  • @ Steve Can You Please Guide me in Dividing the Calls as I am a Novice in Programming
    – Rajesh
    May 16, 2013 at 11:17
  • On one line put var objectReturnedByProc = Namespace.SPs.StoredProcedure(Convert.ToInt32(ViewState["ID"])); and on the second line objectReturnedByProc.Execute(); In this way you separate the calls and you can say which call fails
    – Steve
    May 16, 2013 at 11:32
  • 1
    As it stands now (ID is a real number) there is no reason for the error. Are you sure that the StoredProcedure expects a parameter called @ID of type Int. also put a breakpoint before the conversion and recheck if the ID is really an integer number (And using the code of my answer will no harm your method while giving more robustness to the code)
    – Steve
    May 16, 2013 at 12:25
1

If you using Command Argument to get the ID you need to set it as command argument in the gridview like CommandArgument='<%#Eval("ID")%>'

Or else you can use DaTaKey to Get ID field as below

<asp:gridview id="gridview1" runat="server" datakeynames="ID"
    ..... >
</asp:gridview>

then in Row Command

protected void gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.ToUpper().ToString() == "DELETEREC")
    {
        try
        {
            int index = gridview1.SelectedIndex;

            int ID = Convert.ToInt32(gridview1.DataKeys[index].Value.ToString());

            int r = Namespace.SPs.StoredProcedure(ID ).Execute();
            // do stuff 
        }
        catch (SqlException ex)
        {
            ClientMessaging(ex.Message);
        }
    }
}
2
  • @ Damith when I used datakeynames in this <Anthem:GridView ID="ggvdetail" runat="server" AutoGenerateColumns="False" AutoUpdateAfterCallBack="True" UpdateAfterCallBack="True" Width="100%" OnRowCommand="ggvdetail_RowCommand" DataKeyNames="Id"> it Showed only the Headers of the Grid view While Filling
    – Rajesh
    May 16, 2013 at 11:50
  • @ Damith Thanks for the Response the Error was in my Select command in the Stored Procedure I had Corrected it and it is Working Fine Now...
    – Rajesh
    May 16, 2013 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.