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


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 »

There's a good chance that if you're database driven application is running into the following sql error message that says "String or binary data would be truncated", that the error is being caused by an issue in a SQL statement or in the SQL code of a...  more »

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 »

The new Routing features in ASP.NET 4.0 are pretty awesome. However, one issue I ran into recently was trying to get the fully qualified urls from Page.GetRouteUrl as string urls to be used in emails messages. Unfortunately, I wanted something that worked...  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 »

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 »

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 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 »