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


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 »

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 setting up a new Windows 7 computer with IIS 7.5 and Visual Studio 2010, I tried to start my ASP.NET 4.0 website using the Local IIS web server. However, right off the bat I was hit with the following IIS error message: HTTP Error 500.21 - Internal...  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 »

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 »

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 »