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. Design and Architecture
  4. Empty strings to null: A declarative approach

Empty strings to null: A declarative approach

Scheduled Pinned Locked Moved Design and Architecture
designquestion
7 Posts 3 Posters 2 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.
  • B Offline
    B Offline
    Brady Kelly
    wrote on last edited by
    #1

    Are there any existing mechanisms to achieve this, or do I need to roll my own? I have a small subset of text fields that cause problems when I assign an empty string from the UI to a field, which should be null if not specified.

    L B 2 Replies Last reply
    0
    • B Brady Kelly

      Are there any existing mechanisms to achieve this, or do I need to roll my own? I have a small subset of text fields that cause problems when I assign an empty string from the UI to a field, which should be null if not specified.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Well I agree that this is a design question but since it is so specific we probably need to know the platform and language you are working with, yes?

      Abandon hope all ye who answer here.

      Pete O'Hanlon - the General Discussions forum

      B 1 Reply Last reply
      0
      • L led mike

        Well I agree that this is a design question but since it is so specific we probably need to know the platform and language you are working with, yes?

        Abandon hope all ye who answer here.

        Pete O'Hanlon - the General Discussions forum

        B Offline
        B Offline
        Brady Kelly
        wrote on last edited by
        #3

        Sorry, that was quite an oversight. ASP.NET, in C#. I'm not specifically asking how to do this, just if it is done already by others, and how they do it. Input regarding other areas I would also appreciate.

        L 1 Reply Last reply
        0
        • B Brady Kelly

          Sorry, that was quite an oversight. ASP.NET, in C#. I'm not specifically asking how to do this, just if it is done already by others, and how they do it. Input regarding other areas I would also appreciate.

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Brady Kelly wrote:

          when I assign an empty string from the UI to a field

          So this field is a Database column I guess? I imagine many people today are using Object Relation Mapping on the .NET platform and the tools and/or libraries/frameworks handle that stuff for them. Have you looked at the Castle project, specifically ActiveRecord which is built on NHibernate? Also if you are using 3.5 I have seen lots of positive noise about Linq.

          B 1 Reply Last reply
          0
          • L led mike

            Brady Kelly wrote:

            when I assign an empty string from the UI to a field

            So this field is a Database column I guess? I imagine many people today are using Object Relation Mapping on the .NET platform and the tools and/or libraries/frameworks handle that stuff for them. Have you looked at the Castle project, specifically ActiveRecord which is built on NHibernate? Also if you are using 3.5 I have seen lots of positive noise about Linq.

            B Offline
            B Offline
            Brady Kelly
            wrote on last edited by
            #5

            Thanks Mike, I have planned to look at Linq, as I'm giving the other devs an intro to it next month.

            1 Reply Last reply
            0
            • B Brady Kelly

              Are there any existing mechanisms to achieve this, or do I need to roll my own? I have a small subset of text fields that cause problems when I assign an empty string from the UI to a field, which should be null if not specified.

              B Offline
              B Offline
              Brady Kelly
              wrote on last edited by
              #6

              I've just found one way of doing this with standard ASP.NET. The SqlDataSource Parameter elements all support a ConvertEmptyStringToNull attribute.

              P 1 Reply Last reply
              0
              • B Brady Kelly

                I've just found one way of doing this with standard ASP.NET. The SqlDataSource Parameter elements all support a ConvertEmptyStringToNull attribute.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Brady - If you are reading things in with a datareader, then one way would be to use some code to convert your DB null string to a null string. A simple function would cope with this:

                public string GetString(IDataReader item, string column)
                {
                int ordinal = item.GetOrdinal(column);
                if (item.IsDBNull(ordinal))
                return null;
                return item.GetString(ordinal);
                }

                Deja View - the feeling that you've seen this post before.

                My blog | My articles

                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