How to Fix e.CommandArgument 'input string was not in a correct format.' error

by Updated December 15, 2009

Here's one quick fix for the ASP.NET error System.FormatException: Input string was not in a correct format. when you are using a DataList with an asp:Button control and are trying to get the CommandArgument using the DataList_RowCommand.

Keep in mind this just one solution to why you may be getting this error message...

Essentially, my problem was that my asp:Button control had it's Viewstate set to false (EnableViewState="false"), so when the button was clicked in the DataList, the ItemCommands e.CommandArgument was getting set to null.  So to fix the "Input string was not in a correct format." error message related to the e.CommandArgument all I had to do was to set the button control to EnableViewState="true".  This "Input string was not in a correct format." error may also occur if your DataList's EnableViewState is set to false, so you may want to check that as well.

Here's what the Button control in my DataList looks like when getting the CommandArgument successfully:

<asp:Button ID="SubmitButton" runat="server" CommandName="SubmitComment" EnableViewState="true" CommandArgument='<%# Eval("ArticleId") %>' Text="Save Comment" />

Below is a sample DataList_ItemCommand with a simple check for a null CommandArgument:


protected void CommentsDataList_ItemCommand(object source, DataListCommandEventArgs e)
 if (e.CommandName == "SubmitComment")

                    if (Page.IsValid)

                       // Int64 articleId = Convert.ToInt64(e.CommandArgument);

   // Test for null
   string strArticleId = e.CommandArgument.ToString();
                        if (string.IsNullOrEmpty(e.CommandArgument.ToString()))
                            Response.Write("ArticleId = " + "null articleid" + "
"); } else { Response.Write("ArticleId = " + strArticleId + "
"); } } } }


Add your comment

by Anonymous - Already have an account? Login now!
Your Name:  

Enter the text you see in the image below
What do you see?
Can't read the image? View a new one.
Your comment will appear after being approved.

Related Posts

SQL Server Reporting Services can be a pain in the arse to set up correctly for the first time. Even after you've got things running correctly, you can sometimes run into issues, which is exactly what happened to me recently. I had setup my local...  more »

I recently ran into the following error while trying to get Elmah to work / complie with my Web Application Project in Visual Studio 2010: Could not load file or assembly 'file:///C:\Users\DOUGDELL2\Documents\Visual Studio...  more »

Here's how you can UrlEncode the plus sign (+) in a URL querystring in ASP.NET and then retrieve the plus symbol after UrlDecoding the string. In this example, I will do a postback and redirect the Server.UrlEncoded string to another page. First we will...  more »

Here's how to install Internet Information Services (IIS7) on a Windows 7 (or Vista) computer so that ASP.NET websites will run on the IIS7 web server. First, you will want to make sure that you are signed into an account with Administrator access on your...  more »

Here's how you can find the text of a particular TextBox witnin an ASP.NET GridView row and then Select and Copy the text to Clipboard with Javascript. First put the following javascript in the HTML of your page (either in HEAD of your html page or...  more »

Adding a CSS border to an ASP.NET Image control was a mystery to me for the longest time. While you could easily use an html image and add the runat="server" to it and then add CSS, I really wanted to use an asp:Image control along with a CSS border....  more »

I recently moved my sites over to a new web server that has "Windows Server 2008 R2" installed. The older server had just been running "Windows Server 2008". After moving my sites over I discovered that my AJAX Toolkit AutoComplete functionality had...  more »

After running a ASP.NET website on IIS 7.5 for the first time on a Windows 7 computer, I was faced with the following error message: Login failed for user 'IIS APPPOOL\ASP.NET v4.0'. Description: An unhandled exception occurred during the execution of the...  more »