Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. could everyone show me the way to change CType in VB.NET to C#?

could everyone show me the way to change CType in VB.NET to C#?

Scheduled Pinned Locked Moved C#
csharpsysadminquestion
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    largs
    wrote on last edited by
    #1

    Hi,i can not change this statement in VB.NET to C#.Could everyone helpme? Dim strCompanyName as String strCompanyName = CType(dgItem.FindControl("lblCompanyName"), Label).Text with "dgItem" is DataGridItem and "lblCompanyName" is the id of a label (server control).I've tried this Label lb=(Label)dgItem.FindControl("lblCompanyName"); Response.Write(lb.Text); but it did not work.Thanks for all replies.

    J G N D C 5 Replies Last reply
    0
    • L largs

      Hi,i can not change this statement in VB.NET to C#.Could everyone helpme? Dim strCompanyName as String strCompanyName = CType(dgItem.FindControl("lblCompanyName"), Label).Text with "dgItem" is DataGridItem and "lblCompanyName" is the id of a label (server control).I've tried this Label lb=(Label)dgItem.FindControl("lblCompanyName"); Response.Write(lb.Text); but it did not work.Thanks for all replies.

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      The code you posted looks correct, do you get any specific error? What do you mean by "but it did not work." Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

      L 1 Reply Last reply
      0
      • L largs

        Hi,i can not change this statement in VB.NET to C#.Could everyone helpme? Dim strCompanyName as String strCompanyName = CType(dgItem.FindControl("lblCompanyName"), Label).Text with "dgItem" is DataGridItem and "lblCompanyName" is the id of a label (server control).I've tried this Label lb=(Label)dgItem.FindControl("lblCompanyName"); Response.Write(lb.Text); but it did not work.Thanks for all replies.

        G Offline
        G Offline
        gnjunge
        wrote on last edited by
        #3

        You can also write: Label lb= dgItem.FindControl("lblCompanyName") as Label; Response.Write(lb.Text); Maybe you have created a class with the name label and it tries to convert it to that class, so just to be sure add the whole namespace to the label.

        1 Reply Last reply
        0
        • L largs

          Hi,i can not change this statement in VB.NET to C#.Could everyone helpme? Dim strCompanyName as String strCompanyName = CType(dgItem.FindControl("lblCompanyName"), Label).Text with "dgItem" is DataGridItem and "lblCompanyName" is the id of a label (server control).I've tried this Label lb=(Label)dgItem.FindControl("lblCompanyName"); Response.Write(lb.Text); but it did not work.Thanks for all replies.

          N Offline
          N Offline
          nsimeonov
          wrote on last edited by
          #4

          Try: Label lb = (System.Web.UI.WebControls.Label)dgItem.FindControl("lblCompanyName"); Response.Write(lb.Text); HTH

          L 1 Reply Last reply
          0
          • J J4amieC

            The code you posted looks correct, do you get any specific error? What do you mean by "but it did not work." Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

            L Offline
            L Offline
            largs
            wrote on last edited by
            #5

            I want to retrieve the text of that label but it does not print the text so i say it didnt work.The syntax is correct but the result is not.Thanks for your reply

            1 Reply Last reply
            0
            • N nsimeonov

              Try: Label lb = (System.Web.UI.WebControls.Label)dgItem.FindControl("lblCompanyName"); Response.Write(lb.Text); HTH

              L Offline
              L Offline
              largs
              wrote on last edited by
              #6

              thanks for ur reply.I've tried ur code but nothing happened however it does not generate any error.It did not print the text of the label.Is there others way?thanks for reading

              N 1 Reply Last reply
              0
              • L largs

                thanks for ur reply.I've tried ur code but nothing happened however it does not generate any error.It did not print the text of the label.Is there others way?thanks for reading

                N Offline
                N Offline
                nsimeonov
                wrote on last edited by
                #7

                Looks to me that it prints the code of the label but it's empty at this time... OR that this code is never actually executed. Try printing out a few non-space characters before and after the label text like: Response.Write("*** "+lb.Text+" ***"); Now if you see some asterisks in the result then you know it's printing empty label. Then you have to figure out when this code is executed and what you actually have on this label. If you can't see any asterisks then obviously this code never gets executed... HTH

                1 Reply Last reply
                0
                • L largs

                  Hi,i can not change this statement in VB.NET to C#.Could everyone helpme? Dim strCompanyName as String strCompanyName = CType(dgItem.FindControl("lblCompanyName"), Label).Text with "dgItem" is DataGridItem and "lblCompanyName" is the id of a label (server control).I've tried this Label lb=(Label)dgItem.FindControl("lblCompanyName"); Response.Write(lb.Text); but it did not work.Thanks for all replies.

                  D Offline
                  D Offline
                  Dave Doknjas
                  wrote on last edited by
                  #8

                  From our Instant C# VB to C# converter: string strCompanyName = null; strCompanyName = ((Label)(dgItem.FindControl("lblCompanyName"))).Text; Note that your original conversion was attempting to cast "dgItem" to a Label. David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C# to C++ converter Instant C++: VB to C++ converter Clear VB: Cleans up VB.NET code

                  1 Reply Last reply
                  0
                  • L largs

                    Hi,i can not change this statement in VB.NET to C#.Could everyone helpme? Dim strCompanyName as String strCompanyName = CType(dgItem.FindControl("lblCompanyName"), Label).Text with "dgItem" is DataGridItem and "lblCompanyName" is the id of a label (server control).I've tried this Label lb=(Label)dgItem.FindControl("lblCompanyName"); Response.Write(lb.Text); but it did not work.Thanks for all replies.

                    C Offline
                    C Offline
                    Craig G Fraser
                    wrote on last edited by
                    #9

                    Are you using masterpages ?? If you are then the controls id will not be "lblCompanyName"...it will be something like "ctl00_Main_lblCompanyName"...which is prefixed by the content placeholders id....so you should use the following code. Label lb=(Label)dgItem.FindControl(this.lblCompanyName.ClientID); Response.Write(lb.Text); This may help..possibly Cheers, Craig ** I'd rather try and fail than fail to try ;) **

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups