473,789 Members | 2,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# try catch

21 New Member
Dear All,
I am new to C# and I am developing an application which will 24hours run on the remote PC..To transfer records from mysql to access db.I have succesfully created the application and have to handle the ODBC error and standard error.Basically it has to skip the error and move to next record.I really dont know how to use try catch.I am pasting the code here below.Excuse if I am posting new thread for answered question,Thanks in advance.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.Odbc;
  9. using System.IO;
  10.  
  11. namespace chart_patient_111108
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.  
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.  
  20.  
  21.  
  22.  
  23.                 StreamWriter sw;
  24.  
  25.                 string datename = Application.StartupPath + "\\" + DateTime.Now.ToShortDateString() + ".txt";
  26.  
  27.                 DateTime currentDateTime = DateTime.Now;
  28.  
  29.                 datename = datename.Replace("/", "");
  30.  
  31.                 string shortDate = currentDateTime.ToLongDateString();
  32.  
  33.                 textBox3.Text = datename;
  34.  
  35.  
  36.                     if (!File.Exists(datename))
  37.                     {
  38.  
  39.                         //sw = File.CreateText(path +shortDate+ ".txt");
  40.                         sw = File.CreateText(datename);
  41.  
  42.  
  43.                         //sw = File.CreateText(datename+".txt");
  44.                     }
  45.  
  46.                     else
  47.                     {
  48.                         sw = File.AppendText(datename);
  49.                     }
  50.  
  51.  
  52.  
  53.                 textBox4.Text = datename;
  54.                 sw.Flush();
  55.  
  56.  
  57.  
  58.  
  59.                 string sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";
  60.                 sw.WriteLine("Application initialized at " + sLogFormat);
  61.                 sw.WriteLine("\n\n");
  62.                 sw.Flush();
  63.  
  64.                 string profstr = "DRIVER={MySQL ODBC 5.1 Driver};" +
  65.                            "SERVER=127.0.0.1;" +
  66.                            "DATABASE=andy;" +
  67.                            "UID=root;" +
  68.                            "PASSWORD=mysql123;" +
  69.                            "OPTION=3";
  70.                 OdbcConnection myprofcon = new OdbcConnection(profstr);
  71.  
  72.  
  73.  
  74.  
  75.                     myprofcon.Open();
  76.  
  77.                     try
  78.                     {
  79.                         OdbcCommand procommand = new OdbcCommand("select accountid,server,userid,password,EMRID,mdbpath,mdbuserid,mdbpassword from profiledb where active = 'y'", myprofcon);
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.                     OdbcDataReader proreader = procommand.ExecuteReader(CommandBehavior.CloseConnection);
  87.  
  88.                     if (proreader.HasRows)
  89.                     {
  90.                         try
  91.                         {
  92.                             while (proreader.Read())
  93.                             {
  94.                                 string profid = proreader["EMRID"].ToString();
  95.                                 string accid = proreader["accountid"].ToString();
  96.                                 string server = proreader["server"].ToString();
  97.                                 string userid = proreader["userid"].ToString();
  98.                                 string password = proreader["password"].ToString();
  99.                                 string mdbpath = proreader["mdbpath"].ToString();
  100.                                 string mdbuserid = proreader["mdbuserid"].ToString();
  101.                                 string mdbpassword = proreader["mdbpassword"].ToString();
  102.  
  103.  
  104.  
  105.                                 string MyConString = "DRIVER={MySQL ODBC 5.1 Driver};" +
  106.                                            "SERVER=" + server + ";" +
  107.                                            "DATABASE=" + profid + ";" +
  108.                                            "UID=" + userid + ";" +
  109.                                            "PASSWORD=" + password + ";" +
  110.                                            "OPTION=3";
  111.  
  112.  
  113.  
  114.  
  115.  
  116.                                 OdbcConnection MyConnection = new OdbcConnection(MyConString);
  117.  
  118.                                 MyConnection.Open();
  119.                                 sw.WriteLine(" Starting process for EMR ID '" + profid + "' AccountID->" + accid);
  120.                                 sw.WriteLine("\n");
  121.                                 sw.Flush();
  122.  
  123.                                 OdbcConnection mdbconn = new OdbcConnection();
  124.                                 //mdbconn.ConnectionString =
  125.                                 //                            "Driver={Microsoft Access Driver (*.mdb)};" +
  126.                                 //                           "Dbq=E:/Documents and Settings/Ananjeev/Desktop/Task 29th/client.mdb;" +
  127.                                 //                            "Uid=Admin;Pwd=;";
  128.                                 mdbconn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" +
  129.                                   "Dbq=" + mdbpath + ";" +
  130.                                    "Uid=" + mdbuserid + ";Pwd=" + mdbpassword + ";";
  131.                                 try
  132.                                 {
  133.  
  134.                                     mdbconn.Open();
  135.  
  136.                                 }
  137.                                 catch
  138.                                 {
  139.                                 }
  140.  
  141.  
  142.  
  143.                                 OdbcCommand MyCommand = new OdbcCommand("select * from chart", MyConnection);
  144.                                 //select ssn,MRN,sex,DOB,FirstName,LastName,CI_address1,CI_address2,CI_city,CI_state,CI_zip,count(*) as counttest from chart where lastmodified >= NOW() - INTERVAL 10 MINUTE group by ssn
  145.  
  146.  
  147.  
  148.                                 OdbcDataReader myreader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
  149.  
  150.  
  151.  
  152.  
  153.  
  154.                                 if (myreader.HasRows)
  155.                                 {
  156.  
  157.                                     while (myreader.Read())
  158.                                     {
  159.  
  160.  
  161.  
  162.                                         string MRN1 = (string)myreader["MRN"];
  163.  
  164.                                         string ssn1 = (string)myreader["ssn"];
  165.  
  166.                                         string sex1 = myreader["sex"].ToString();
  167.  
  168.                                         string dob1 = myreader["DOB"].ToString();
  169.  
  170.                                         string firstname1 = myreader["firstname"].ToString();
  171.  
  172.  
  173.                                         string lastname1 = myreader["lastname"].ToString();
  174.  
  175.                                         string CI_address11 = myreader["CI_address1"].ToString();
  176.  
  177.                                         string CI_address21 = myreader["CI_address2"].ToString();
  178.  
  179.                                         string CI_city1 = myreader["CI_city"].ToString();
  180.  
  181.                                         string CI_state1 = myreader["CI_state"].ToString();
  182.  
  183.                                         string CI_zip1 = myreader["CI_zip"].ToString();
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.                                         OdbcCommand mycommand2 = new OdbcCommand("select * from patientinfo where patientid ='" + MRN1 + "' ", mdbconn);
  193.                                         OdbcDataReader myreader2 = mycommand2.ExecuteReader(CommandBehavior.CloseConnection);
  194.  
  195.  
  196.  
  197.  
  198.                                         if (myreader2.HasRows)
  199.                                         {
  200.                                             OdbcCommand mycommand3 = new OdbcCommand("update patientinfo set [patient last] = '" + lastname1 + "',[Account]='" + accid + "',[patient first] = '" + firstname1 + "',[pt dob] = '" + dob1 + "',ssn = '" + ssn1 + "',address = '" + CI_address11 + "',address2 = '" + CI_address21 + "',city = '" + CI_city1 + "',state = '" + CI_state1 + "',sex = '" + sex1 + "',zipcode = '" + CI_zip1 + "'  where patientid = '" + MRN1 + "'", mdbconn);
  201.  
  202.                                             int nrows = mycommand3.ExecuteNonQuery();
  203.                                             textBox1.Text = Convert.ToString(nrows);
  204.  
  205.  
  206.  
  207.                                             if (nrows > 0)
  208.                                             {
  209.  
  210.                                                 sw.WriteLine("\t" + sLogFormat + "  update:True on MRN '" + MRN1 + "' AccountID->" + accid);
  211.                                                 sw.Flush();
  212.  
  213.                                             }
  214.  
  215.                                             else
  216.                                             {
  217.  
  218.                                                 sw.WriteLine("\t" + sLogFormat + "  update:False on MRN '" + MRN1 + "'AccountID->" + accid);
  219.                                                 sw.Flush();
  220.  
  221.                                             }
  222.  
  223.  
  224.  
  225.                                         }
  226.  
  227.                                         else
  228.                                         {
  229.  
  230.                                             OdbcCommand mycommand4 = new OdbcCommand("insert into patientinfo([patientid],[patient first],[patient last],[pt DOB],[SSN],[address],[address2],[city],[state],[zipcode],[sex]) values ('" + MRN1 + "','" + firstname1 + "','" + lastname1 + "','" + dob1 + "','" + ssn1 + "','" + CI_address11 + "','" + CI_address21 + "','" + CI_city1 + "','" + CI_state1 + "','" + CI_zip1 + "','" + sex1 + "')", mdbconn);
  231.                                             int k = mycommand4.ExecuteNonQuery();
  232.                                             textBox2.Text = Convert.ToString(k);
  233.  
  234.                                             if (k > 0)
  235.                                             {
  236.                                                 sw.WriteLine("\t" + sLogFormat + "  Insert:True on MRN '" + MRN1 + "'AccountID->" + accid);
  237.                                                 sw.Flush();
  238.  
  239.                                             }
  240.                                             else
  241.                                             {
  242.                                                 sw.WriteLine("\t" + sLogFormat + "  Insert:False on MRN '" + MRN1 + "'AccountID->" + accid);
  243.                                                 sw.Flush();
  244.                                             }
  245.  
  246.  
  247.  
  248.                                         }
  249.  
  250.                                         MRN1 = null;
  251.                                         ssn1 = null;
  252.                                         sex1 = null;
  253.                                         dob1 = null;
  254.                                         firstname1 = null;
  255.                                         lastname1 = null;
  256.                                         CI_address11 = null;
  257.                                         CI_address21 = null;
  258.                                         CI_city1 = null;
  259.                                         CI_state1 = null;
  260.                                         CI_zip1 = null;
  261.                                         /// emrid = null;
  262.                                         accid = null;
  263.                                         server = null;
  264.                                         userid = null;
  265.                                         password = null;
  266.  
  267.  
  268.  
  269.  
  270.  
  271.                                     }
  272.                                     try
  273.                                     {
  274.  
  275.                                         while (myreader.NextResult()) ;
  276.                                     }
  277.                                     catch
  278.                                     {
  279.                                     }
  280.  
  281.                                 }
  282.                                 myreader.Close();
  283.  
  284.                                 sw.WriteLine("Ending process for EMRID'" + profid + "' AccountID->" + accid);
  285.                                 sw.WriteLine("\n");
  286.                                 sw.Flush();
  287.                                 profid = null;
  288.  
  289.                             }//while close
  290.  
  291.                             try
  292.                             {
  293.  
  294.                                 while (proreader.NextResult()) ;
  295.                             }
  296.  
  297.                             catch
  298.                             {
  299.                             }
  300.  
  301.                         }//if close
  302.  
  303.                         catch
  304.                         {
  305.  
  306.                         }
  307.  
  308.                         sw.WriteLine("application terminated at \n" + currentDateTime);
  309.                         sw.WriteLine("\n");
  310.                         sw.WriteLine("---------------------------------------------------");
  311.                         sw.Flush();
  312.                         proreader.Close();
  313.                     }
  314.  
  315.                 }// try close
  316.  
  317.                 catch 
  318.  
  319.                     {
  320.  
  321.  
  322.                 }
  323.  
  324.  
  325.  
  326.  
  327.  
  328.                 }
  329.  
  330.  
  331.  
  332.             }
  333.         }
