|
Exceptions are string messages. You need something like ex.Message which gives the text message of the exception. Hope this helps you
|
|
|
|
|
There is no error number. .NET uses exceptions instead of error codes, with good reason. The exception defines the error, why return a code which, you hope, the user can turn back into an Exception so that it contains meaningful error. No code can give a call stack - it's a step backwards.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Yes, but I think he means he wants to differentiate between specific exceptions, so he can decide how to handle them programatically. In other words, how do you compare exceptions?
I've wondered this myself.
For almost any method or property in .NET, you can look at the help files and see what possible Exceptions they can throw. So it stands to reason that you can somehow say "if myException is X, do this" somehow.
I suppose you could compare the message text, but I doubt this is canonical, guaranteed to be unique or not change...
|
|
|
|
|
I'm a newb, but it looks like you have to know what exceptions can be thrown, and handle it in one of two ways:
try
{
DoWork();
}
catch (System.File.FileNotExists e)
{
}
OR,
catch (Exception e)
{
if (e is StackOverflowException ||
e is OutOfMemoryException)
DoSomething();
}
of course, you don't want to eat all exceptions, as I am in the second example. That will screw you.
|
|
|
|
|
JoeRip wrote: how do you compare exceptions?
You don't really compare exceptions. If you need to take different action based on different exceptions, you would write multiple catch handlers.
try
{
}
catch (System.NullReferenceException)
{
}
catch (System.InvalidOperationException)
{
}
The only caution is that you need to pay attention to the ordering of the catch handlers. The runtime will go through the catch handlers until it finds the first one that can handle the exception and then stop. This means that if you catch a base exception before a derived one, you will never handle the derived exception. For example:
try
{
}
catch (System.ArgumentException)
{
}
catch (System.ArgumentNullException)
{
}
try
{
}
catch (System.ArgumentNullException)
{
}
catch (System.ArgumentException)
{
}
In general, you only want to catch exceptions that your code can actually do something about and perform some sort of cleanup. You don't want to use them to help control your program flow as actually catching the exception is expensive.
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
JoeRip wrote: So it stands to reason that you can somehow say "if myException is X, do this" somehow.
You can catch Exception and use the 'is' keyword, ( and hopefully rethrow if it's unhandled ), or you can specify in your catch statemetns what you wan to catch ( better ) Some exceptions have base classes, which you can also catch.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
try
{
// throw specific exception here
}
catch ( KeyNotFoundException ex )
{
}
catch ( InvalidOperationException ex )
{
}
catch ( Exception ex )
{
// all unhandled exceptions go here
}
HTH,
Martin
|
|
|
|
|
Hello, I'm fairly new to the C# world. What am I saying? I'm pretty new to coding all around and I've ran into a problem I have had to luck figuring out. So...
Before you ask, yes I have looked threw articles on the site. But none of them really help me. It is very hard to understand uncommented code at my skill level so it really does no good to me.
My first C# application is a simple RSS reader. I have most of the stuff done for it besides the TreeView. I cannot seem to figure out how to get this TreeView to grab data from my database and then display it in a certain way.
The tree view in the end should look like this:
<br />
TreeView<br />
<br />
|Folder<br />
|-RSS Channel/Feed<br />
|--News Item<br />
Ok so I have three folders in my database: Folders, Feeds, and NewsItems. The folders in the tree view will call the database table Folders, the rss channels/feed will call data from the Feeds table and finally the news items will call data from the NewsItems table.
How would I go about doing this? I have predefined values in all tables in the database, I'm not worried about adding new stuff to it yet and have it update. I just need to get the view to work.
Thanks for any help you can give
|
|
|
|
|
You can read data from the database using the System.Data.SqlClient.DataReader class to read data from your database. Once you have that data, you can create tree nodes from that data, then add those nodes to the treeview. Here's some idea of what this would look like:
SqlConnection connection = new SqlConnection("your connection string goes here");
SqlCommand myCommand = new SqlCommand("Select * from MyTable");
myCommand.Connection = connection;
SqlDataReader reader = myCommand.ExecuteReader();
while(reader.Reader())
{
string nodeText = reader.GetString(0);
TreeNode node = new TreeNode(nodeText);
myTreeView.Nodes.Add(node);
}
connection.Dispose();
myCommand.Dispose();
reader.Dispose();
|
|
|
|
|
Thanks for the help. I've ran into another problem while editing somethings. In order to use this it said to open the database, so I used:
connection.Open();
But it spits out an exception:
System.Data.SqlClient.SqlException was unhandled<br />
Message="An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
I don't understand how it is a remote connection if the database is the SQLServer that comes with C# Express 2005. Any ideas?
Thanks again for the help.
|
|
|
|
|
Oh, yes, you'll need to open a connection to SQL. This error occurs when SQL either isn't configured to allow connections to it from remote machines, or your connection string is invalid. Are you sure that connection string works? Try connecting to the database from, say, Sql Management Studio, or some other tool with that connection string to verify your connection string works.
p.s. judging by the error, named pipes might not work in this scenario. Try TCP.
|
|
|
|
|
Yup, 100KB app, 25KB of pictures, no sounds.
When it loads it imediatly takes up 9MB, now i know thats not alot, but where is it going? When the app loads it displays an empty listbox with 4 colums, two combo boxes with 10 items in, 5 buttons, and a text box.
It also creates 2 bools and a ListViewItem array, all empty. (The array is not given a size, just defined)
It does no processing, doesn't load any files, doesn't connect to the internet, until the user does something.
So err, why is it using so much and how do i fix it?
|
|
|
|
|
Well, that's the way things are nowadays, your first line of code is very expensive
(whatever it contains).
The good news is you can add 100,000 lines of code and still may need no more than say 50 MB
of memory.
Luc Pattyn
|
|
|
|
|
Indeed, i just checked a test app i made, which has one button, a text box, with just 100 lines of code total. It also yoinks itself 9MB.
It just seems a bit of a waste really.
|
|
|
|
|
Sure it is, but then those tons of megabytes are sitting there anyway, so why not fill them ?
(Same holds true for disks).
Luc Pattyn
|
|
|
|
|
That's the way .NET works. It allocates that "much" memory without actualy needing it. Reason is performance. Allocating one big block is more efficient then allocate many little blocks of memory. When later you need memory, "allocating" is as fast as changing one pointer. IIRC .NET does release this extra memory when OS is getting low on memory. No big deal in 99% of cases.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
|
|
|
|
|
As has been said, nothing to fix. Your usage is not linear, adding anouther 25kb of pictures will probably have no effect on your program size at all.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Ah, you learn somehting new every day. Im just suprised i never noticed this before, i've been doing it long enough. And with 2GB of RAM, it really doesn't make any difference.
|
|
|
|
|
We are using Visual Studio .NET 2003 ,C# and MS SQL SERVER 2000.
Ours is a Web Application. Now i need to develop some reporting application with which our power users or executives should be able to pull data and make decisions.
can I integrate that with my web application?
What are options here? why?
My visual Studio.net 2003 has crystal reports but asking me for registration. do i have buy a different license for it?
Is crystal reports and Business objects same? and what version should i select?
Any help on this would be really appreciated.
Thanks.
|
|
|
|
|
Hi everyone,
Could anyone tell me the best book available for:
1. OOPS
2. C# 2.0
2. .NET Framework 2.0
I am new to .NET and C# (and programming). I have been working on VB.NET 2003 for the past 6 months and would like to learn more about .NET 2.0, OOPS, and .NET Framework 2.0. Any information that you guys would like to share would be appreciated.
Regards,
Blumen
|
|
|
|
|
It does not fit in any of your 3 categories but I found Professional C# 2005 (Wrox) an excellent resource.
led mike
|
|
|
|
|
Professional C# 2005 is not a professional level book. Sure it isn't an intro to programming but I am looking for more in books with the professional title.
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
|
|
|
|
|
I hated Wrox. I was not surprised that they went broke. Overthick books full of useless code listings ( i.e. listings of boiler plate code, etc ).
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Ennis Ray Lynch, Jr. wrote: Professional C# 2005 is not a professional level book.
I don't understand that statement. For example when I used the Remoting Chapter, I found the information was complete and presented in a logical fashion. What do you mean by "not a professional level"?
led mike
|
|
|
|
|
Implies years of industry experience and the ability to search on the web and read API references. A book with the title Professional should understand that focus on applications of the new technologies in difficult and new ways which would expose a professional to A) Solid reference to advanced topics and B) Provide exposure to methodology
While definately not intro to programming the book is an intro to C#.
File Not Found
|
|
|
|