0

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>
            &nbsp;<asp:Button id="btnConvert" onclick="btnConvert_Click" runat="server" Text="Do it!"></asp:Button>
        </p>
        <p>
            Convert 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>
1
  • 1
    I know this may seem obvious to most, but VB is different to C#. You will need to change the code to C# before it will work. You cant change the language for it to work. Commented Jan 24, 2011 at 11:49

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi SWeko, this would make sense alright but I haven't got a clue of the differences between C# and VB code. I will try use your above help to change some code
Oops, I guess I overdosed on Javascript
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.