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


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

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 »

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 »

Here's how you can add "nofollow" tags to links generated by a Sitemap file that is bound to an ASP.NET Repeater control using a SiteMapDataSource. I'm currently using this technique for the footer links of GotKnowHow.com, so if you View Source of the...  more »

A regular expression for validating a image url.  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 »