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


0
0

Add your comment

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

Comment:
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


For a while I wasn't sure how to access GetRouteUrl from an .ashx IHttpHandler page. I wanted to access route url's setup in the Global.asax file to be used in files like rss.ashx, instead of having to hard codes the page URL's in my .ashx pages. Well,...  more »

This is one of those simple web page design things that can drive a web developer absolutely crazy.  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 »

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 »

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 »

A regular expression for validating a image url.  more »

The AjaxFileUpload control that's part of the AJAX Control Toolkit, works great for easily uploading multiple files at once. However, it gets a little tricky if you want to update an UpdatePanel after all the files have finished uploading, especially if...  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 »