Compiler Error Message: CS1026: ) expected - C# .Net, fine in VB .Net
Hey everyone, I'm a complete newbie to ASP .NET programming. I'm trying to get a simple script running which takes string input from two text boxes, converts them to integers and checks that the process happened succesfully and then adds the two and inserts into the text box in the page.
I can get it working in VB.Net, but the reason I am learning ASP.Net is to make an application in work, which must use C#.Net. Can someone help me as to why this works in VB and not C#? The code is below and the error message is: Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1026: ) expected
Source Error:
Line 1: <%@ Page Language="C#" %>
Line 2:
Line 3: Sub btnConvert_Click(sender As Object, e As EventArgs)
Line 4: Try
Line 5: lblToInt1.Text = cint(txtValue1.Text)
<%@ Page Language="C#" %>
<script runat="server">
Sub btnConvert_Click(sender As Object, e As EventArgs)
Try
lblToInt1.Text = cint(txtValue1.Text)
Catch
lblToInt1.Text = "Could not convert to Integer"
End Try
Try
lblToInt2.Text = cint(txtValue2.Text)
Catch
lblToInt2.Text = "Could not convert to Integer"
End Try
lblToInt3.Text = cint(txtValue1.Text)+cint(txtValue2.Text)
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
Text Value 1:
<asp:TextBox id="txtValue1" runat="server"></asp:TextBox>
</p>
<p>
Text Value 2:
<asp:TextBox id="txtValue2" runat="server"></asp:TextBox>
<asp:Button id="btnConvert" onclick="btnConvert_Click" runat="server" Text="Do it!"></asp:Button>
</p>
<p>
Conver开发者_Go百科t to Integer produces 1:
<asp:Label id="lblToInt1" runat="server"></asp:Label>
</p>
<p>
Convert to Integer produces 2:
<asp:Label id="lblToInt2" runat="server"></asp:Label>
</p>
<p>
Total of your 2 numbers:
<asp:Textbox id="lblToInt3" runat="server"></asp:Textbox>
</p>
</form>
</body>
</html>
You just said that the page will use C# as a language, but the code is still in VB.
The equivalent C# code will be something like:
void btnConvert_Click(object sender, EventArgs e)
{
try
{
lblToInt1.Text = int.Parse(txtValue1.Text).ToString();
}
catch //this could be replaced by a single call to int.TryParse
{
lblToInt1.Text = "Could not convert to Integer";
}
// etc, etc...
}
Find a nice C# / asp.net reference, and start from the provided examples.
Thanks SWeko, I am going to go look at some other C# examples to get used to the C# code as opposed to VB etc. Thank you. If you have any good tutorials for C# it would be helpful. Thanks, Cian.
精彩评论