|
Yes, it's a textbox only to display information of a binding field, but what the component does is retrieve data from another datasource and sets the information to the binding field.
Therefore, I would have a textbox control binding the field "Total" (display only) and a custom dynamically created property in the component, binding to the same binding field "Total", the only difference is that the component is going to set the information of the Total Field and as BOTH are linked to the SAME bindingfield, the commom textbox will be updated too for display only.
|
|
|
|
|
I still don't get it.
Can't your component just set the Total property, to which the TextBox is bound?
Why does it need to have another property and bind this to the Total property? To do so dynamically it would have to know about (discover) the Total property anyway, so rather than create another property that also represents the total, and then bind this to the "real" Total property, and then set this other property so that the read-write binding causes the Total property to change... would it not be a lot easier to simply assign the Total property directly?
It may very well be that I haven't understood what you're trying to do. But if all you want to do is to automatically load the data from a foreign key into another data source, I cannot see how bindable properties comes into the picture at all. You just need to know what the foreign key value is and to what table it refers, and you could hold this information in any number of simple data structures, regardless of how it is obtained (mapping files, dynamically discovering relations defined in the database, attributes on the entity types, or whatever).
I will simply repeat this: Writing a bunch or relational logic and putting into a user interface component is not a good idea. It is no less work yet has many drawbacks compared to properly separating UI from business logic from data access. The problem you are trying to address, that of having to spend a lot of time on mundane data access code that to a large extent follows automatically from the data model (in most cases, the entities in the domain model largely correspond to the tables in the database), is one that many large and well organized groups of people have tried to tackle before you. Take a look at the Entity Framework, or NHibernate, or even CodeSmith templates that will generate a DAL for you.
|
|
|
|
|
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
|
|
|
|