Nov 19 '08 #1
4 4532
RedSon
5,000 Recognized Expert Expert
I've added code tags to your post. Please be advised that continuing to not follow forum guidelines could get your account banned. For the location of the guidelines please click help at the top of the page.
Nov 19 '08 #2
RedSon
5,000 Recognized Expert Expert
Dear All,
I am new to C# and I am developing an application which will 24hours run on the remote PC..To transfer records from mysql to access db.I have succesfully created the application and have to handle the ODBC error and standard error.Basically it has to skip the error and move to next record.I really dont know how to use try catch.I am pasting the code here below.Excuse if I am posting new thread for answered question,Thanks in advance.

...snip...
Your basic flow will be like this:

Expand|Select|Wrap|Line Numbers
  1. while (!done)
  2. {
  3.     try
  4.     {
  5.         Transfer records
  6.         if (finished) 
  7.            done = true;
  8.     }
  9.     catch (someError)
  10.     {
  11.         Handle the error
  12.     {
  13. }
  14.  
See now you will loop back around everytime you transfer a record or catch an error. Your code above is too long of an example to be useful. You should post only what is absolutely necessary as an example. No one wants to read through your entire code base.
Nov 19 '08 #3
andyehi
21 New Member
Thank you so much.I will try this.This is my first post on the forum and will post the example goin forward
Nov 19 '08 #4
andyehi
21 New Member
I tried and it worked.Thanks again
Nov 19 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
30324
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program, it quitted with core dumped. %test
11
6895
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill non-supporting browsers? TIA -- --
4
4345
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
11
3493
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
3725
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
3083
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand and datareader object ONLY if certain conditions are met. But, if I want to clean these up in the...
32
6129
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I understand Finally runs whether an error was caught or not, I haven't found a use for finally yet.
23
2333
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
0
9665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.