.ASHX file need to do a IF ELSE Statement within it - vb.net

I am using an Upload script (http://www.uploadify.com/demos/ ) Converted to VB.NET for uploading multiple images. This script I have been using for years. It is very robust and works well. However, when I am working on it locally, I have to go inside of the ASHX file and comment out the LIVE database and uncomment the Local Database server.
My question is the following: Is there a way to do an IF ELSE statement inside of the ASHX file? I have tried and it errors out.
What I would like to do is something like this.
if Request.ServerVariables("SERVER_NAME")="192.168.2.12" then
'local connection here
else
'live server connection here
end if
This will make it so I can work on the scripts, without having to change this ALL THE TIME... It is right annoying.
Any idea's?

There is an IsLocal property you can read, which will tell you if the current request is being called locally. Assuming you are running within the ProcessRequest of the .ASHX
Dim isLocal As Boolean = context.Request.IsLocal
If isLocal Then
'local connection here
Else
'live server connection here
End If

OK, I figured it out. I used another method that I am using in an aspx.vb file. And I changed it up a little to work within the ASHX file. Works like a charm.
dim ipAdd as string
dim dbName as string
dim UN as string
dim PW as string
dim theLocal as string = Httpcontext.Current.Request.ServerVariables("SERVER_NAME")
if theLocal="192.168.2.12" then
ipAdd = "LOCAL_SERVER_NAME\INSTANCE_NAME"
dbName = "LOCAL_DB_NAME"
UN = "LOCAL_USERNAME"
PW = "LOCAL_PASSWORD"
else
ipAdd = "00.00.00.00"
dbName = "LIVE_DB_NAME"
UN = "SERVER_USERNMAE"
PW = "SERVER_PASSWORD"
end if
Dim cn As New System.Data.SqlClient.SqlConnection("Data Source="+ipAdd+"; Initial Catalog="+dbName+";User ID="+UN+";Password="+PW+";")
cn.Open()
Thanks for the attempted assistance from Malcor.
This is a wrap all.
Wayne

Related

Why GetFiles from a remote folder using Linq is hanging

I'm trying to fetch the files inside a folder ordered by LastWriteTime.
The code is running very fast when accessing to a local path (C:\MyFolder), but is hanging when accessing to a remote path (\\MyServer\MyFolder)
Dim myOrderedList As List(Of String) = (From item In IO.Directory.GetFiles(strFolderSource) _
Let file = New IO.FileInfo(item) _
Order By file.LastWriteTime _
Select item).ToList()
Should this code work? Is not allowed this method to get files from a remote folder?
Which alternative code could I use to get the same result without hanging?
EDITED (2019-01-18 16:32):
Sorry guys, I've tried the proposed solution from Rango, and still the same hang. Finally I created a small logging system to catch the step that caused the problem, and realized that all is a credential problem.
Just before the code I posted I do a NET USE to grant access to the remote computer, and the net use is executed, but for any reason, the GetFiles() fails because of Logon failure: unknown user name or bad password.
So, Could I ensure the credentials with the net use before call the GetFiles()?
Maybe using a pause or something like this?
FULL CODE:
Dim processInfo As New System.Diagnostics.ProcessStartInfo()
processInfo.FileName = "C:\WINDOWS\system32\net"
processInfo.Arguments = "net use \\MyServer\IPC$ ""password"" /USER:Username"
System.Diagnostics.Process.Start(processInfo)
Dim myOrderedList As List(Of String) = (From item In IO.Directory.GetFiles("\\MyServer\g$\MyFolder") _
Let file = New IO.FileInfo(item) _
Order By file.LastWriteTime _
Select item).ToList()
You could try to use DirectoryInfo.EnumerateFiles instead wich has two advantages:
No consecutive security handshakes from the remote server necessary
Streaming the files instead of loading all into memory before you start ordering them
Dim di = new DirectoryInfo(strFolderSource)
Dim files = From fi In di.EnumerateFiles() Order By fi.LastWriteTime Select fi.FullName
Dim myOrderedList As List(Of String) = files.ToList()
Finally solved including a sleep of 5 seconds after the net use and before the GetFiles():
System.Threading.Thread.Sleep(5000)
Thanks for your time, and hope this helps anybody with a similar problem.

SQLite database file can't be opened when placed in network folder

Can someone help me to understand why this works fine...
Dim cs = "Data Source=C:\folder\Livros.sdb;Version=3;"
Dim cn = New System.Data.SQLite.SQLiteConnection(cs)
cn.Open() ' no exception
... while this breaks when opening connection (it is exactly the same file)...
Dim cs = "Data Source=\\NetworkServer\folder\Livros.sdb;Version=3;"
Dim cn = New System.Data.SQLite.SQLiteConnection(cs)
cn.Open() ' exception: {"unable to open database file"}
... and fix it because I need to place database file in network location so I can access it regardless of the computer I run the application?
Thank you very much!
Ok, so by trial and error I found the solution, although I can't quite understand the reason it works:
Dim cs = "Data Source=\\NetworkServer\folder\Livros.sdb;Version=3;"
Dim cn = New System.Data.SQLite.SQLiteConnection(cs)
cn.ParseViaFramework = True ' JUST ADDED THIS STATEMENT
cn.Open() ' no exception
If somebody can explain why .ParseViaFramework = True does the trick, please feel free to comment.
Similar question was asked here.
SQLite: Cannot open network file programmatically, even though worked before
The top answer gives a few more fixes. Linking here as this is the first stackoverflow that came up when I searched. Also I was using a SQLiteConnectionStringBuilder and could not find a way to set the parseViaFramework so the first solution was the one I needed.
Double the leading two backslashes in the file name (e.g. "\\\\network\share\file.db").
Use a mapped drive letter.
Use the SQLiteConnection constructor that takes the parseViaFramework boolean argument and pass 'true' for that argument.

Cant connect to MongoDB by vb.net

I have created a MongoDB account at mlab, but I cant seem to connect to it. It fails with the credentials. I cant seem to figure out, what I does wrong, besides it maybe is the connection string. Problem is, that I got that from Mlab (have added it here, but without the right user and password)
Thanks in advance
Dim connstring As String = "mongodb://USER:PASS#ds021239.mlab.com:22234"
Dim mongo As MongoServer = MongoServer.Create(connstring)
mongo.Connect()
Dim mydatabase As MongoDatabase = mongo.GetDatabase("profundo", SafeMode.True)
Dim collection = mydatabase.GetCollection(Of BsonDocument)("Client")
MsgBox(collection.Count)

SAS VBA connection

I tried to run a SAS code through VBA. I'm running SAS on a server. I have a test SAS program "Program1.sas" which I've kept on the server. Below is the VBA code that I've used. The SAS code is running but the output dataset is just showing the column headers and the rows are empty.Can anyone help me what could be the possible reason.
Sub Form_Load23()
Dim obObjectFactory As New SASObjectManager.ObjectFactory
Dim obObjectKeeper As New SASObjectManager.ObjectKeeper
Dim obServer As New SASObjectManager.ServerDef
Dim obSAS As SAS.Workspace
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
obServer.MachineDNSName = "xyz"
obServer.Protocol = SASObjectManager.Protocols.ProtocolBridge
obServer.Port = 8871
obObjectFactory.LogEnabled = True
Set obSAS = obObjectFactory.CreateObjectByServer("sas", True, obServer,"userid", "password")
obSAS.LanguageService.Submit ("options source2; %include '/abc/AFP/shikhar.gupta1/amg/**Program1.sas**';")
End Sub'
I believe you have to change the security settings in excel. Find the security settings and protected viewing. The remove all the check marks.
This is an issue on the SAS side. It can happen when your user does not have the metadata 'read' permission on the relevant dataset. It can also happen if your program has an error, and options obs=0; is set.
As #vasilij mentions, use proc printto to write your log to a file system and check for issues, also use SMC to check your metadata permissions.

Making a back up of A SQL Server Compact Edition Corrupts the database

On close of an application written in VB, I need to copy the .sdf file, this is the local SQL Server CE database that syncs with the server. Each time we close the app we require a backup to be made.
I am using the file.copy method. First I close the connection to the database then copy the file to a specific location.
Dim conn As SqlCeConnection = New SqlCeConnection(My.Settings.MyClientConnectionString)
conn.Open()
conn.Close()
Dim tpath As String = My.Settings.CertPath
Dim path As String = tpath.Replace("db\", "MyDatabase.sdf")
Dim fs As FileStream = File.Create(path)
fs.Close()
File.Delete("C:\db\backup\MyDatabase.sdf")
File.Copy(path, "C:\db\backup\MyDatabase.sdf")
The problem arises on the line of code:
Dim fs As FileStream = File.Create(path)
The database becomes corrupted and has a size of 0 kb.
Has anyone seen this before? Thanks
Are you calling Create on an existing database file? According to the docs, Create creates or overwrites a file in the specified path. In the code you show, calling Create isn't necessary anyway. Just call File.Copy.