|
Agreed with the above 2 posters. You could also use
my.Computer.FileSystem.DeleteDirectory("c:\Temp",FileIO.DeleteDirectoryOption.DeleteAllContents)
I did a couple projects which had massive amounts of file/directory IO and the system.IO methods were faster.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
|
|
|
|
|
Hi folks,
I am looking for a bit of guidance on the best way to structure the following code to prevent the compiler warning.
The code works and does what it should, i just don't want to have the warning message sitting there all the time.
Bascially, the code polls a database for a list of values and returns them to the caller as a list(of String).
Public Function GetPlatformList() As List(Of String)
Dim result As New List(Of String)
Dim dbCon As New OleDb.OleDbConnection(dbConnectionString)
Dim dbCMD As OleDb.OleDbCommand
Dim dbReader As OleDb.OleDbDataReader
dbCMD = New OleDb.OleDbCommand("SELECT PlatformName FROM Platforms", dbCon)
Try
dbCon.Open()
dbReader = dbCMD.ExecuteReader()
Dim HasResult As Boolean = True
Do Until Not HasResult
Do While dbReader.Read()
result.Add(dbReader.GetString(0))
Loop
HasResult = dbReader.NextResult()
Loop
Catch ex As Exception
'Error Trying
MsgBox("Error Accessing Database: " & ex.Message, MsgBoxStyle.Critical, "Database Error")
End Try
dbReader.Close()
dbCon.Close()
'Return the list of platforms
Return result
End Function
I have tried coding this different ways, and this is following the examples in MSDN etc, but i always end up with the compiler waring;
Variable 'dbReader' is used before it has been assigned a value. A null reference exception could result at runtime.
The guidance I am looking for is how to structure it in such a way as to prevent the warning.
Thanks,
dave
|
|
|
|
|
daveauld wrote: Dim dbReader As OleDb.OleDbDataReader
This should read:
Dim dbReader As OleDb.OleDbDataReader = New OleDb.OleDbDataReader
Regards: Didi
|
|
|
|
|
Don't think so, as that cause a 'No Constructors error' and also if you read the spec for OleDbCommand.ExecuteReader, it returns a new reader with the result set.
|
|
|
|
|
Hi Dave,
the problem is the try block may be interrupted by an exception, so the compiler isn't sure the <code>dbReader = dbCMD.ExecuteReader()</code> will be executed, in which case <code>dbReader.Close()</code> uses dbReader which never obtained a value.
The solution to this is:
1. declare dbReader outside of the try block (you have that)
2. AND set it to Nothing
3. apply "test-before-use" outside the try block: If dbReader <> Nothing Then dbReader.Close()
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
modified on Friday, May 29, 2009 7:53 AM
|
|
|
|
|
Thanks Luc.
problem solved. I had completley forgot how the Nothing works, and for some reason had it in my mind that when setting something to nothing it could no longer be used.
I won't forget again! (hopefully!). Just goes to show how if you are not a regular programmer (which i am definitely not) you forget the basics!
Cheers Again,
dave
|
|
|
|
|
You're welcome.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
I'm not good at English
Just ignore this warning, because you assigned a value to dbReader in Try Statement
Think about Try: It means code in Try-Catch can be exited at any time.
chilinhhacker
|
|
|
|
|
The following code:
Private Sub ie_BeforeNavigate2(ByVal pDisp As Object, ByRef URL, ByRef Flags, ByRef TargetFrameName, ByRef PostData, ByRef Headers, ByRef Cancel As Boolean)
If (InStr(URL, "Dialog()") > 0) Or (URL = DIALOG_URL) Then wd.captureScreen
If (URL = MAIN_URL) Or (URL = BYE_URL) Then wd.captureScreen
End Sub
Used to trigger a screen capture based on specified URLs including main pages and script generated pop-ups; however, the web application is now using .NET and the BeforeNavigate2 event is not firing when the user navigates away from the pop-up. Is there a simple way to identify the closing of a pop-up in this environemnt and produce a screen image prior to the closing of a dialog window?
|
|
|
|
|
Have you considered using a real programming language ?
Christian Graus
Driven to the arms of OSX by Vista.
"I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
|
|
|
|
|
Yes, but I'm constrained by what is available on site. Do you have a specific recommendation?
|
|
|
|
|
I'm encountering problems when i want to download files from my server to my clients. My client is able to connect to my server but it will never transfer the file over to my client and i am unable to solve it.
Private Sub btnDL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDL.Click
btnDL.Enabled = True
btnRef.Enabled = True
btnULAbort.Enabled = True
Try
Disconnect()
Catch ex As Exception
End Try
Dim Message As String = "[RequestFileDownload]"
'SelectedItems(0) so that it knows to get the item from column 0 which is name
Message += ListView1.SelectedItems(0).Text
Dim filename As String
If ListView1.SelectedItems.Count < 1 Or ListView1.SelectedItems.Count > 1 Then
MsgBox("Please select a file")
Exit Sub
Else
client = New TcpClient
client.Connect("152.226.152.179", portNo)
btnDL.Enabled = False
SendMessage(Message)
client.GetStream.BeginRead(msgData, 0, CInt(client.ReceiveBufferSize), AddressOf RReceiveMessage, Nothing)
'MsgBox(FileRequest)
If FileRequest = "[DLTotalBytes]" Then
Dim DLTotal As String
DLTotal = FileRequest.Remove(0, 14)
FTP_Send(filename, "152.226.152.179")
End If
FTP_Receive(filename)
End If
End Sub
Public Sub FTP_Receive(ByVal filename As String)
Try
'---get the local IP address
Dim localAdd As System.Net.IPAddress = System.Net.IPAddress.Parse(ips.AddressList(0).ToString)
Dim nws As NetworkStream = client.GetStream
'---delete the file if it exists
If File.Exists("C:\Documents and Settings\user\My Documents\My Received Files" & filename) Then
File.Delete("C:\Documents and Settings\user\My Documents\My Received Files" & filename)
End If
'---create the file
fs = New System.IO.FileStream("C:\Documents and Settings\user\My Documents\My Received Files" & filename, FileMode.Append, FileAccess.Write)
Dim counter As Integer = 0
Dim totalBytes As Integer = 0
Do
'---read the incoming data
Dim bytesRead As Integer = nws.Read(msgData, 0, client.ReceiveBufferSize)
totalBytes += bytesRead
fs.Write(msgData, 0, bytesRead)
'--update the status label
ToolStripStatusLabel1.Text = "Downloading " & totalBytes & " bytes...."
Application.DoEvents()
System.Threading.Thread.Sleep(200)
counter += 1
Loop Until Not nws.DataAvailable
ToolStripStatusLabel1.Text = "Downloading " & totalBytes & " bytes....Done."
fs.Close()
client.Close()
Catch x As Exception
End Try
End Sub
|
|
|
|
|
First, you might want to put something inside the Catch x As Exception blocks so you can see the errors you're getting instead of swallowing them without a trace.
Just a quick glance and I can tell you that the paths you're using are illegal. You're doing this:
If File.Exists("C:\Documents and Settings\user\My Documents\My Received Files" & filename) Then
File.Delete("C:\Documents and Settings\user\My Documents\My Received Files" & filename)
End If
'---create the file
fs = New System.IO.FileStream("C:\Documents and Settings\user\My Documents\My Received Files" & filename...
Which will result in a path that looks like this:
C:\Documents and Settings\user\My Documents\My Received FilesSomeFileName.Ext
See something wrong with that path?? I can see three things at a minimum. First, the user better be a legal user name. I hope you're replacing that with the real user name. Second, your code doesn't put a backslash between the end of the directory chain and the filename you appended to it. Third, you shouldn't be building paths with hard-coded path information. You should be using the Path class to build that paths and the Environment class to get the current users profile path. Also, you might want to just build the path once and reuse the variable holding it instead of building the path multiple times.
Private Const FileStoreFolderName As String = "My Received Files"
...
Dim filename As String ' (this should be set to something prior to this code)
Dim userDocumentsPath As String = Environment.GetFolderPath(Environment.MyDocuments)
' This code broken out for clarity
Dim fullReceiveFilePath As String
fullReceiveFilePath = Path.Combine(userDocumentsPath, FileStoreFolderName)
fullReceiveFilePath = Path.Combine(fullReceiveFilePath, filename)
There's a ton of other things wrong with the code, but this is the most glaring. For example, just above this code in your snippet, you (attempt) to get an IPAddress, but you never use it in the rest of the code.
|
|
|
|
|
i need a code to find a data in an excel file all worksheets and if it's find get the all row, i tried to use worksheetfunction but it always failured, i could not find any document about it, have you got? or would you like to help me about this subject
here is my code for match function but it's always return an error that could not find in google because it's turkish
Imports Microsoft.Office.Interop
Module ModuleVedop
Public Function MatchAndGetRowNumber(ByVal Data As String, ByVal ExcelPath As String, ByVal WorkBookname As String) As Double
Try
Dim i As Integer = 0
Dim RowNumber As Double = Nothing
Dim XlApp As Excel.Application = New Excel.Application
XlApp.Workbooks.Open(ExcelPath)
Dim XlWorkSheet As Excel.Worksheet = New Excel.Worksheet
Dim Matchrange As Excel.Range
For Each XlWorkSheet In XlApp.Worksheets
Matchrange = XlApp.Workbooks(WorkBookname).Worksheets(XlWorkSheet.Name).range("A1:Z65536")
RowNumber = XlApp.WorksheetFunction.Match(Data, Matchrange, 0)
Next
If IsError(RowNumber) Then
MsgBox("not found")
Else
MsgBox("Found on row: " & RowNumber)
End If
XlApp.Workbooks.Close()
Return RowNumber
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
|
|
|
|
|
Hi. I am an excellent VB programmer but a poor web programmer. I am trying to get some data off a grid on a web page into a vb app and I am having a terrible time doing it. The site also requires an id and password. The site is http://ctmls.mlxchange.com/ and I'm using VB 6 on Vista.
I don't care what form I get the data in but its not in the source when I view it or save the page. It looks like it is coming from javascript.
I think it changes pages in frames because the address never changes, but the display does. That's why I don't think I can use winsock. Inet doesn't work. I can browse to the page fine with the webBrowser control and see the data.
Here's how I get the data on the screen:
1. Type in URL; go to site
2. Login to site, frame changes
3. Click "Search" from menu, frame changes
4. Type in some criteria, click results
5. Frame loads with a grid with data on I need to get into VB
Here is what the screen looks like after the data is retrieved (with sensitive data blocked out). http://www.marvelousware.com/images/Form.jpg[^]
The Userid is not mine, I am using it to write this program for the owner.
What property(s) or method(s) will return data? Can it be in innerHTML or outerHTML? Or someplace else?
Can I find out what script is getting the data and run it?
What is a proxy? Can a proxy pass the data through? Can it be done in VB?
Otherwise I will have to write OCR code to pull the data off the screen. That doesn't seem like the smartest way, or easiest way.
any help is appreciated, I'll buy a book if anyone knows one that WILL solve this problem.
Thank you.
|
|
|
|
|
Looks as though the HTML is being served up by an ASP script into an iFrame (well at least from the homepage anyway). If this is the case then you will not be able to run the script. But if you look at the source of the iFrame (src attribute) and navigate to it you might be able to see the HTML when viewing source.
|
|
|
|
|
Thanks for the quick reply.
I looked at the source from the IFRAME ()[^]
I found nothing. I tried to navigate to it and didn't even see the logon text boxes. It seems to only work as an inline frame.
Does the data have to be in the HTML? And if not, where else can it come from?
|
|
|
|
|
Did you look at the iFrame source for the homepage or the datagrid page?
And yes, the datagrid in question will almost definately be presented using HTML (the other option is Flash but It doesn't look like Flash to me).
|
|
|
|
|
Thanks again.
I looked at the IFRAME source on the homepage, this is what i found:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
-- UserData.asp
--
-- This page acts as a host page to all the user data that will be used by the application
--
-- IMPORTANT: This page MUST reside in a static folder (e.g. outside 5.0.05.46) as user data
-- is only available in the same directory. If we move this file to a different directory,
-- previously saved user data would be lost.
--
-- IMPORTANT: The page using UserData must not contain a frame named "frameUserData"
--
-- Setup:
-- 1. Include UserData.js
-- <script language="javascript" src="/5.0.05.46/Include/UserData.js"></script>
-- 2. Create the iframe that contains this page. There are two ways of doing this.
-- a) Through javascript. Call CreateUserDataFrame() once before using
-- any other functions in UserData.js
--
-- or
--
-- b) Through html. Have the following html iframe:
-- <iframe src="/Tools/UserData.asp" id="frameUserData" style="display: none;" />
--
-- Usage:
--
--
-->
<HTML xmlns="http://www.w3.org/1999/xhtml" >
<HEAD>
<TITLE>User Data Utility - Host Page</TITLE>
</HEAD>
<BODY style="behavior: url('#default#userData');">
</BODY>
</HTML>
I don't know where it goes next. I tried running the following code when I get to the page:
Private Function PrintDomBegin(hDoc As HTMLDocument) As Long
Dim str As String
Dim str2 As String
Dim ElemColl As Object
'Dim ElemColl As HTMLElementCollection
If Not hDoc Is Nothing Then
frmDetail.List2.Clear
Set ElemColl = hDoc.getElementsByTagName("HTML")
str = PrintDom(ElemColl, str2, 0, 0)
frmDetail.Text1 = str
End If
'PrintDomBegin = ElemColl.Count
End Function
Private Function PrintDom(ByVal ElemColl As Object, ByRef ReturnStr As String, ByVal lngDepth As Long, lngIndex As Long) As String
Dim str As String
Dim strKey As String
Dim strElemName As String
Dim Elem As HTMLDTElement
For Each Elem In ElemColl
lngIndex = lngIndex + 1
strKey = "F: " & Trim(CStr(mlngFrameIndex)) & " - D: " & Trim(CStr(lngDepth)) & " #" & Trim(CStr(lngIndex))
mcol.Add Elem, strKey
'frmDetail.List1.AddItem strKey
strElemName = Elem.getAttribute("ID")
If strElemName = "" Then
strElemName = "" & Elem.getAttribute("name")
If strElemName = "" Then
strElemName = "<no name>"
End If
End If
'If strElemName = "txtLogin" Then Debug.Print Elem.outerHTML
str = Space(lngDepth * 4)
str = str & (strElemName & ": " & Elem.tagName & "(Level " & lngDepth & ")")
ReturnStr = ReturnStr & str & vbCrLf
frmDetail.List2.AddItem str
frmDetail.List2.ItemData(frmDetail.List2.NewIndex) = mlngFrameIndex * 1000000 + lngDepth * 1000 + lngIndex
frmDetail.lblItems = "Count = " & frmDetail.List2.ListCount
If (Elem.canHaveChildren) Then
PrintDom Elem.children, ReturnStr, lngDepth + 1, lngIndex
End If
str = ""
Next
PrintDom = ReturnStr
End Function
I can't get it to work with ElemColl decalred as HTMLElementCollection, but I don't think that it would make much difference. I then searched all the ElemColl objects' properties of .innerHTML, innerText, outerHTML & outerText and can't find anything that is in the grid. I don't know if tis helps, but the grid isn't completely populated when it first comes up, some data is there (that I can't find) but scroll down a little bit and it loads more rows.
Can data be on the screen and not accessable?
Does getElementsByTagName("HTML") return ALL the HTML?
Is there somewhere else I can look?
I don't know what I am missing and where.
|
|
|
|
|
Sorry, but without being able to see the source for the actualy page where the datagrid sits it's just to difficult for mw to help you. Maybe someone more experienced would be able to help?
You may better off posting on the Web Development board and asking how you can get to the HTML for the datagrid which you believe is in an iFrame. Once you have the HTML then ask here how to get the data out.
|
|
|
|
|
Thanks for the help. I'll repost it.
|
|
|
|
|
Hi There,
I have an Application of Laboratory Management.
In this i have created multiple pages to take the results of the tests that has been performed for the patient.
Supposidly, there are 2 Blood Tests out of 10 , 3 Urine Tests out of 8, 2 Stool Tests out of 10.
All these have different Pages and Different Tables to get store.
Now what i want is to print only the tests that have been performed having correct format of print.
I do not know how many Labels i have to use for the display and then print the same page. This will be decided at runtime.
If possible can you plz tell me how to Create a Scrolling Page in VB and Print the same.
Regards,
Kaushal Arora
|
|
|
|
|
Kaushal Arora wrote: Now what i want is to print only the tests that have been performed having correct format of print.
I do not know how many Labels i have to use for the display and then print the same page. This will be decided at runtime.
Why not just use a PDF document, fill in the fields propertly from your code, then display that on your form. The exact same document can be printed from your code or from another PDF reader without modification.
You'd need a 3rd party library to work with the PDF document, something like PDF.NET[^].
|
|
|
|
|
I have to Build a Dynamic Page. How can i do this?
|
|
|
|
|
What do you mean by saying 'dynamic page' a dynamicly created Windows Form or a page created using GDI+ or what else? Your question looks to me like a attempt to let others do your work.
Regards: Didi
|
|
|
|
|