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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. C# winform -> SQL server Trouble writing null values

C# winform -> SQL server Trouble writing null values

Scheduled Pinned Locked Moved Database
databasecsharpsql-serversysadminquestion
3 Posts 2 Posters 1 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.
  • M Offline
    M Offline
    Mattzimmerer
    wrote on last edited by
    #1

    I'm no professional, so I am aware that I may be missing out on a better method but here is what I am trying to do.

                int Market\_Value;
                    if (this.MarketValue.Text == "") Market\_Value = -1;
                    else Market\_Value = Int32.Parse(this.MarketValue.Text);
    
                TableAdapter.InsertQuery(Market\_Value == -1 ? Market\_Value : null)
    
                TableAdapter.Update(DataSet);
    

    *lots of code left out This doesn't compile because of the line : Market_Value == -1 ? Market_Value : null, its telling me that I cannot set an int to null Perhaps I could handle this in my InsertQuery? I'm super new to SQL so I cant see how... heres that function:

    INSERT INTO [dbo].[THE DATABASE]
    ([Market Value])

    VALUES (@Market_Value)

    I guess I could make the conditional statement in there somehow... but how? :confused: :sigh: .. need to learn more SQL :laugh: I just dont know, any insight into a better way would be greatly appreciated!

    I 1 Reply Last reply
    0
    • M Mattzimmerer

      I'm no professional, so I am aware that I may be missing out on a better method but here is what I am trying to do.

                  int Market\_Value;
                      if (this.MarketValue.Text == "") Market\_Value = -1;
                      else Market\_Value = Int32.Parse(this.MarketValue.Text);
      
                  TableAdapter.InsertQuery(Market\_Value == -1 ? Market\_Value : null)
      
                  TableAdapter.Update(DataSet);
      

      *lots of code left out This doesn't compile because of the line : Market_Value == -1 ? Market_Value : null, its telling me that I cannot set an int to null Perhaps I could handle this in my InsertQuery? I'm super new to SQL so I cant see how... heres that function:

      INSERT INTO [dbo].[THE DATABASE]
      ([Market Value])

      VALUES (@Market_Value)

      I guess I could make the conditional statement in there somehow... but how? :confused: :sigh: .. need to learn more SQL :laugh: I just dont know, any insight into a better way would be greatly appreciated!

      I Offline
      I Offline
      i j russell
      wrote on last edited by
      #2

      This is a c# issue and has nothing to do with Sql or the database. Your code will not compile because the Conditional Operator (?) is typed by the test values as an int and it cannot convert null to an int. Try changing Market_Value to a nullable int (int?).

      M 1 Reply Last reply
      0
      • I i j russell

        This is a c# issue and has nothing to do with Sql or the database. Your code will not compile because the Conditional Operator (?) is typed by the test values as an int and it cannot convert null to an int. Try changing Market_Value to a nullable int (int?).

        M Offline
        M Offline
        Mattzimmerer
        wrote on last edited by
        #3

        duh.. yea I always seem to hit the wrong forum.. thanks for your help!!! =)

        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