How to I call a variable somewhere else in my code in VB? - vb.net

So, I have the following VB code:
Try
Using verSR As New StreamReader(appDataVersionLoc)
Dim txtVern As String
txtVern = verSR.ReadToEnd()
End Using
Catch ex As Exception
Dim verFile As System.IO.FileStream
verFile = System.IO.File.Create(appDataVersionLoc)
My.Computer.FileSystem.WriteAllText("appDataVersionLoc", "0.0.0.0", True)
MessageBox.Show("Version file missing/corrupt, created a new one.")
End Try
See that variable called "txtVern?" I'm trying to use that somewhere else in my code outside of the Try-End Try block.
Pretty much this:
Try
Using verSR As New StreamReader(appDataVersionLoc)
Dim txtVern As String
txtVern = verSR.ReadToEnd()
End Using
Catch ex As Exception
Dim verFile As System.IO.FileStream
verFile = System.IO.File.Create(appDataVersionLoc)
My.Computer.FileSystem.WriteAllText("appDataVersionLoc", "0.0.0.0", True)
MessageBox.Show("Version file missing/corrupt, created a new one.")
End Try
blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah
Dim currentversion As String = txtVern
Now, I'm a complete VB noob, and I'm getting this error:
'txtVern' is not declared. It may be inaccessible due to its
protection level.
How can I fix this?
Thanks!

Variable declared inside any block is visible only inside that block. You have to move declaration before Try block:
Dim txtVern As String = String.Empty
Try
Using verSR As New StreamReader(appDataVersionLoc)
txtVern = verSR.ReadToEnd()
End Using
Catch ex As Exception
Dim verFile As System.IO.FileStream
verFile = System.IO.File.Create(appDataVersionLoc)
My.Computer.FileSystem.WriteAllText("appDataVersionLoc", "0.0.0.0", True)
MessageBox.Show("Version file missing/corrupt, created a new one.")
End Try
blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah
Dim currentversion As String = txtVern

In declaring your variable txtVern in your try block you are limiting their scope to the try block. Try declaring them at the beginning of your subroutine.

Related

SqlFileStream writing in separate thread not working

