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. problem in insert picture to sql

problem in insert picture to sql

Scheduled Pinned Locked Moved C#
databasehelpquestion
10 Posts 5 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.
  • S Offline
    S Offline
    SajjadZare
    wrote on last edited by
    #1

    I use below code for add picture to dataBase but if I need that insert null instead of picture into database the program give me Error (I set allowNull for column in Sql) (it's neccesary becuase maybe user dosen't have picture for insert into database) how can I do that? MemoryStream mstr = new MemoryStream(); pictureBox1.Image.Save(mstr, pictureBox1.Image.RawFormat); byte[]pic= mstr.GetBuffer(); SqlConnection sq=new SqlConnection(...); SqlParameter sp1 = new SqlParameter("@pic", pic); SqlCommand sc = new SqlCommand("insert into Info values(@pic)", sq); sc.Parameters.Add(sp1); sc.ExecuteNonQuery();

    S C A 3 Replies Last reply
    0
    • S SajjadZare

      I use below code for add picture to dataBase but if I need that insert null instead of picture into database the program give me Error (I set allowNull for column in Sql) (it's neccesary becuase maybe user dosen't have picture for insert into database) how can I do that? MemoryStream mstr = new MemoryStream(); pictureBox1.Image.Save(mstr, pictureBox1.Image.RawFormat); byte[]pic= mstr.GetBuffer(); SqlConnection sq=new SqlConnection(...); SqlParameter sp1 = new SqlParameter("@pic", pic); SqlCommand sc = new SqlCommand("insert into Info values(@pic)", sq); sc.Parameters.Add(sp1); sc.ExecuteNonQuery();

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #2

      Hi, could you post the error you receive (message etc.)? Maybe your db-schema doesn't allow null-values for the picture column. Regards Sebastian

      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

      S 1 Reply Last reply
      0
      • S SeMartens

        Hi, could you post the error you receive (message etc.)? Maybe your db-schema doesn't allow null-values for the picture column. Regards Sebastian

        It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

        S Offline
        S Offline
        SajjadZare
        wrote on last edited by
        #3

        I set allowNull for that column

        S 1 Reply Last reply
        0
        • S SajjadZare

          I set allowNull for that column

          S Offline
          S Offline
          SeMartens
          wrote on last edited by
          #4

          And the error you receive is?

          It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

          S 1 Reply Last reply
          0
          • S SeMartens

            And the error you receive is?

            It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

            S Offline
            S Offline
            SajjadZare
            wrote on last edited by
            #5

            Error : parameter @pic, which was not supplied

            S 1 Reply Last reply
            0
            • S SajjadZare

              Error : parameter @pic, which was not supplied

              S Offline
              S Offline
              SeMartens
              wrote on last edited by
              #6

              Try to pass System.DBNull.Value as parameter value when pic1 is null.

              It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

              S 1 Reply Last reply
              0
              • S SeMartens

                Try to pass System.DBNull.Value as parameter value when pic1 is null.

                It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                S Offline
                S Offline
                SajjadZare
                wrote on last edited by
                #7

                Error : Operand type clash: nvarchar is incompatible with image

                P 1 Reply Last reply
                0
                • S SajjadZare

                  Error : Operand type clash: nvarchar is incompatible with image

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

                  There's your problem right there. nvarchar is a text based field, and you are trying to store an image in it - you need to change the column type in the database, or save the image to disk and store the location of the image in this field.

                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                  My blog | My articles | MoXAML PowerToys | Onyx

                  1 Reply Last reply
                  0
                  • S SajjadZare

                    I use below code for add picture to dataBase but if I need that insert null instead of picture into database the program give me Error (I set allowNull for column in Sql) (it's neccesary becuase maybe user dosen't have picture for insert into database) how can I do that? MemoryStream mstr = new MemoryStream(); pictureBox1.Image.Save(mstr, pictureBox1.Image.RawFormat); byte[]pic= mstr.GetBuffer(); SqlConnection sq=new SqlConnection(...); SqlParameter sp1 = new SqlParameter("@pic", pic); SqlCommand sc = new SqlCommand("insert into Info values(@pic)", sq); sc.Parameters.Add(sp1); sc.ExecuteNonQuery();

                    C Offline
                    C Offline
                    carlecomm
                    wrote on last edited by
                    #9

                    hi, try the following code, it may help you. int len = Upload.PostedFile.ContentLength; byte[] pic = new byte[len]; Upload.PostedFile.InputStream.Read (pic, 0, len); SqlConnection sq=new SqlConnection(...); SqlCommand cmd = new SqlCommand ("insert into values(@pic)", sq); cmd.Parameters.Add ("@pic", pic); cmd.ExecuteNonQuery ();

                    1 Reply Last reply
                    0
                    • S SajjadZare

                      I use below code for add picture to dataBase but if I need that insert null instead of picture into database the program give me Error (I set allowNull for column in Sql) (it's neccesary becuase maybe user dosen't have picture for insert into database) how can I do that? MemoryStream mstr = new MemoryStream(); pictureBox1.Image.Save(mstr, pictureBox1.Image.RawFormat); byte[]pic= mstr.GetBuffer(); SqlConnection sq=new SqlConnection(...); SqlParameter sp1 = new SqlParameter("@pic", pic); SqlCommand sc = new SqlCommand("insert into Info values(@pic)", sq); sc.Parameters.Add(sp1); sc.ExecuteNonQuery();

                      A Offline
                      A Offline
                      April Fans
                      wrote on last edited by
                      #10

                      hi, try the following code, it may help you. int len = Upload.PostedFile.ContentLength; byte[] pic = new byte[len]; Upload.PostedFile.InputStream.Read (pic, 0, len); SqlConnection sq=new SqlConnection(...); SqlCommand cmd = new SqlCommand ("insert into values(@pic)", sq); cmd.Parameters.Add ("@pic", pic); cmd.ExecuteNonQuery ();

                      April Comm100 - Leading Live Chat Software Provider

                      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