Image control is not displaying image using mappath property vb.net - asp.net

I am trying to set an image dynamically. Image control is not displaying the image but it is displaying the alternate text. I'm using VS 2008 with vb.net. I used information from this post to construct the code. During debug the file path is correct.
vb Code:
Dim strImgURL As String = "C:\fldr1\fldr2\Projfldr3\images\emps\" 'local dev path where visual studio solution is located
Dim filename As String = System.IO.Path.Combine(strImgURL, Session("EmpID").ToString() & ".jpg")
If (File.Exists(filename)) Then
imgEmp.ImageUrl = filename
Else
filename = System.IO.Path.Combine(strImgURL, "99999.jpg")
imgEmp.ImageUrl = filename
End If
aspx:
<asp:image id="imgEmp" Runat="server" Height="100px" Width="77px"
AlternateText="Employee's picture" />
Is it wrong to use "IO.Path.combine" when passing to ImageUrl?
UPDATE: Still working on this with lessons learned from this thread and this thread using MapPath property to set the path to images. The mappath should theoretically detect file path to the images folder in whatever environment I'm testing.
The only way I can get an image to show is if I hard-code filename value like below (removing any mappath code and system.io.path.combine() code entirely). btw I am taking a Int session variable, Session("EmpID") converting to string to create the filename property (e.g. 12345) - none of the conversion methods I'm using are effective:
1. Session("EmpID").ToString() (or Convert.ToString(Session("EmpID"))
2. strEmpID =Session("EmpID").ToString() + ".jpg"
strImgURL = "http://server/images/" + strEmpID + ".jpg"

Based on the link you provided, System.IO.Path.Combine automatically adds forward slashes to the strings. Then try to remove the excess forward slash at the end of strImgURL:
Dim strImgURL As String = "C:\fldr1\fldr2\Projfldr3\images\emps"
Hope this will help. :)

Related

Format to allow ASP Classic <% %> tags inside of a FileSystem object Writeline command

I have a script I am building that creates ASP Classic pages from form input.
I am using the file object to create the asp file. It is working as intended, however I am slightly perplexed how to embed ASP CLASSIC code into the string. The open tag works like this:
fname.WriteLine"<% DIM GENERIC_VAR"
This displays in the file properly, however the use of the close tag doesn't seem to want to work. Even my IDE is indicating a formatting issue. Usually in the instances I need to replace a " (double quote) a ' (single quote) will work, but in this case I get compile errors (Unterminated string constant) or the file doesn't create the line as expected. I know about doubling up "" but so far haven't had any luck. Thanks.
Side note as an example, I just need to be able to print this into the line of the file:
fname.WriteLine"%>"
openasp = "<"
openasp = openasp & "% "
closeasp = " %"
closeasp = closeasp & ">"
Set up the ASP tags like above.
Then Wrap them in line as needed for example:
fname.WriteLine openasp & "DIM VAR_NAME" & closeasp

How to convert xls to xlsx in vb.net?