Learning and testing using Sql FILESTREAM for a web app. A client uploads a large file form the web page which takes 'X' time and when fully uploaded shows 100% complete. However, very large files also take time for SqlFileStream to write to the file system so I want to spin off a thread to complete that part. The code I've got seems to work fine but no data ends up in the filestream file.
I'm wrapping the initial record creation in it's own transaction scope and using a separate transaction scope in the thread. In the threaded routine I have the appropriate PathName() and TransactionContext but I assume I'm missing something while using a thread.
I've commented out the normal SqlFileStream call which works fine. Can you see anything wrong with what I'm doing here?
Public Function StoreFileStream()
Dim json As New Dictionary(Of String, Object)
Dim parms As New FileStreamThreadParameters
If HttpContext.Current.Request.Files.Count > 0 Then
Dim file As HttpPostedFile = HttpContext.Current.Request.Files(0)
If "contentType" <> String.Empty Then
Dim fs As Stream = file.InputStream
Dim br As New BinaryReader(fs)
Dim noBytes As New Byte()
Try
Dim filePath As String = ""
Dim trxContext As Byte() = {}
Dim baseFileId As Integer
Using trxScope As New TransactionScope
Using dbConn As New SqlConnection(DigsConnStr)
Using dbCmd As New SqlCommand("ADD_FileStreamFile", dbConn)
dbConn.Open()
Using dbRdr As SqlDataReader = dbCmd.ExecuteReader(CommandBehavior.SingleRow)
dbRdr.Read()
If dbRdr.HasRows Then
filePath = dbRdr("Path")
trxContext = dbRdr("TrxContext")
baseFileId = dbRdr("BaseFileID")
End If
dbRdr.Close()
End Using
' Code below writes to file, but trying to offload this to a separate thread so user is not waiting
'Using dest As New SqlFileStream(filePath, trxContext, FileAccess.Write)
' fs.CopyTo(dest, 4096)
' dest.Close()
'End Using
End Using
dbConn.Close()
End Using
trxScope.Complete()
End Using ' transaction commits here, not in line above
parms.baseFileId = baseFileId
parms.fs = New MemoryStream
fs.CopyTo(parms.fs)
Dim fileUpdateThread As New Threading.Thread(Sub()
UpdateFileStreamThreaded(parms)
End Sub)
fileUpdateThread.Start()
json.Add("status", "success")
Catch ex As Exception
Elmah.ErrorSignal.FromCurrentContext().Raise(ex)
json.Add("status", "failure")
json.Add("msg", ex.Message)
json.Add("procedure", System.Reflection.MethodBase.GetCurrentMethod.Name)
End Try
Else
json.Add("status", "failure")
json.Add("msg", "Invalid file type")
json.Add("procedure", System.Reflection.MethodBase.GetCurrentMethod.Name)
End If
End If
Return json
End Function
Public Class FileStreamThreadParameters
Public Property baseFileId As Integer
Public fs As Stream
End Class
Private Sub UpdateFileStreamThreaded(parms As FileStreamThreadParameters)
Dim filePath As String = ""
Dim trxContext As Byte() = {}
Try
Using trxScope As New TransactionScope
Using dbConn As New SqlConnection(DigsConnStr)
Using dbCmd As New SqlCommand("SELECT FileBytes.PathName() 'Path', GET_FILESTREAM_TRANSACTION_CONTEXT() 'TrxContext' FROM FileStreamFile WHERE Id = " & parms.baseFileId, dbConn)
dbConn.Open()
Using dbRdr As SqlDataReader = dbCmd.ExecuteReader(CommandBehavior.SingleRow)
dbRdr.Read()
If dbRdr.HasRows Then
filePath = dbRdr("Path")
trxContext = dbRdr("TrxContext")
End If
dbRdr.Close()
Using dest As New SqlFileStream(filePath, trxContext, FileAccess.Write)
parms.fs.CopyTo(dest, 4096)
dest.Close()
End Using
End Using
End Using
dbConn.Close()
End Using
trxScope.Complete()
End Using
Catch ex As Exception
'Elmah.ErrorSignal.FromCurrentContext().Raise(ex)
End Try
End Sub
Obviously this is a complex issue. I actually got it to work with the code below. However I eventually abandoned using SQL FILESTREAM altogether due to too much complexity in getting it all set up.
This is an existing web application with the sql server on a different box. After I got the filestreaming to work the next hurdle was authentication setup. Filestream requires Integrated Security on your connection string. Even with windows authentication on our Intranet app, I could not get the web app to use the windows credentials when connecting to the database. It always seemed to use the computer name or the app pool service. I tried many many examples I found on the net and here to no avail. Even if I got that to work then I would want to use and Active Directory group over individual logins which looked to be another hurdle.
This app stores documents in a varbinary column so that full text search can be enabled at some point. The issue was with large files which are generally pictures or videos. Since you can't search text on those anyway the strategy now is to store those on the file system and all other searchable docs (.docx, .pptx, etc) will still be stored in the varbinary. I'm actually sad that I could not get filestream to work as it seems like the ideal solution. I'll come back to it some day but it really should not be so frickin complicated. :-(
The code I got working is:
Dim filePath As String = ""
Dim trxContext As Byte() = {}
Dim baseFileId As Integer
Using trxScope As New TransactionScope
Using dbConn As New SqlConnection(DigsFSConnStr)
Using dbCmd As New SqlCommand("NEW_FileStreamBaseFile", dbConn)
dbCmd.CommandType = CommandType.StoredProcedure
dbCmd.Parameters.AddWithValue("#Title", fileDesc)
dbCmd.Parameters.AddWithValue("#Summary", summary)
dbCmd.Parameters.AddWithValue("#Comments", comments)
dbCmd.Parameters.AddWithValue("#FileName", uploadedFileName)
dbCmd.Parameters.AddWithValue("#ContentType", contentType)
dbCmd.Parameters.AddWithValue("#FileExt", ext)
'dbCmd.Parameters.AddWithValue("#FileBytes", noBytes) ' now that were offloading the file byte storage to a thread
dbCmd.Parameters.AddWithValue("#UploadedByResourceID", uploadedByResourceID)
dbCmd.Parameters.AddWithValue("#UploadedByShortName", uploadedByShortName)
dbCmd.Parameters.AddWithValue("#FileAuthor", fileAuthor)
dbCmd.Parameters.AddWithValue("#TagRecID", tagRecID)
dbCmd.Parameters.AddWithValue("#UserID", samAccountName)
dbCmd.Parameters.AddWithValue("#FileDate", fileDate)
dbCmd.Parameters.AddWithValue("#FileType", fileType)
dbCmd.Parameters.AddWithValue("#FileTypeRecID", fileTypeRecId)
' Save to file system too for xod conversion
file.SaveAs(HttpContext.Current.Server.MapPath("~/files/uploaded/") & uploadedFileName)
dbConn.Open()
Using dbRdr As SqlDataReader = dbCmd.ExecuteReader(CommandBehavior.SingleRow)
dbRdr.Read()
If dbRdr.HasRows Then
filePath = dbRdr("Path")
trxContext = dbRdr("TrxContext")
json.Add("baseFileId", dbRdr("BaseFileID"))
virtualFileRecId = dbRdr("VirtualFileRecID")
dbStatus = dbRdr("status")
If dbStatus = "failure" Then
json.Add("msg", dbRdr("msg"))
End If
End If
dbRdr.Close()
End Using
' Prepare and start Task thread to write the file
If dbStatus = "success" Then
bytes = br.ReadBytes(fs.Length)
Dim task As New System.Threading.Tasks.Task(
Sub()
UpdateNewFileStreamBytes(virtualFileRecId, bytes)
End Sub)
task.Start()
json.Add("status", "success")
Else
json.Add("status", "failure")
End If
End Using
dbConn.Close()
End Using
trxScope.Complete()
End Using ' transaction commits here, not in line above
With the task procedure of:
Private Sub UpdateNewFileStreamBytes(virtualFileRecId As Integer, fileBytes As Byte())
Dim filePath As String = ""
Dim trxContext As Byte() = {}
Try
Using trxScope As New TransactionScope
Using dbConn As New SqlConnection(DigsFSConnStr)
Using dbCmd As New SqlCommand("UPD_FileStreamBaseFile", dbConn)
dbCmd.CommandType = CommandType.StoredProcedure
dbCmd.Parameters.AddWithValue("#VirtualFileRecID", virtualFileRecId)
dbConn.Open()
Using dbRdr As SqlDataReader = dbCmd.ExecuteReader(CommandBehavior.SingleRow)
dbRdr.Read()
If dbRdr.HasRows Then
filePath = dbRdr("Path")
trxContext = dbRdr("TrxContext")
End If
dbRdr.Close()
Using dest As New SqlFileStream(filePath, trxContext, FileAccess.Write)
dest.Write(fileBytes, 0, fileBytes.Length)
dest.Close()
End Using
End Using
End Using
dbConn.Close()
End Using
trxScope.Complete()
End Using
Catch ex As Exception
Elmah.ErrorSignal.FromCurrentContext().Raise(ex)
End Try

