locked
Can anyone help out a newbie? RRS feed

  • Question

  • User766850287 posted

    I am trying another go at ASP.net and C#, and I have run into a problem.

    I am making a very simple CRUD system, showing delays of trains. Everything was working well until I got to the UPDATE part.

    I am getting a "Input string was not in a correct format" error when trying to update my data. I have looked at posts with similar issues, but I could not really relate the solutions to my project.

                <asp:GridView ID="gvDisruptions" runat="server" AutoGenerateColumns="false">
    
                    <Columns>
                        <asp:BoundField Datafield="trainID" HeaderText="trainID" />
                        <asp:BoundField Datafield="delay" HeaderText="delay" />
                        <asp:BoundField Datafield="cancellation" HeaderText="cancellation" />
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="linkView" runat="server" CommandArgument='<% Eval("routeID") %>' OnClick="link_OnClick">View</asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
    
                </asp:GridView>
    protected void link_OnClick(object sender,EventArgs e)
            {
                int routeID = Convert.ToInt32((sender as LinkButton).CommandArgument);
                if (sqlCon.State == ConnectionState.Closed)
                    sqlCon.Open();
                SqlDataAdapter sqlData = new SqlDataAdapter("DisruptionsViewByID", sqlCon);
                sqlData.SelectCommand.CommandType = CommandType.StoredProcedure;
                sqlData.SelectCommand.Parameters.AddWithValue("@routeID", routeID);
                DataTable dtbl = new DataTable();
                sqlData.Fill(dtbl);
                sqlCon.Close();
                hfrouteID.Value = routeID.ToString();
                txtTrain.Text = dtbl.Rows[0]["Train"].ToString();
                txtDelay.Text = dtbl.Rows[0]["Delay"].ToString();
                txtCancellation.Text = dtbl.Rows[0]["Cancellation"].ToString();
                btnSave.Text = "Update";
                btnDelete.Enabled = true;
            }

    The problem is this line: int routeID = Convert.ToInt32((sender as LinkButton).CommandArgument);


    Monday, November 18, 2019 9:31 PM

All replies

  • User303363814 posted

    Do you know how to use breakpoints?

    Put a breakpoint on the line that is giving you the error. (Click in the left hand column in Visual Studio and you will get a red/brown dot and the line will be highlighted In the same colour).

    Now run your program with F5.  After you click the button the execution should stop on the line with the breakpoints.  It will be highlighted in yellow.

    Hold your mouse over "CommandArgument" and you should see what the command argument is.  (If you don't see it hen use the "local" window to find the "sender" parameter and expand it to find the value of the CommandArgument.

    What is the value of "CommandArgument"?  Is it what you expect (which would seem to be an Id of some kind)?  What do you expect?  What do you see?

    Monday, November 18, 2019 9:45 PM
  • User-1780421697 posted

    Please have a look, its good fro getting started and complete example with code:

    https://www.c-sharpcorner.com/UploadFile/4aae29/complete-crud-operations-in-Asp-Net/

    Tuesday, November 19, 2019 11:51 AM
  • User61956409 posted

    Hi McQvist,

    McQvist

    "Input string was not in a correct format" error

    <ItemTemplate>
       <asp:LinkButton ID="linkView" runat="server" CommandArgument='<% Eval("routeID") %>' OnClick="link_OnClick">View</asp:LinkButton>
    </ItemTemplate>

    The problem is this line: int routeID = Convert.ToInt32((sender as LinkButton).CommandArgument);

    If you debug your code, you can find the actual value of (sender as LinkButton).CommandArgument, it would be "<% Eval("routeID") %>" instead of the value of routeID field. 

    You can modify the code like below and check if it can help get expected value from CommandArgument.

    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="linkView" runat="server" CommandArgument='<%#Eval("routeID") %>' OnClick="link_OnClick">View</asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>

    With Regards,

    Fei Han

    Thursday, November 21, 2019 8:51 AM