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
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.Odbc;
- using System.IO;
- namespace chart_patient_111108
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- StreamWriter sw;
- string datename = Application.StartupPath + "\\" + DateTime.Now.ToShortDateString() + ".txt";
- DateTime currentDateTime = DateTime.Now;
- datename = datename.Replace("/", "");
- string shortDate = currentDateTime.ToLongDateString();
- textBox3.Text = datename;
- if (!File.Exists(datename))
- {
- //sw = File.CreateText(path +shortDate+ ".txt");
- sw = File.CreateText(datename);
- //sw = File.CreateText(datename+".txt");
- }
- else
- {
- sw = File.AppendText(datename);
- }
- textBox4.Text = datename;
- sw.Flush();
- string sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";
- sw.WriteLine("Application initialized at " + sLogFormat);
- sw.WriteLine("\n\n");
- sw.Flush();
- string profstr = "DRIVER={MySQL ODBC 5.1 Driver};" +
- "SERVER=127.0.0.1;" +
- "DATABASE=andy;" +
- "UID=root;" +
- "PASSWORD=mysql123;" +
- "OPTION=3";
- OdbcConnection myprofcon = new OdbcConnection(profstr);
- myprofcon.Open();
- try
- {
- OdbcCommand procommand = new OdbcCommand("select accountid,server,userid,password,EMRID,mdbpath,mdbuserid,mdbpassword from profiledb where active = 'y'", myprofcon);
- OdbcDataReader proreader = procommand.ExecuteReader(CommandBehavior.CloseConnection);
- if (proreader.HasRows)
- {
- try
- {
- while (proreader.Read())
- {
- string profid = proreader["EMRID"].ToString();
- string accid = proreader["accountid"].ToString();
- string server = proreader["server"].ToString();
- string userid = proreader["userid"].ToString();
- string password = proreader["password"].ToString();
- string mdbpath = proreader["mdbpath"].ToString();
- string mdbuserid = proreader["mdbuserid"].ToString();
- string mdbpassword = proreader["mdbpassword"].ToString();
- string MyConString = "DRIVER={MySQL ODBC 5.1 Driver};" +
- "SERVER=" + server + ";" +
- "DATABASE=" + profid + ";" +
- "UID=" + userid + ";" +
- "PASSWORD=" + password + ";" +
- "OPTION=3";
- OdbcConnection MyConnection = new OdbcConnection(MyConString);
- MyConnection.Open();
- sw.WriteLine(" Starting process for EMR ID '" + profid + "' AccountID->" + accid);
- sw.WriteLine("\n");
- sw.Flush();
- OdbcConnection mdbconn = new OdbcConnection();
- //mdbconn.ConnectionString =
- // "Driver={Microsoft Access Driver (*.mdb)};" +
- // "Dbq=E:/Documents and Settings/Ananjeev/Desktop/Task 29th/client.mdb;" +
- // "Uid=Admin;Pwd=;";
- mdbconn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" +
- "Dbq=" + mdbpath + ";" +
- "Uid=" + mdbuserid + ";Pwd=" + mdbpassword + ";";
- try
- {
- mdbconn.Open();
- }
- catch
- {
- }
- OdbcCommand MyCommand = new OdbcCommand("select * from chart", MyConnection);
- //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
- OdbcDataReader myreader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
- if (myreader.HasRows)
- {
- while (myreader.Read())
- {
- string MRN1 = (string)myreader["MRN"];
- string ssn1 = (string)myreader["ssn"];
- string sex1 = myreader["sex"].ToString();
- string dob1 = myreader["DOB"].ToString();
- string firstname1 = myreader["firstname"].ToString();
- string lastname1 = myreader["lastname"].ToString();
- string CI_address11 = myreader["CI_address1"].ToString();
- string CI_address21 = myreader["CI_address2"].ToString();
- string CI_city1 = myreader["CI_city"].ToString();
- string CI_state1 = myreader["CI_state"].ToString();
- string CI_zip1 = myreader["CI_zip"].ToString();
- OdbcCommand mycommand2 = new OdbcCommand("select * from patientinfo where patientid ='" + MRN1 + "' ", mdbconn);
- OdbcDataReader myreader2 = mycommand2.ExecuteReader(CommandBehavior.CloseConnection);
- if (myreader2.HasRows)
- {
- 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);
- int nrows = mycommand3.ExecuteNonQuery();
- textBox1.Text = Convert.ToString(nrows);
- if (nrows > 0)
- {
- sw.WriteLine("\t" + sLogFormat + " update:True on MRN '" + MRN1 + "' AccountID->" + accid);
- sw.Flush();
- }
- else
- {
- sw.WriteLine("\t" + sLogFormat + " update:False on MRN '" + MRN1 + "'AccountID->" + accid);
- sw.Flush();
- }
- }
- else
- {
- 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);
- int k = mycommand4.ExecuteNonQuery();
- textBox2.Text = Convert.ToString(k);
- if (k > 0)
- {
- sw.WriteLine("\t" + sLogFormat + " Insert:True on MRN '" + MRN1 + "'AccountID->" + accid);
- sw.Flush();
- }
- else
- {
- sw.WriteLine("\t" + sLogFormat + " Insert:False on MRN '" + MRN1 + "'AccountID->" + accid);
- sw.Flush();
- }
- }
- MRN1 = null;
- ssn1 = null;
- sex1 = null;
- dob1 = null;
- firstname1 = null;
- lastname1 = null;
- CI_address11 = null;
- CI_address21 = null;
- CI_city1 = null;
- CI_state1 = null;
- CI_zip1 = null;
- /// emrid = null;
- accid = null;
- server = null;
- userid = null;
- password = null;
- }
- try
- {
- while (myreader.NextResult()) ;
- }
- catch
- {
- }
- }
- myreader.Close();
- sw.WriteLine("Ending process for EMRID'" + profid + "' AccountID->" + accid);
- sw.WriteLine("\n");
- sw.Flush();
- profid = null;
- }//while close
- try
- {
- while (proreader.NextResult()) ;
- }
- catch
- {
- }
- }//if close
- catch
- {
- }
- sw.WriteLine("application terminated at \n" + currentDateTime);
- sw.WriteLine("\n");
- sw.WriteLine("---------------------------------------------------");
- sw.Flush();
- proreader.Close();
- }
- }// try close
- catch
- {
- }
- }
- }
- }