String is not assigned in a while loop

I'm reading a file from a FTP server, and I'm trying to use a while loop to assign the next line to a String and check if it's not null (Nothing).
The issue is that I'm pretty much used to C#, and this is my first attempt at VB. Can you tell me if I did something wrong that's causing it?
Dim request As WebClient = New WebClient()
Dim url As String = "ftp://ftp.harelwebs.net/" + "Default.aspx"
request.Credentials = New NetworkCredential("USERNAME", "PASS")
Try
Dim stream As IO.Stream = request.OpenRead(url)
Dim sr As StreamReader = New StreamReader(stream)
Dim line As String = Nothing
While (stream.CanRead And (line = sr.ReadLine()) <> Nothing)
MessageBox.Show(line)
End While
stream.Close()
sr.Close()
Catch ex As WebException
MessageBox.Show("Error.. " + ex.Message)
I've tested the same code in C# and it works perfectly, I have no idea why it doesn't work in VB.
Unfortunately you cannot assign a value in VB.NET inside a test clause. The reason being that = is used for both assignment and comparison and it would be very confusing and ambiguous in cases.
The solution to your problem would simply be to do this:
Dim line As String = sr.ReadLine()
While (stream.CanRead And line <> Nothing)
MessageBox.Show(line)
line = sr.ReadLine()
End While
If necessary you could wrap the assignment in an if statement if there may be instances where you want to set the line value to Nothing to prevent the loop running.

VB .NET error handling, pass error to caller

