473,378 Members | 1,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

C# try catch

21
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 4506
RedSon
5,000 Expert 4TB
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 Expert 4TB
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
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
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
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,...
11
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...
4
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
11
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
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
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...
32
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...
23
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);
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.