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. Web Development
  3. ASP.NET
  4. Set parameter value to "Insert Parameters"

Set parameter value to "Insert Parameters"

Scheduled Pinned Locked Moved ASP.NET
helpquestiondatabasewpfwcf
4 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.
  • Q Offline
    Q Offline
    Q_Quek
    wrote on last edited by
    #1

    Hi, I am using objectdatasource as datasource in page. I use the "File Upload" control to get the image and save to database. Within the database table, there are a lot of datafield. I got the problem of setting the parameter value for the image. I use three datafield to store the image relevant data. Datafield DataType 1. imgName (nvarchar 50) 2. imgData (Image) 3. imgType (nvarchar 50) Try Dim imgName As String Dim imgContentType As String Dim imgLen As Int32 Dim imgbin() As Byte If Me.FileUpload.HasFile = True Then If Me.FileUpload.PostedFile.FileName.Trim.Length > 0 And Me.FileUpload.PostedFile.ContentLength > 0 Then Dim imgStream As Stream = Me.FileUpload.PostedFile.InputStream() imgLen = Me.FileUpload.PostedFile.ContentLength imgContentType = Me.FileUpload.PostedFile.ContentType imgName = Me.FileUpload.PostedFile.FileName.Substring(Me.FileUpload.PostedFile.FileName.LastIndexOf("\") + 1) Dim imgBinaryData(imgLen) As Byte 'Dim n As Int32 = imgStream.Read(imgBinaryData, 0, imgLen) imgbin = imgBinaryData ObjectDataSource.InsertParameters("imgName") = imgName ObjectDataSource.InsertParameters("imgData") = imgbin ObjectDataSource.InsertParameters("imgType") = imgContentType ObjectDataSource.Insert() Question 1: How can I set the "value" to the parameter? 2. Do I make mistake to the image field, since declare in database is "Image" datatype and use "Byte" in codebehind? 3: For next time if user want to retrieve the image, can it be "restore" as "image file"? 4. Can I use the "File Upload" control in "DetailsView" with objectdatasource? Because I have try to do it, but unsuccessful. Since I use the "Template Field" and drag the "File Upload" control to the "InsertItemTemplate", but there is no bound property of "text" which to bound to the "field binding". So, is this possible to use "file upload" control in DetailsView?? If so, how to do It. 5. With the approach in question 4, the DetailsView was include in the "atlas:UpdatePanel", will this make error? Appreciate for those giving solution and help. Calvin ****

    M 1 Reply Last reply
    0
    • Q Q_Quek

      Hi, I am using objectdatasource as datasource in page. I use the "File Upload" control to get the image and save to database. Within the database table, there are a lot of datafield. I got the problem of setting the parameter value for the image. I use three datafield to store the image relevant data. Datafield DataType 1. imgName (nvarchar 50) 2. imgData (Image) 3. imgType (nvarchar 50) Try Dim imgName As String Dim imgContentType As String Dim imgLen As Int32 Dim imgbin() As Byte If Me.FileUpload.HasFile = True Then If Me.FileUpload.PostedFile.FileName.Trim.Length > 0 And Me.FileUpload.PostedFile.ContentLength > 0 Then Dim imgStream As Stream = Me.FileUpload.PostedFile.InputStream() imgLen = Me.FileUpload.PostedFile.ContentLength imgContentType = Me.FileUpload.PostedFile.ContentType imgName = Me.FileUpload.PostedFile.FileName.Substring(Me.FileUpload.PostedFile.FileName.LastIndexOf("\") + 1) Dim imgBinaryData(imgLen) As Byte 'Dim n As Int32 = imgStream.Read(imgBinaryData, 0, imgLen) imgbin = imgBinaryData ObjectDataSource.InsertParameters("imgName") = imgName ObjectDataSource.InsertParameters("imgData") = imgbin ObjectDataSource.InsertParameters("imgType") = imgContentType ObjectDataSource.Insert() Question 1: How can I set the "value" to the parameter? 2. Do I make mistake to the image field, since declare in database is "Image" datatype and use "Byte" in codebehind? 3: For next time if user want to retrieve the image, can it be "restore" as "image file"? 4. Can I use the "File Upload" control in "DetailsView" with objectdatasource? Because I have try to do it, but unsuccessful. Since I use the "Template Field" and drag the "File Upload" control to the "InsertItemTemplate", but there is no bound property of "text" which to bound to the "field binding". So, is this possible to use "file upload" control in DetailsView?? If so, how to do It. 5. With the approach in question 4, the DetailsView was include in the "atlas:UpdatePanel", will this make error? Appreciate for those giving solution and help. Calvin ****

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Q_Quek wrote:

      1: How can I set the "value" to the parameter?

      You can set the value in the Selecting event handler of the ObjectDataSource control.

      Q_Quek wrote:

      2. Do I make mistake to the image field, since declare in database is "Image" datatype and use "Byte" in codebehind?

      No.

      Q_Quek wrote:

      3: For next time if user want to retrieve the image, can it be "restore" as "image file"?

      Yes. You simply create a seperate web page or custom http handler for showing up an image store in DB, you can search for the sample code out there or on this site.

      Q_Quek wrote:

      4. Can I use the "File Upload" control in "DetailsView" with objectdatasource? Because I have try to do it, but unsuccessful. Since I use the "Template Field" and drag the "File Upload" control to the "InsertItemTemplate", but there is no bound property of "text" which to bound to the "field binding". So, is this possible to use "file upload" control in DetailsView?? If so, how to do It.

      Yes, you can. You can define a select parameter for the ObjectDataSource control like this:

      <asp:Parameter Name="imgData" Type="Object" />

      In the Selecting event handler of the ObjectDataSource, you can get the binary data of the uploaded image from the FileUpload control, then set it to the select parameter in the InputParameters collection:

      protected void ObjectDataSource1\_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
      {
      
          FileUpload upload = DetailsView1.FindControl("FileUpload") as FileUpload;
                           
          using (BinaryReader reader = new BinaryReader(upload.PostedFile.InputStream))
          {
              byte\[\] bytes = new byte\[upload.PostedFile.ContentLength\];
              reader.Read(bytes, 0, bytes.Length);
              e.InputParameters\["imgData"\] = bytes;
          }
      }
      

      Q_Quek wrote:

      5. With the approach in question 4, the DetailsView was include in the "atlas:UpdatePanel", will this make error?

      Yes, it may raise the error since the file does not get uploaded when you make an out-of-band request with Atlas, you need to submit the form to upload the image.

      Q 1 Reply Last reply
      0
      • M minhpc_bk

        Q_Quek wrote:

        1: How can I set the "value" to the parameter?

        You can set the value in the Selecting event handler of the ObjectDataSource control.

        Q_Quek wrote:

        2. Do I make mistake to the image field, since declare in database is "Image" datatype and use "Byte" in codebehind?

        No.

        Q_Quek wrote:

        3: For next time if user want to retrieve the image, can it be "restore" as "image file"?

        Yes. You simply create a seperate web page or custom http handler for showing up an image store in DB, you can search for the sample code out there or on this site.

        Q_Quek wrote:

        4. Can I use the "File Upload" control in "DetailsView" with objectdatasource? Because I have try to do it, but unsuccessful. Since I use the "Template Field" and drag the "File Upload" control to the "InsertItemTemplate", but there is no bound property of "text" which to bound to the "field binding". So, is this possible to use "file upload" control in DetailsView?? If so, how to do It.

        Yes, you can. You can define a select parameter for the ObjectDataSource control like this:

        <asp:Parameter Name="imgData" Type="Object" />

        In the Selecting event handler of the ObjectDataSource, you can get the binary data of the uploaded image from the FileUpload control, then set it to the select parameter in the InputParameters collection:

        protected void ObjectDataSource1\_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
        {
        
            FileUpload upload = DetailsView1.FindControl("FileUpload") as FileUpload;
                             
            using (BinaryReader reader = new BinaryReader(upload.PostedFile.InputStream))
            {
                byte\[\] bytes = new byte\[upload.PostedFile.ContentLength\];
                reader.Read(bytes, 0, bytes.Length);
                e.InputParameters\["imgData"\] = bytes;
            }
        }
        

        Q_Quek wrote:

        5. With the approach in question 4, the DetailsView was include in the "atlas:UpdatePanel", will this make error?

        Yes, it may raise the error since the file does not get uploaded when you make an out-of-band request with Atlas, you need to submit the form to upload the image.

        Q Offline
        Q Offline
        Q_Quek
        wrote on last edited by
        #3

        Thank you for answer my question with the solution. That's works. However, I get System.Byte[] :confused:from the gridview of ImageField or templatefield. May I know what wrong to the gridview? ****

        M 1 Reply Last reply
        0
        • Q Q_Quek

          Thank you for answer my question with the solution. That's works. However, I get System.Byte[] :confused:from the gridview of ImageField or templatefield. May I know what wrong to the gridview? ****

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          Q_Quek wrote:

          However, I get System.Byte[] from the gridview of ImageField or templatefield. May I know what wrong to the gridview?

          I'm not sure what you mean this. How do you get the byte[] from the ImageField or TemplateField of the GridView control? Do you mean you get it when you use the GridView to display the images stored in DB?

          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