|
i have this code on a form :
Function f1 as boolean
Try
------------
-----------
if condition
return true
else
return false
end if
Catch ex as Exception
Me.close
End try
End function
Private sub s1
if f1 then
instruction 1
else
instruction 2
end if
End sub
But if an exception occur inside f1 , the instruction Me.close
doesn't close the form instantly , but after instruction 2 on s1 sub is executed. How can i do to close the form instantly?
Thank you!
|
|
|
|
|
Assuming you are talking about WinForms, and sub s1 is called from inside some handler (say a button click handler) that behavior is what they intended it to be. Form.Close() does not actually close the Form, it merely queues a request to close the Form; which will be handled when the main thread (on which your button click handler is executing right now) gets around to it. And even then, it will first fire a FormClosing event, and then close the Form, and finally fire the FormClosed event. This is a common pattern in Windows and its event mechanism.
What you could do inside function f1 is either not catch the exception, or throw a new one; and then try-catch in sub s1 and do (or not do) as you please.
Please keep in mind that often functions are supposed to calculate something and return the result, and NOT to cause side effects (such as closing a Form). When you deviate from that, you have to take full responsibility and bear the consequences.
|
|
|
|
|
Hello !
I have an application in VB.net 2010 / Entity Framework with database in SQL server 2008R2. The application works , but doesn't handle the situation when connection with server is lost during a transaction.For example if i press a button that select some records from database but before the sql server for example become innaccesible on network , the application crash. I want to handle this situation to keep running the application and display only error messages.I start to modify my code and adding Try...Catch blocks.But sometime i don't know where is better to put try... Catch and what to do with the form where the exception occur.For example :
---------------------
Private sub ......
Instruction 1
Instruction 2
...
instruction 5 ( Read from database)
Instruction 6
----
instruction 8 ( Write to database
------
-----
End sub
So is better to start try on the beginning of sub and End try to the end of sub ?The problem is that instruction 1,2,3,4 do something on the form and if instruction 5 has exception and is not executed the form may have wrong data and the instruction 8 if is executed correctly ( for example if the connection with server is ok on this instruction) may have other errors because instruction 5 is not executed.So can you suggest that is better to close the form if an exception (lost sql server connection) occur ?
Can you give me a suggestion?
yhank you.
modified 23-Feb-12 16:55pm.
|
|
|
|
|
The best advice here is not to mingle GUI code and your database operations; keep them well apart, in different methods, if not different classes. And then insert try-catch blocks that catch those exceptions you can deal with, and NOT the ones you cannot handle at that level. The ones you can't handle should ripple through to the caller, hoping he would know what to do with them.
Example: if you write a method that should delete a file, you'd use File.Delete inside a try block inside a loop; the loop is needed because the delete might fail as helper programs (disk indexers, search tools, ...) might be reading the file and have it locked. So the code might look like this (in a pseudo-code which resembles C#):
deleteFile(string filename) {
for(int tries=5; i>=0; i--) {
try {
File.Delete(filename);
return;
} catch (IOException) {
if (i>0) Thread.Sleep(100);
else throw;
}
}
}
In this example, "dealing with the problem" meant retrying it a couple of times.
If the retry loop is a surprise, know that Windows Explorer does this in a similar way; it is why it takes several seconds before you get a failure notice when it is unable to delete a file...
|
|
|
|
|
Thank you !
But my application work with a sql server.And if a have this block ( is excecuted when i press a button):
Try
.....Read something from sql server
Catch ex as exception
.... Dispaly an error message
end try
i noticed that when the sql server is not ready on network , when i press the button for the first time the error message display very fast.If i try again to press the button ( and the server is again not ready) the error message display after some time .
|
|
|
|
|
alejx wrote: If i try again to press the button ( and the server is again not ready) the error message display after some time .
15 seconds[^].
Bastard Programmer from Hell
|
|
|
|
|
But why on the first time the message is displayed instantly?
The button do the same thing on first and ather times
|
|
|
|
|
Put a breakpoint in there and see what the connection-timeout is. I'd guess that the first time it'd be initialized to 0.
Bastard Programmer from Hell
|
|
|
|
|
Yes , is 0 , but you think is better to initialize with another value , and how can i do this?
i'm working with entity framework.
|
|
|
|
|
alejx wrote: Yes , is 0 , but you think is better to initialize with another value , and how can i do this?
I'm usually using the default setting; when I do remember to initialize it, it's mostly set to a timeout of five seconds, since my development-database is located on the same machine - it wouldn't need very long to respond and open a connection.
The MSDN page that I linked to shows how you can change the value.
Bastard Programmer from Hell
|
|
|
|
|
Is it possible to run a Photoshop action with VB.NET. If so can any one tell me the way.
|
|
|
|
|
what is "Photoshop action"? are you after launching photoshop from your application? if so I would look at ProcessStartInfo
If I have it all wrong please expand your question
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
I just want to run a action installed in Photoshop. First my app will start Photoshop. I can do it. Then it should run given action installed in Photoshop. That is the problem
|
|
|
|
|
Tried SendKeys[^]?
Bastard Programmer from Hell
|
|
|
|
|
It turns out [^] that Photoshop has an automation interface. So if you can use VBScript to automate it than it can be done with VB.Net.
|
|
|
|
|
True, but whether it's worth it or not depends on what he wants to do. If it's just resizing an image, using Photoshop is like using a hydrogen bomb to kill a fly.
|
|
|
|
|
I want to run any action given by user. Not only resize image.
|
|
|
|
|
How can I pass the value from pop up menu to column gridview?
In my gridview I have a link button which will open another page which has a gridview also.
After I click an item to the pop up menu gridview, the data selected should copy to
the parent page inside column gridview.
note: this gridview is a third party which is obout.
To make it clear, please see the attached image.
http://i44.tinypic.com/4socgk.jpg[^]
C# コードMicrosoft End User
2000-2008
「「「「「「「「「「「「「「「「「「「「「「「「「「「「
The best things in life are free
」」」」」」」」」」」」」」」」」」」」」」」」」」」」
|
|
|
|
|
whenever my program used by multiple computer, i have to change the connection string to the destination computer IP (database there) and build again the project,
i wonder if visual basic support reading connection string from .txt?
can somebody gimme the illustration?
i think it's easier this way.
|
|
|
|
|
There are many ways to get a dynamic connection string, one of them is supported directly by .NET through the application config, which has a connection string section, and even a way to encrypt that part. Start Googling! I'm pretty sure you even will find articles here on CodeProject that illustrate it all.
|
|
|
|
|
i found it, check this out :
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim FileName As String = Application.StartupPath + "\Config\conString.txt"
Dim TextLine As String = String.Empty
If System.IO.File.Exists(FileName) = True Then
Dim ObjectReader As New System.IO.StreamReader(FileName)
Do While ObjectReader.Peek() <> -1
TextLine = TextLine & ObjectReader.ReadLine()
Loop
Else
MessageBox.Show("Connection String not found " + vbNewLine + vbNewLine + FileName, "Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
End If
End Sub
working perfectly, thankyou..
|
|
|
|
|
you're welcome.
However:
1. That wasn't what I suggested, through the official app config file.
2. You could read all content of a text file without using streams, just a single Dim Text As String = File.ReadAllText(Path)
|
|
|
|
|
Have a read of this Connection Strings and Configuration Files (ADO.NET)[^]
This is what Luc was pointing you towards
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
did SQL Server need ADO.NET?
|
|
|
|
|
When programming for the .NET framework, ADO.NET is the normal way to interact with an SQL Server database.
|
|
|
|