VB.NET - how to store and retrive only image file path into db while the original images is stored in disk in vb.net

Asked By muthu kumar on 13-Nov-08 12:21 AM
end of post

re

Web Star replied to muthu kumar on 13-Nov-08 12:42 AM

u need to get file name from database table and simply need to map complete path to that folder which contain physical image file like

u fill the data table from that table contain the image file name

and show image as

<img id = "image1" runat="server" src='" + Server.MapPath(datatable["imageName"].ToString()) + "' </img>

here Server.MapPath map to that file with yr vertual directory if yr image file name is in database and image containing folder is in wwwroot then u specify that folder name also

like

<img id = "image1" runat="server" src='" + Server.MapPath("\Report\" + datatable["imageName"].ToString()) + "' </img>



reply

Binny ch replied to muthu kumar on 13-Nov-08 02:13 AM
if you want to save the path of the image to the database you would (clearly) need to save the stream to disk first and then get the path

how to store and retrive only image file path into db while the original images is stored in disk in vb.net

Perry replied to muthu kumar on 13-Nov-08 02:28 AM
This is how the people stored the images and attachments in the database efficient way. Whenever you find some image to be stored in database given by user in your application, you will just need to read path given by user and stored that image to the central network location using FQDN path like \\domain\\site\\IPorMachineName\\folder\\filename . You might need to create unique file name if it already exists. At last you will need to stored final path \\domain\\site\\IPorMachineName\\folder\\filename into the database.
This Example will surely help
Binny ch replied to muthu kumar on 13-Nov-08 04:17 AM
 Protected Sub SaveBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Try
   
               Dim filename As String = ""
                Dim filecontent As String = ""
              Dim path As String = Server.MapPath("~/PMap/Main/" & PMapFU.FileName)

                Dim allowExtension As String() = {".gif", ".png", ".jpeg", ".jpg", ".bmp"}
              
             
               If PMapFU.HasFile = False Then
                 ' No file uploaded!
                   ImgStatus.Text = "Please first select an image to upload"
                  
               Else
                   Dim fileExtension As String = System.IO.Path.GetExtension(PMapFU.FileName).ToLower
                   Dim i As Integer
                  
                   'Check file type
                  For i = 0 To allowExtension.Length - 1
                       If fileExtension = allowExtension(i) Then
                          
                           ' Save the file in folder
                           PMapFU.PostedFile.SaveAs(path)
                         
                         ' Display the uploaded file's details
                           ImgStatus.Text = String.Format( _
                                 "Uploaded file: {0}<br />", _
                                   PMapFU.FileName)
                      End If
                   Next
               End If
              
               Dim cnn As Data.SqlClient.SqlConnection
               Dim cmd As Data.SqlClient.SqlCommand
               Dim param As Data.SqlClient.SqlParameter
               Dim strSQL As String
               strSQL = "Insert Into MainProcess( ProcessName, ProcessPGU, EntityBelongTo, TeamBelongTo, ProcessOwner, CurrentVersion, VersionDate, FirstIssueDate, PreparedBy, ApprovedBy, Purpose, Description, ProcessMap) Values( @ProcessName, @ProcessPGU, @EntityBelongTo, @TeamBelongTo, @ProcessOwner, @CurrentVersion, @VersionDate, @FirstIssueDate, @PreparedBy, @ApprovedBy, @Purpose, @Description, @ProcessMap)"
 
              Dim connString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\Processes.mdf"";Integrated Security=True;User Instance=True"
               cnn = New Data.SqlClient.SqlConnection(connString)
 
              cmd = New Data.SqlClient.SqlCommand(strSQL, cnn)
 
               param = New Data.SqlClient.SqlParameter("@ProcessName", Data.SqlDbType.NVarChar)
               param.Value = PNameTxt.Text
               cmd.Parameters.Add(param)
 
              param = New Data.SqlClient.SqlParameter("@ProcessPGU", Data.SqlDbType.NVarChar)
               param.Value = PPGUDDL.SelectedValue
               cmd.Parameters.Add(param)
 
               param = New Data.SqlClient.SqlParameter("@EntityBelongTo", Data.SqlDbType.NVarChar)
               param.Value = EntityDDL.SelectedValue
               cmd.Parameters.Add(param)
                  
               param = New Data.SqlClient.SqlParameter("@TeamBelongTo", Data.SqlDbType.NVarChar)
               param.Value = TeamDDL.SelectedValue
               cmd.Parameters.Add(param)
                  
               param = New Data.SqlClient.SqlParameter("@ProcessMap", Data.SqlDbType.VarChar)
               param.Value = "~/PMap/Main/" & PMapFU.FileName
              cmd.Parameters.Add(param)
                  
              param = New Data.SqlClient.SqlParameter("@ProcessOwner", Data.SqlDbType.NVarChar)
               param.Value = POwnerTxt.Text
               cmd.Parameters.Add(param)
                  
               param = New Data.SqlClient.SqlParameter("@CurrentVersion", Data.SqlDbType.Decimal)
               param.Value = CurrentVerTxt.Text
               cmd.Parameters.Add(param)
                  
               param = New Data.SqlClient.SqlParameter("@VersionDate", Data.SqlDbType.DateTime)
               param.Value = VerDateTxt.Text
               cmd.Parameters.Add(param)
                  
               param = New Data.SqlClient.SqlParameter("@FirstIssueDate", Data.SqlDbType.DateTime)
               param.Value = FIDateTxt.Text
               cmd.Parameters.Add(param)
 
               param = New Data.SqlClient.SqlParameter("@PreparedBy", Data.SqlDbType.NVarChar)
              param.Value = PreparedByTxt.Text
               cmd.Parameters.Add(param)
                  
               param = New Data.SqlClient.SqlParameter("@ApprovedBy", Data.SqlDbType.NVarChar)
               param.Value = ApprovedByTxt.Text
               cmd.Parameters.Add(param)
                  
               param = New Data.SqlClient.SqlParameter("@Purpose", Data.SqlDbType.NVarChar)
               param.Value = PurposeTxt.Text
               cmd.Parameters.Add(param)
                 
               param = New Data.SqlClient.SqlParameter("@Description", Data.SqlDbType.NVarChar)
               param.Value = DescTxt.Text
               cmd.Parameters.Add(param)
 
                  
               cnn.Open()
               cmd.ExecuteNonQuery()
              cnn.Close()
        
              SaveMsg.Text = PNameTxt.Text + " is saved!"
              Return
          Catch ex As Exception
              SaveMsg.Text = ex.Message.ToString
          End Try
 
      End Sub

Hope this helps:
save/retrive an image into a table of MS
C_A P replied to muthu kumar on 13-Nov-08 08:27 AM
http://www.shabdar.org/store-save-images-in-sql-server.html
http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=2120808&SiteID=1