Converting web control from string to decimal.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BezerkRogue
    New Member
    • Oct 2007
    • 68

    Converting web control from string to decimal.

    I have created some code that finds controls in a datalist that is generated from a SQL query. It takes the control and puts the data into a string. Here is the code.

    Code:
    If c0a IsNot Nothing Then
                        If c0a.[GetType]() Is GetType(TextBox) Then
                            cb = c0a.ToString()
    I need to convert that to a decimal. I am using the following code to do this.

    Code:
     IData.Value = Decimal.Parse(cb)
    I am getting the following error. Input string was not in a correct format.

    What am I missing?
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Have you tried printing what value c0a.ToString() returns?

    Comment

    • BezerkRogue
      New Member
      • Oct 2007
      • 68

      #3
      It is only showing a LabelSystem.Web .UI.WebControls .TextBox. It should be displaying 23.

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        i will presume that c0a is an instance of your webControl you created.

        Override the ToString method in that control to return the text in the textBox

        Comment

        • BezerkRogue
          New Member
          • Oct 2007
          • 68

          #5
          Ok. I am new to this. How do I do that?

          Comment

          • Shashi Sadasivan
            Recognized Expert Top Contributor
            • Aug 2007
            • 1435

            #6
            How To: Override the ToString Method

            Do Post back if this dosent help

            Comment

            • BezerkRogue
              New Member
              • Oct 2007
              • 68

              #7
              It comes up with nothing. The code I have inserted is:

              Code:
              cb = c0a.ToString()
              System.Console.WriteLine(cb)

              Comment

              • Shashi Sadasivan
                Recognized Expert Top Contributor
                • Aug 2007
                • 1435

                #8
                Originally posted by BezerkRogue
                It comes up with nothing. The code I have inserted is:

                Code:
                cb = c0a.ToString()
                System.Console.WriteLine(cb)
                Could you please paste the code of the overridden ToString Method?

                Comment

                • BezerkRogue
                  New Member
                  • Oct 2007
                  • 68

                  #9
                  I am having difficulties with this as I am not using C#. I have tried to duplicate the code but can't. I scrapped it and used the code I inserted earlier.

                  Comment

                  • Shashi Sadasivan
                    Recognized Expert Top Contributor
                    • Aug 2007
                    • 1435

                    #10
                    Sorry I did not realise you were doing this in VB .Net
                    Im not sure about the Vb .Net syntax for overriding methods, But it should be similar to

                    [CODE=vbnet]Public Overrides Function ToString() As String
                    Return Me.textBox1.tex t + "---"
                    End Function[/CODE]

                    Do paste your Vb .Net code. Overriding methods might be a new thing for you but this is exactly what you want.

                    Comment

                    • BezerkRogue
                      New Member
                      • Oct 2007
                      • 68

                      #11
                      Here is the Override code:
                      Code:
                          Public Overrides Function ToString() As String
                              Return Me.ca.text + "---"
                          End Function
                      It get an error that ca is not a member of HR_HR2. I get the same error is I insert the variable for the find control.

                      Comment

                      • Shashi Sadasivan
                        Recognized Expert Top Contributor
                        • Aug 2007
                        • 1435

                        #12
                        Sorry about this,
                        We got deviated from the main topic.

                        Read the post from the beginning and found I was heading in the wrong direction (i was assuming you were creating user controls)

                        this is what I came up with
                        [CODE=vbnet]Dim controlText As String = ""
                        If c0a IsNot Nothing Then
                        If c0a.[GetType]() = GetType(TextBox ) Then
                        Dim tb As TextBox = DirectCast(c0a, TextBox)
                        controlText = tb.Text
                        End If
                        End If[/CODE]

                        Got the vb .net equivalent by converting the foolowing c# code
                        [CODE=cpp]string controlText = "";
                        if(c0a != null)
                        {
                        if(c0a.GetType( ) == typeof(TextBox) )
                        {
                        TextBox tb = (TextBox)c0a;
                        controlText = tb.Text;
                        }
                        }[/CODE]

                        Comment

                        • BezerkRogue
                          New Member
                          • Oct 2007
                          • 68

                          #13
                          It is showing the text I entered into the box. I typed in 23. Could this code resolve the issue that I am having?

                          Comment

                          • Shashi Sadasivan
                            Recognized Expert Top Contributor
                            • Aug 2007
                            • 1435

                            #14
                            Yes implement it in your code. I think this is what will work.
                            Sorry i got into the influnce that it was a custom conrtol

                            Comment

                            • BezerkRogue
                              New Member
                              • Oct 2007
                              • 68

                              #15
                              That worked perfectly. Thanks for the help. I was totally lost there.

                              Comment

                              Working...