Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 27th, 2009, 02:54 AM
Authorized User
Points: 135, Level: 2
Points: 135, Level: 2 Points: 135, Level: 2 Points: 135, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2007
Location: , , .
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Default Exception handling

Hi,
I have a method which returns a data table.
The data access code is in a try catch block.

Code:
public DataTable GetData(string fileName)
        {
            string methodmessage;

            DataTable dt;

            string sqlSelect = "SELECT * FROM " + fileName;

            try
            {
                OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\;Extended Properties=""Text;HDR=No;FMT=Delimited""");
                OleDbCommand cmd = new OleDbCommand(sqlSelect, cn);
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);

                dt = new DataTable("SalesRep");
                dt.Locale = CultureInfo.InvariantCulture;
                da.Fill(dt);
            }
            catch (OleDbException oleEx)
            {
                methodmessage = oleEx.Message;
            }

            return dt;
        }
In the calling part of the program I have it like this:

Code:
this.dtperson = new DataTable();
this.dtperson = Form1.GetData(@"C:\TempCsvFiles\person.CSV");
My question is if an exeception occurs in the GetData code, what needs to be done to handle that in the call

Code:
this.dtperson = Form1.GetData(@"C:\TempCsvFiles\person.CSV");
as the GetData method returns a data table.

Thanks and Regards.
  #2 (permalink)  
Old August 27th, 2009, 05:40 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

When you say "what needs to be done" it depends greatly on what you want to happen.

In the case above nothing happens - a blank dataset is assigned to the dtperson property. If this is what you want to happen then you're done.

If you want something else to happen then you have to decide what that is before we can help you write code to do it.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
  #3 (permalink)  
Old August 27th, 2009, 07:00 AM
Authorized User
Points: 135, Level: 2
Points: 135, Level: 2 Points: 135, Level: 2 Points: 135, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2007
Location: , , .
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Default

I would like to show the exception that is being caught and assigned to methodMessage variable.
That way the use is informed of what has happened.

Thanks and Regards.
  #4 (permalink)  
Old August 27th, 2009, 07:07 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well in that case get rid of the try/catch inside the method, and just catch it in the outer code:

Code:
try
{
this.dtperson = Form1.GetData(@"C:\TempCsvFiles\person.CSV");
}
catch(Exception ex)
{
    MessageBox.Show("An exception occurred: " + ex.Message);
}
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
  #5 (permalink)  
Old August 27th, 2009, 07:46 AM
Authorized User
Points: 135, Level: 2
Points: 135, Level: 2 Points: 135, Level: 2 Points: 135, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2007
Location: , , .
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thank you samjudson.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception Handling NewTitle2007 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 6 August 8th, 2007 04:03 AM
exception handling jay schumacher VB.NET 2002/2003 Basics 1 April 14th, 2006 08:40 AM
Exception handling aldwinenriquez General .NET 0 August 17th, 2005 06:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.