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. save null to sql using Linq to Sql

save null to sql using Linq to Sql

Scheduled Pinned Locked Moved C#
csharpdatabaselinqquestion
7 Posts 4 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.
  • M Offline
    M Offline
    mutafa81
    wrote on last edited by
    #1

    hi have following table student id name school_ID 1 john 5 2 sara 2 3 luije 4 school id name 1 area1 2 area2 3 area3 4 area4 5 area5 need to save "null" to school_ID with student ID(2)..?? thanks in advance

    M 1 Reply Last reply
    0
    • M mutafa81

      hi have following table student id name school_ID 1 john 5 2 sara 2 3 luije 4 school id name 1 area1 2 area2 3 area3 4 area4 5 area5 need to save "null" to school_ID with student ID(2)..?? thanks in advance

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      sounds like homework to me - what have you tried so far

      Never underestimate the power of human stupidity RAH

      M 1 Reply Last reply
      0
      • M Mycroft Holmes

        sounds like homework to me - what have you tried so far

        Never underestimate the power of human stupidity RAH

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

        var q = from d in sdc.student where d.school_ID == 4 select d; q.school_ID = null;

        L M 2 Replies Last reply
        0
        • M mutafa81

          var q = from d in sdc.student where d.school_ID == 4 select d; q.school_ID = null;

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          You cant make the primary key NULL!

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 beta 1 - out now!
          ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

          1 Reply Last reply
          0
          • M mutafa81

            var q = from d in sdc.student where d.school_ID == 4 select d; q.school_ID = null;

            M Offline
            M Offline
            Mirko1980
            wrote on last edited by
            #5

            In your code, q is not a database row, but the query itself. Also, you are not selecting on student id 2. You should do something like this:

            //query for student with id = 2
            var q = from d in sdc.student
            where d.id == 2
            select d;

            //getting the first result or null if there is no student
            var row = q.FirstOrDefault();

            //if there is a result, we update it
            if (row != null)
            {
            row.school_ID = null;
            }

            //saving changes to DB
            sdc.SubmitChanges()

            M 1 Reply Last reply
            0
            • M Mirko1980

              In your code, q is not a database row, but the query itself. Also, you are not selecting on student id 2. You should do something like this:

              //query for student with id = 2
              var q = from d in sdc.student
              where d.id == 2
              select d;

              //getting the first result or null if there is no student
              var row = q.FirstOrDefault();

              //if there is a result, we update it
              if (row != null)
              {
              row.school_ID = null;
              }

              //saving changes to DB
              sdc.SubmitChanges()

              M Offline
              M Offline
              mutafa81
              wrote on last edited by
              #6

              ok, got it solve but it is very straing this not work: -------

              var q = (from d in sdc.student
              where d.id == 2
              select d).FirstorDefault();

              try {if (q != null) q.school_ID = null;} cathc{ }

              ------- following works:

              int k = 0;
              var q = (from d in sdc.student
              where d.id == 2
              select d).FirstorDefault();
              // make exception Division by constant zero
              try {if (q != null) k = 1/k;} catch { q.school_ID = null;}

              --------------------------- Really don't know why when write this inside catch{} it works if write outside not work Pleae check Thanks

              M 1 Reply Last reply
              0
              • M mutafa81

                ok, got it solve but it is very straing this not work: -------

                var q = (from d in sdc.student
                where d.id == 2
                select d).FirstorDefault();

                try {if (q != null) q.school_ID = null;} cathc{ }

                ------- following works:

                int k = 0;
                var q = (from d in sdc.student
                where d.id == 2
                select d).FirstorDefault();
                // make exception Division by constant zero
                try {if (q != null) k = 1/k;} catch { q.school_ID = null;}

                --------------------------- Really don't know why when write this inside catch{} it works if write outside not work Pleae check Thanks

                M Offline
                M Offline
                Mirko1980
                wrote on last edited by
                #7

                I don't see why this should not work. What you mean by not work? Gives you an error, does nothing or what?

                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