e.CommandArgument.ToString() error

I get an error with the below code.   The error is:  {"Input string was not in a correct format."}

The error happens on this line: int index = Convert.ToInt32(e.CommandArgument.ToString());

How do I resolve this error?

The aspx page:

    <asp:Content ID="MyMainPage" ContentPlaceHolderID="MMain" Runat="Server">
         <div id="awindow" >
     <asp:Panel ID="Panel1" runat="server" CssClass="main">
        <div style="width: 100%; float: left">    
      <asp:TextBox runat="server"  style="margin-left:5px; float:left;" ID="newauthor" />
    <asp:ImageButton runat="server" ID="addauthor" Height="17px" ToolTip="Add a new author"  ImageUrl="~/siteimages/Addbuttons/add.png" OnClick="AddAuthor_Click"/>
     <asp:TextBox runat="server" ID="txtAuthor" Text='<%# Bind("Author") %>' />
     <asp:Button ID="btnFindAuthor" runat="server" Text="Author Quick Find" CommandName="Select" OnCommand="btnFindAuthor_Click" />
     <ajaxToolkit:AutoCompleteExtender ID="authors" runat="server" DelimiterCharacters="" Enabled="True" ServicePath="~/WebServices/authorsearchdisplay.asmx" ServiceMethod="AutoComplete" TargetControlID="authors" MinimumPrefixLength="3" />             

    <asp:SqlDataSource runat="server" ID="sqlAuthors" ConnectionString="<%$ ConnectionStrings:Libro %>" ProviderName="<%$ ConnectionStrings:Libro.ProviderName %>" 
    SelectCommand="SELECT * FROM Authors" 
    InsertCommand="authors_GetOrCreate" InsertCommandType="StoredProcedure">
        <asp:ControlParameter ControlID="newAuthor" Name="authorame" />

     <asp:GridView runat="server" ID="gvAuthors" DataSourceID="sqlAuthors" DataKeyNames="AuthorID" AutoGenerateColumns="false">
            <asp:HyperLinkField HeaderText="Author Name" DataNavigateUrlFormatString="~/authors.aspx?id={0}" DataNavigateUrlFields="AuthorID" DataTextField="AuthorName" />





Open in new window

My script:

protected void btnFindAuthor_Click(object sender, CommandEventArgs e)
        if (e.CommandName == "Select") 

            // Convert the row index stored in the CommandArgument
            // property to an Integer.
              int index = Convert.ToInt32(e.CommandArgument.ToString()); 

            // Get the last name of the selected author from the appropriate
            // cell in the GridView control.
            GridViewRow selectedRow = gvAuthors.Rows[index];


Open in new window

The error happens on this line:     int index = Convert.ToInt32(e.CommandArgument.ToString());

How do I resolve this error?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
You're not binding a commandArgument:

 <asp:Button ID="btnFindAuthor" runat="server" Text="Author Quick Find" CommandName="Select" OnCommand="btnFindAuthor_Click" CommandArgument=' <%# Bind("AuthorId") >%' />
programmherAuthor Commented:
I modified the tag so the command argument is bound to the AuthorID.

I still got the same error on the same line.
Kyle AbrahamsSenior .Net DeveloperCommented:
do this:

string s = e.CommandArgument.ToString();

s = s;  // Set a break point on this line and inspect S.  Make sure S is an integer.

That error means it's trying to process a value that's not an integer.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

programmherAuthor Commented:
ged325, the good news is that there is no more error.

The bad news is when I use the button nothing happens now.   The author's name I select should  bring that author's name to the top of the grid.
Kyle AbrahamsSenior .Net DeveloperCommented:
From here:

gvAuthors.SelectedRow = gvAuthors.Rows[index];

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
programmherAuthor Commented:
New error on this line:

gvAuthors.SelectedRow = gvAuthors.Rows[index];

The error:

'System.Web.UI.WebControls.GridView.SelectedRow' cannot be assigned to -- it is read only

I will also look at the link you included.
programmherAuthor Commented:
Added this - MaintainScrollPositionOnPostback ="true" - to the top of the page and it resolved the error.  Testing now to make sure it is going to the row/record the user selects.

Many, ,many, MANY thanks to GED!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.