this is my very first project on vb.net and i am now struggling to migrate a vba working add in to a vb.net COM Add-in. I think i'm sort of getting the hang, but error handling has me stymied.
This is a test i've been using to understand the try-catch and how to pass exception to caller
Public Sub test()
Dim ActWkSh As Excel.Worksheet
Dim ActRng As Excel.Range
Dim ActCll As Excel.Range
Dim sVar01 As String
Dim iVar01 As Integer
Dim sVar02 As String
Dim iVar02 As Integer
Dim objVar01 As Object
ActWkSh = Me.Application.ActiveSheet
ActRng = Me.Application.Selection
ActCll = Me.Application.ActiveCell
iVar01 = iVar02 = 1
sVar01 = CStr(ActCll.Value)
sVar02 = CStr(ActCll.Offset(1, 0).Value)
Try
objVar01 = GetValuesV(sVar01, sVar02)
'DO SOMETHING HERE
Catch ex As Exception
MsgBox("ERROR: " + ex.Message)
'LOG ERROR SOMEWHERE
Finally
MsgBox("DONE!")
End Try
End Sub
Private Function GetValuesV(ByVal QryStr As Object, ByVal qryConn As String) As Object
Dim cnn As Object
Dim rs As Object
Try
cnn = CreateObject("ADODB.Connection")
cnn.Open(qryConn)
rs = CreateObject("ADODB.recordset")
rs = cnn.Execute(QryStr)
If rs.EOF = False Then
GetValuesV = rs.GetRows
Else
Throw New System.Exception("Query Return Empty Set")
End If
Catch ex As Exception
Throw ex
Finally
rs.Close()
cnn.Close()
End Try
End Function
i'd like to have the error message up to test, but
MsgBox("ERRORE: " + ex.Message)
pops out something unexpected (Object variable or With block variable not set)
What am i doing wrong here??
Thanks
D
It may be because you're doing a
Throw ex
Try using
Throw
instead. This maintains the error stack, instead of generating a new one each time.
Based on your comments, I think the specific problem you're getting is in your Finally block. You're trying to close the recordset and the connection, but if you got an error when you instantiated them, they will not exist; therefore, you get the Object variable or with block error.
(I would put the connection is a using statement anyway).

The process cannot access the file because it is being used by another process.-error while writing to a file in vb.net

in my vb.net code im using streamwriter to write to a file that has been given as input to the form.
Dim strContents As String
Dim objReader As StreamReader
Try
objReader = New StreamReader("C:\test.txt")
strContents = objReader.ReadToEnd()
objReader.Close()
Catch Ex As Exception
End Try
Dim Contents As String
Dim bAns As Boolean = False
Dim objWriter As StreamWriter
Dim FileStream As System.IO.FileStream
Try
FileStream = New FileStream("C:\test.txt", FileMode.Open, FileAccess.ReadWrite)
objWriter = New StreamWriter("C:\test.txt")
objWriter.Write("fdgdfgdjkljljklg")
objWriter.Close()
bAns = True
Catch Ex As Exception
End Try
in some of the systems its working fine but in some other workstations its showing error that mentioned in the subject.
What restricts the access to the file here?
can anyone help me on this?
What restricts the access to the file here?
The fact that you haven't disposed the stream and thus the process is locking the file. A StreamWriter is holding an unmanaged handle to the file. If you don't dispose this handle other threads/process cannot open the file.
I would recommend you to always wrap IDisposable resources in Using statement to ensure proper disposal (even if an exception is thrown inside the block):
Using filestream As New StreamWriter(Inifile, True, System.Text.Encoding.UTF8)
' ... use the filestream here to write to the file
End Using

Object reference not set to an instance of an object when calling a function

I call the function GetDataTable and when it gets to the return line, I get the error message:
Object reference not set to an instance of an object.
Dim DB As New DBConn
Dim gd As New DataAccess.GetData
Dim DT As New DataTable
Dim repotid1 As Decimal = 1150
Dim startdata1 As DateTime = "6/1/2012"
Dim EndDate1 As DateTime = "6/12/2012"
Dim StartDate3 As DateTime = "11/1/2011"
Dim Enddate3 As DateTime = "5/1/2012"
Dim sql1 As String = String.Format("EXEC [dbo].[usp_GetReportData_All] #ReportID=N'{0}', #StartDate=N'{1}' #EndDate=N'{2}', #StartDate2=N'{3}' #EndDate2=N'{4}'", repotid1, startdata1, EndDate1, StartDate3, Enddate3)
DT = DB.GetDataTable(sql1)
Public Class DBConn
Dim gd As New DataAccess.GetData
Public Function GetDataTable(ByVal sql As String) As DataTable
Dim _appID As String = "IS"
Dim _transID As String = "MSSQL01"
Return gd.getDataTable(_appID, _transID, sql) 'I get the error message here
End Function
End Class
Updated. I'll see if I can update with the DataAccess.GetData code. I didn't originally put it in there because our shop use it all the time and have no issues with it.
Without seeing what's in gd.getDataTable, there's not a lot we can do to help you. What kind of application are you writing that is calling this function? Can you put a breakpoint on that line in the editor and step into it to see where the error is occurring? Otherwise, do you have access to the DataAccess.GetData code? If so, you would get a lot more information from it if you threw the original exception without destroying the stack trace:
BAD!!!!
Try
'your code
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
GOOD:
Try
'your code
Catch ex As Exception
Throw
End Try