[Solved] ASP.Net Error: Operator > cannot be applied to operands of type string and int

kankon
 
on Sep 03, 2021 10:47 PM
Sample_143819.zip
537 Views

hello

i have old code from VB.NET am trying transfer to ASP.NET

its INCREMENT or DECREMENT NUMBER from 0 to 4 max number is 4 

when i try change the code  i get error 

https://i.ibb.co/DLnXHjw/Snag-4804ab1.png

Private Sub Button71_Click(sender As Object, e As EventArgs) Handles Button71.Click
    If EmergencyMonthPointsTextBox1.Text > 0 Then
        EmergencyMonthPointsTextBox1.Text -= 1
        EmergencyTotalPointsTextBox1.Text += 1
    End If
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 03, 2021 11:14 PM
on Sep 06, 2021 06:07 AM

Hi kankon,

1. The TextBox with ID EmergencyTotalPointsTextBox1 not present in the page.

2. You can't apply greater than symbol between string and integer.

3. You can't increment or decrement string value.

You have interger value on both left and right hand side.

Refer below example.

HTML

<asp:TextBox runat="server" ID="EmergencyMonthPointsTextBox1" />
<asp:TextBox runat="server" ID="EmergencyTotalPointsTextBox1" />
<br />
<asp:Button Text="-" runat="server" OnClick="Button71_Click" />
<asp:Button Text="+" runat="server" OnClick="Button70_Click" />

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        EmergencyMonthPointsTextBox1.Text = "1";
        EmergencyTotalPointsTextBox1.Text = "4";
    }
}

protected void Button71_Click(object sender, EventArgs e)
{
    if (Convert.ToInt16(EmergencyMonthPointsTextBox1.Text) > 0)
    {
        EmergencyMonthPointsTextBox1.Text = (Convert.ToInt16(EmergencyMonthPointsTextBox1.Text) - 1).ToString();
        EmergencyTotalPointsTextBox1.Text = (Convert.ToInt16(EmergencyTotalPointsTextBox1.Text) + 1).ToString();
    }
}

protected void Button70_Click(object sender, EventArgs e)
{
    if (Convert.ToInt16(EmergencyTotalPointsTextBox1.Text) > 0)
    {
        EmergencyMonthPointsTextBox1.Text = (Convert.ToInt16(EmergencyMonthPointsTextBox1.Text) + 1).ToString();
        EmergencyTotalPointsTextBox1.Text = (Convert.ToInt16(EmergencyTotalPointsTextBox1.Text) - 1).ToString();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        EmergencyMonthPointsTextBox1.Text = "1"
        EmergencyTotalPointsTextBox1.Text = "4"
    End If
End Sub

Protected Sub Button71_Click(ByVal sender As Object, ByVal e As EventArgs)
    If Convert.ToInt16(EmergencyMonthPointsTextBox1.Text) > 0 Then
        EmergencyMonthPointsTextBox1.Text = (Convert.ToInt16(EmergencyMonthPointsTextBox1.Text) - 1).ToString()
        EmergencyTotalPointsTextBox1.Text = (Convert.ToInt16(EmergencyTotalPointsTextBox1.Text) + 1).ToString()
    End If
End Sub

Protected Sub Button70_Click(ByVal sender As Object, ByVal e As EventArgs)
    If Convert.ToInt16(EmergencyTotalPointsTextBox1.Text) > 0 Then
        EmergencyMonthPointsTextBox1.Text = (Convert.ToInt16(EmergencyMonthPointsTextBox1.Text) + 1).ToString()
        EmergencyTotalPointsTextBox1.Text = (Convert.ToInt16(EmergencyTotalPointsTextBox1.Text) - 1).ToString()
    End If
End Sub

Screenshot