|
And your problem is ... ?
All I see is one or more TextBoxes that are bound to a "second" BindingSource (the "lookup" table) and a custom component that is used to "position" that BindingSource via a CurrencyManager / BindingManager.
You could even use a relation between your primary and "lookup" tables and bind to that, dispensing with the "custom component" altogether.
Are you binding to Typed DataSets or Entities / Entity Framework, or is everything just custom code / properties and no backing collection types? If the latter, that may be why you are finding things so difficult; you're not using the features that are available in the Framework.
|
|
|
|
|
Hi, I have a word file which contains numbers of shapes. Each shape contain some textboxes. I am copying that file to another location and assigning some values to those textboxes of those shapes. Now I have to assign the values to those textboxes then I have to ungroup those shape first then only I can assign values those text boxes. Now the problem is that when I ungroup those shapes, they change their locations in the document. I do not understand what is the problem. Here is the code
//copy the sourc file to another location
System.IO.File.Copy(Convert.ToString(source), Convert.ToString(destination));
Microsoft.Office.Interop.Word.Document obDoc = new Microsoft.Office.Interop.Word.Document();
object unknown = Type.Missing;
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault;
obDoc = varWord.Documents.Add(ref source, ref unknown, ref unknown, ref visible);
obDoc.Activate();
//hiding a shape--working fine
obj = "shape1";
varWord.ActiveDocument.Shapes.get_Item(ref obj).Select(ref unknown);
varWord.ActiveDocument.Shapes.get_Item(ref obj).Visible = MsoTriState.msoFalse;
//ungroup the shape
obj = "Shape2";
varWord.ActiveDocument.Shapes.get_Item(ref obj).Select(ref unknown);
varWord.ActiveDocument.Shapes.get_Item(ref obj).Ungroup();
obj = "txtbox1";
varWord.ActiveDocument.Shapes.get_Item(ref obj).Select(ref unknown);
varWord.Selection.TypeText(txtbox1value);
//show the word file
varWord.Visible = true;
varWord = null;
Pankaj
|
|
|
|
|
Got the solution
I have used following code before ungrouping the shape
System.Threading.Thread.Sleep(1000);
Actually when c# code run its ungroup the shape first, then render the file, that's why shape is showing in the header of the page, not in the proper position. Now using the sleep method it renders the shape in proper place.
Pankaj
|
|
|
|
|
ha ha ... I think the process takes some time to ungroup. So sleeping the Thread cures the problem.
|
|
|
|
|
May be you are right. I have also posted the code for ungrouping. But there are no problem in ungrouping. Do you have any solution without using threading ?
Pankaj
|
|
|
|
|
Is there a way to discover which is the actual exception, without using the catch block?
For example:
Dispose() will be called if there is an exception or not.
I want to know if this Dispose is happening naturally, at the end of the block or by an exception that forced the end of the block.
My idea is simple test something like:
Exception.GetCurrentException() != null
But I don't know if there is somewhere to look for this.
|
|
|
|
|
Paulo Zemek wrote: Is there a way to discover which is the actual exception, without using the catch block?
This is the second question on this subject that makes no sense. The try/catch construct is there for a reason, so make use of it and stop your programs from causing havoc.
|
|
|
|
|
Paulo Zemek wrote: Dispose() will be called if there is an exception or not.
if you have a using statement, yes, Dispose() will be called no matter what.
The functionality of Dispose() is to dispose of managed and/or unmanaged resources, independent of circumstances; so you are not even entitled to know if and which exception has occurred.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Don't use 'using' block, try this instead:
bool isOK = true;
try {
..........
..........
}
catch {
isOK = false;
}
finally {
if (!isOK) {
} else {
}
}
|
|
|
|
|
I know I can do this with try/catch/finally.
But I really want to know if there is any way to discover if the method I am is actually called by an exception (is inside a catch/finally) or not.
The purpose is to make the method simpler to use, as the caller of the method will not be required to pass the exception (or the boolean) as parameter.
A very ugly example:
using(var someTransaction = new MyTransaction())
{
... code here ...
someTransaction.Commit();
}
And then, in Dispose, I will check:
If the transaction was committed/rollbacked, do nothing.
If the transaction was not committed or rollbacked, but there is no exception, I will throw an exception telling that Commit or Rollback is required.
If there is already an exception, I will rollback and keep the original exception.
- Note: I know how to use try/catch/finally. But I really want to make the use of the code simpler for the others. using keyword looks nicer than try, catch and finally.
modified on Monday, November 23, 2009 12:38 PM
|
|
|
|
|
One possible solution is to have a global variable which all try/catch code can log their exception to. (Probably best to make that a List<exception> and process stuff on or off that exception stack.) Any code can then test the global exception variable to see if there is an exception outstanding. Once you have acted appropriately to that exception then clear it off the stack.
If you have knowledge, let others light their candles at it.
Margaret Fuller (1810 - 1850)
www.JacksonSoft.co.uk
|
|
|
|
|
But this brings the original problem again:
The developer will need to "add/remove" exceptions manually. My question is simple:
.Net has something like that built in (in any class) or not?
I originally thought that I could find catch using StackTrace, but it has no information about catchs.
Is there any way to discover this, even if this is an expensive operation? After all, this will be used only in Debug code.
-> To be honest, I really think that .Net must register exceptions automatically in a "FILO/LIFO" queue and so, when one exception is thrown inside a catch it could automatically fill the "inner exception" property without manual coding. Of course, such list must be ThreadStatic, as one thread can be executing normal code, while the other is executing a catch inside a catch.
|
|
|
|
|
In ASP.NET, there is something like Server.GetLastError();
|
|
|
|
|
Short answer: No.
There are sometimes ways to get an otherwise unhandled exception without a try-catch. In asp.net apps for example you can handle Application_OnError. But it's not a substitute for exception handling elsewhere in the code, only a last-resort chance to at least do things like log errors.
You can use either delegates or polymorphism if you want to standardize exception handling in certain situations. Something like this would be possible:
public static class DbHelper
{
public static RunInTransaction(SqlConnection cnx, Action method)
{
cnx.Open();
SqlTransaction tx = cnx.BeginTransaction();
try
{
method();
tx.Commit();
}
catch (Exception ex)
{
tx.Rollback();
throw new ApplicationException("Error during database transaction.", ex);
}
finally
{
cnx.Close();
}
}
}
Here I used the predefined Action delegate, which is void and takes no parameters, but you could of course
use any delegate you'd like. Or you could use polymorphism: It could be an abstract method that derived classes must implement (obviously the class then can no longer be static!), or the method could accept an object implementing some interface instead of a delegate.
There are some cool possibilities with IDisposable but it is really intended specifically for early release of non-managed resources. It's not meant to be a general try-finally replacement, and certainly not a try-catch-finally replacement.
modified on Monday, November 23, 2009 2:39 PM
|
|
|
|
|
|
Thanks a lot. This solves my problem!
|
|
|
|
|
Hi
I have a rather simple question. I have quite a few try/catch blocks in my system, but I don't really need to handle the exceptions catched. So I just use something like this:
try
{
}
catch (Exception error)
{
}
What I want to know is, since I'm not using "error" that I'm declaring, can I just have a parameterless catch like the following:
try
{
}
catch
{
}
|
|
|
|
|
You can.
But why are you doing it? It's a terrible practice to use exceptions as part of normal program flow. Exceptions are supposed to be *exceptional* - and there are extremely few cases where it would make any sense to just catch any exception, ignore it, and attempt to continue.
|
|
|
|
|
What is the sense in catching an exception and then ignoring it?
|
|
|
|
|
To prevent my system from crashing when an error does occur.
|
|
|
|
|
But once the error has occured, don't you want user to know? Or will your application behave normally even after any kind of exception occurs in it?
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
Presumably your program is supposed to *do* something, not merely "not crash". All that can be achieved by catching exceptions and doing nothing about them is to prevent the program from exiting because of the error. But there's a reason why programs exit by default when errors occur: The program state is no longer well known. Programs close by default in order to prevent a program from *seeming* to work but actually producing corrupt data.
If you are getting exceptions a lot of the time without any associated external reason (hardware failure, network failure, out of disk space/memory, and so on) it just means your code isn't correct, and you should take the time to find out what's wrong with it and correct it.
|
|
|
|
|
Etienne_123 wrote: To prevent my system from crashing when an error does occur
Most often that does not make sense at all.
An exception indicates something went wrong, you can not just ignore it, you should:
1) probably log it
2) act on it (if nothing else, throw it again, so a higher level of the code can act on it).
3) consider informing the user.
And you should only catch the exceptions you can handle in a meaningful way, hence catch them as specific as possible.
Examples:
- when writing to a file, if it fails (error in path, disk full, whatever IOException) you can't ignore that, since then your output would be uncertain; here you must inform the user, unless you have a retry that succeeds.
- when parsing user input, a parse exception has to be reported to the user so he can re-enter data.
The rare exception to the rule is e.g. where you have a retry loop, then all exceptions except for the one in the last retry iteration can be left unhandled and unreported (I suggest to include a comment as to why a catch block is empty then); the exception in the last retry iteration has to be handled though, since that is the exit result of the retry loop.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
I think you need to study exceptions: what they are for and what action you should take when you catch one.
|
|
|
|
|
"On Error Resume Next", and it should be taken out and shot.
I are Troll
|
|
|
|