I am using a resource file(abc.xls) in my project.
" IO.File.WriteAllBytes(tempPath, My.Resources.abc) "
But i want to use abc.xlsx instead of abc.xls without changing resourse file.
can I do saveAs? how?
or please give any other solution.
Thanks.......
Not sure what you exactly want to do, but I'll try to answer the second portion of your question.
'Make a path where you want to save the file
Dim Path as String
Path = "C:\Programs\blabla\"
Dim Final As String
Final = Path & "Filename.xlsx"
YourWorkBook.SaveAs(Final)
Or just try searching google for a more detailed explanation. (You've given little no none info, showing very little research made)
Try searching "How to save excel file in vb.net"

server.mappath returning wrong path

Sever.mappath is returning wrong path that is I think it is converting the initial part of the path to lowercase which is the problem;
String path = Server.MapPath("~/UploadImages/");
When I check for the path in my page by storing it in a textbox it returns:
c:\users\dell\documents\visual studio 2013\Projects\OFR\OFR\UploadImages\
instead of
C:\Users\DELL\Documents\Visual Studio 2013\Projects\OFR\OFR\UploadedImages
What can I do to get the proper path?
It is just spell mistake. you have to write like this
String path = Server.MapPath("~/UploadedImages/");
which will give your expected path
C:\Users\DELL\Documents\Visual Studio
2013\Projects\OFR\OFR\UploadedImages
Instead of UploadImages use UploadedImages.

Base64 Encode a ZIP file using Classic ASP and VB Script

I have a zip file, which contains one CSV file.
I need to Base64 encode this zip file to send to eBay (using their API).
I used this website: http://www.opinionatedgeek.com/DotNet/Tools/Base64Encode/ which works nicely, I upload my zip file and it returns a base64 encoded string which eBay likes.
I need to do what this website does, but using Classic ASP and VB Script.
I already have a base64 encode function, from here: http://www.motobit.com/tips/detpg_base64encode/ so I don't need a script for that. This function takes a parameter, so I need to turn my zip file into a string (I think) to pass into this function.
I have tried using ADODB.Stream and the LoadFromFile method, but the string it returns, after base64 encoding, doesn't match that from the opinionated geek website and isn't accepted by eBay.
This is what I've tried:
Dim objStream, strFileText
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1
objStream.Open
objStream.LoadFromFile Server.MapPath("myzipfile.zip")
strFileText = Base64Encode(objStream.Read)
Response.Write strFileText
objStream.Close
Set objStream = Nothing
Can anyone help..?
Thanks!
This is now solved...
I was missing the BinaryToString function between the stream output and the base64 encode.
Now I use:
strFileText = Base64Encode(BinaryToString(objStream.Read))
Where the new function is...
Function BinaryToString(Binary)
Dim I, S
For I = 1 To LenB(Binary)
S = S & Chr(AscB(MidB(Binary, I, 1)))
Next
BinaryToString = S
End Function
The output from this now matches the output from the opinionated geek tool.
Thanks to ulluoink for pointing me in the right direction!

Character Support Issue - How to Translate Higher ASCII Characters to Lower ASCII Characters

So I have an ASP.Net (vb.net) application. It has a textbox and the user is pasting text from Microsoft Word into it. So things like the long dash (charcode 150) are coming through as input. Other examples would be the smart quotes or accented characters. In my app I'm encoding them in xml and passing that to the database as an xml parameter to a sql stored procedure. It gets inserted in the database just as the user entered it.
The problem is the app that reads this data doesn't like these characters. So I need to translate them into the lower ascii (7bit I think) character set. How do I do that? How do I determine what encoding they are in so I can do something like the following. And would just requesting the ASCII equivalent translate them intelligently or do I have to write some code for that?
Also maybe it might be easier to solve this problem in the web page to begin with. When you copy the selection of characters from Word it puts several formats in the clipboard. The straight text one is the one I want. Is there a way to have the html textbox get that text when the user pastes into it? Do I have to set the encoding of the web page somehow?
System.Text.Encoding.ASCII.GetString(System.Text.Encoding.GetEncoding(1251).GetBytes(text))
Code from the app that encodes the input into xml:
Protected Function RequestStringItem( _
ByVal strName As System.String) As System.String
Dim strValue As System.String
strValue = Me.Request.Item(strName)
If Not (strValue Is Nothing) Then
RequestStringItem = strValue.Trim()
Else
RequestStringItem = ""
End If
End Function
' I get the input from the textboxes into an array like this
m_arrInsertDesc(intIndex) = RequestStringItem("txtInsertDesc" & strValue)
m_arrInsertFolder(intIndex) = RequestInt32Item("cboInsertFolder" & strValue)
' create xml file for inserts
strmInsertList = New System.IO.MemoryStream()
wrtInsertList = New System.Xml.XmlTextWriter(strmInsertList, System.Text.Encoding.Unicode)
' start document and add root element
wrtInsertList.WriteStartDocument()
wrtInsertList.WriteStartElement("Root")
' cycle through inserts
For intIndex = 0 To m_intInsertCount - 1
' if there is an insert description
If m_arrInsertDesc(intIndex).Length > 0 Then
' if the insert description is of the appropriate length
If m_arrInsertDesc(intIndex).Length <= 96 Then
' add element to xml
wrtInsertList.WriteStartElement("Insert")
wrtInsertList.WriteAttributeString("insertdesc", m_arrInsertDesc(intIndex))
wrtInsertList.WriteAttributeString("insertfolder", m_arrInsertFolder(intIndex).ToString())
wrtInsertList.WriteEndElement()
' if insert description is too long
Else
m_strError = "ERROR: INSERT DESCRIPTION TOO LONG"
Exit Function
End If
End If
Next
' close root element and document
wrtInsertList.WriteEndElement()
wrtInsertList.WriteEndDocument()
wrtInsertList.Close()
' when I add the xml as a parameter to the stored procedure I do this
cmdAddRequest.Parameters.Add("#insert_list", OdbcType.NText).Value = System.Text.Encoding.Unicode.GetString(strmInsertList.ToArray())
How big is the range of these input characters? 256? (each char fits into a single byte). If that's true, it wouldn't be hard to implement a 256 value lookup table. I haven't toyed with BASIC in years, but basically you'd DIM an array of 256 bytes and fill in the array with translated values, i.e. the 'a'th byte would get 'a' (since it's OK as is) but the 150'th byte would get a hyphen.
This seems to work for long dash to short dash and smart quotes to regular quotes. As my html pages has the following as the content type. But it converts all the accented characters to questions marks. Which is not what the Text version of the clipboard has. So I'm closer, I just think I have the target encoding wrong.
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
System.Text.Encoding.ASCII.GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(m_arrFolderDesc(intIndex)))
Edit: Found the correct target encoding for my purposes which is 1252.
System.Text.Encoding.GetEncoding(1252).GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(m_arrFolderDesc(intIndex)))
If you convert to a non-unicode character set, you will lose some characters in the process. If the legacy app reading the data doesn't need to do any string transformations, you might want to consider using UTF-7, and converting it back once it gets back into the unicode world - this will preserve all special characters.

Resources