Don't know how to handle C# null values in form controls!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jakeesgirl
    New Member
    • Oct 2007
    • 17

    Don't know how to handle C# null values in form controls!!

    I'm developing a C# application in Visual Studio that have forms which insert values into an sql database. I am allowing null values in the database. However, I have some combo boxes on the forms that give me an error when I insert the form values into the database. It allows me to have null values in the textboxes, but not the combo boxes. How can I fix this problem?
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by jakeesgirl
    I'm developing a C# application in Visual Studio that have forms which insert values into an sql database. I am allowing null values in the database. However, I have some combo boxes on the forms that give me an error when I insert the form values into the database. It allows me to have null values in the textboxes, but not the combo boxes. How can I fix this problem?
    How are you filling your combo boxes?

    If you are looping through your DataSet to do so, you should check if the value is null...if it is, don't add it.

    -Frinny

    Comment

    • jakeesgirl
      New Member
      • Oct 2007
      • 17

      #3
      Originally posted by Frinavale
      How are you filling your combo boxes?

      If you are looping through your DataSet to do so, you should check if the value is null...if it is, don't add it.

      -Frinny

      That's going to be a lot of extra code. Is there a simpler way to do it? Or no?

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by jakeesgirl
        That's going to be a lot of extra code. Is there a simpler way to do it? Or no?
        No... sorry.

        -Frinny

        Comment

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

          #5
          since its a combo box , i presume you would only be assigning the items from one specific field of your retrieved table.

          Check the row.IsMyFieldNa me_Null() == true

          seems like abt 4 lines of code to me....(2 lines for each bracket)

          cheers

          Comment

          • jakeesgirl
            New Member
            • Oct 2007
            • 17

            #6
            Originally posted by Shashi Sadasivan
            since its a combo box , i presume you would only be assigning the items from one specific field of your retrieved table.

            Check the row.IsMyFieldNa me_Null() == true

            seems like abt 4 lines of code to me....(2 lines for each bracket)

            cheers

            My problem is, I have about 8 combo boxes on this form, and I would have to re-write a new sql insert statement for each possibility of nulls in the combo boxes. For example, if I have 3 combo boxes (box1, box2, box3), I would have these possibilities of insert statements:

            insert into table (column1, column2, column3) values (box1, box2, box3)
            insert into table (column1, column2) values (box1, box2)
            insert into table (column1) values (box1)
            insert into table (column2) values (box2)
            insert into table (column3) values (box3)
            insert into table (column2, column3) values (box2, box3)
            insert into table (column1, column3) values (box1, box3)

            With about 8 combo boxes, that adds up to a lot of insert statements. Is there any better way to do this?

            Comment

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

              #7
              Sorry for that.
              I thought you were quering about how not to allow Null values from being populated in a combo box.

              for this case, if you get a field value as null (Which has to inserted into your combo box) insert a space or empty text field into your combobox.

              and this way you can get one insert statement to do the job for you

              cheers

              Comment

              • jakeesgirl
                New Member
                • Oct 2007
                • 17

                #8
                Originally posted by Shashi Sadasivan
                Sorry for that.
                I thought you were quering about how not to allow Null values from being populated in a combo box.

                for this case, if you get a field value as null (Which has to inserted into your combo box) insert a space or empty text field into your combobox.

                and this way you can get one insert statement to do the job for you

                cheers

                With this same form, I want the user to be able to search the database. How would I handle select statements in this case? They have all the combo boxes available, but they just enter in the information they have. So the select statement is going to depend on which combo boxes they make a selection. What is the best way to write my code? The way I'm imagining the code right now is really long and tedious.

                Comment

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

                  #9
                  If you can dynamically create your SQL statements then for each text/combo box whose text.Length > 0 , you can add its fields to the SQL statement using the "AND" operator.

                  cheers

                  Comment

                  • jakeesgirl
                    New Member
                    • Oct 2007
                    • 17

                    #10
                    Originally posted by Shashi Sadasivan
                    If you can dynamically create your SQL statements then for each text/combo box whose text.Length > 0 , you can add its fields to the SQL statement using the "AND" operator.

                    cheers

                    Awesome. Thanks so much for your help!!!!!!!!!

                    Comment

                    Working...