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.
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?
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.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
5 Posts
Can anyone help out a newbie?
Nov 18, 2019 09:31 PM|McQvist|LINK
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.
The problem is this line: int routeID = Convert.ToInt32((sender as LinkButton).CommandArgument);
Participant
1660 Points
952 Posts
Re: Can anyone help out a newbie?
Nov 18, 2019 09:45 PM|PaulTheSmith|LINK
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?
All-Star
53681 Points
24024 Posts
Re: Can anyone help out a newbie?
Nov 18, 2019 09:48 PM|mgebhard|LINK
You want to use the RowCommand event of the GridView not the click event.
https://weblog.west-wind.com/posts/2007/Jun/07/GridView-and-CommandArguments
There's also the GridView reference docs and Google.
https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview?view=netframework-4.8
https://www.aspsnippets.com/Articles/GridView-CRUD-Select-Insert-Edit-Update-Delete-using-Single-Stored-Procedure-in-ASPNet.aspx
Contributor
2096 Points
1040 Posts
Re: Can anyone help out a newbie?
Nov 19, 2019 11:51 AM|Khuram.Shahzad|LINK
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/
All-Star
40565 Points
6233 Posts
Microsoft
Re: Can anyone help out a newbie?
Nov 21, 2019 08:51 AM|Fei Han - MSFT|LINK
Hi McQvist,
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