?
Solved

Generate HL7 Message

Posted on 2006-07-24
5
Medium Priority
?
617 Views
Last Modified: 2006-11-18
I'd like to create a C# program that generate the HL7 messages by rooping through the table in database.
I have two tables: Patient and lab result.

I am trying to looping through this tables and generate the HL7 message. some fields are default and some fields are comeing from the actaul table

Please see the comments

For example,

Here is the data in the tables:
PId                 lastname firstname
19191919       John        Rick

Test         Result    name
URR        79          John, rick
HGB       11.2        John, rick


and the HL 7 message should look like:

MSH|^&\~|NAPPY|||4|20060705003913||DFT^P03|0|P|2.2
EVN|P03|20060705003913||OVR
PID|||19191919||John^Rick  -- -----------> Patient Id 19191919 , and last and first name John^Rick comes from the dataabse
OBX||ST|URR^URR||79||||||R ------------>Lab value: 79 comes from the database
OBX||ST|HCT^HGB||11.2||||||R ----------------->Lab value:11.2 comes from the database
FT1||||20060601||C|ZZ^Load Lab Results^^N^N|||1|||A230||||||586.6 ----------------> A230 comes from the database
BTS|1 -------------------> count of message in file
FTS|1


what is the best way to do this? can you show me some examples?
0
Comment
Question by:jung1975
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:tokabi
ID: 17170769
This is an example of something similar, I've just hacked it a bit to see if it fits your requirements:

// retrieve a datatable with all required data from database

sqlcon = new SqlConnection("server=ODIN; database=teststuff; uid=sa; pwd=????????");

string sqlcmd = "select pid, firstname, lastname, test, result, name from Patient"
      + " inner join Labresult on name = rtrim(lastname) + ', ' + rtrim(firstname)";

sqlda = new SqlDataAdapter(sqlcmd, sqlcon);
DataSet ds = new DataSet();
sqlda.Fill(ds, "Patients");
dataGrid1.DataSource = ds;
DataTable dt = ds.Tables["Patients"];

// need to add code to set date in correct format

string curDate = "20060705003913";
string oldPID = "";
int msgCount = 0;

System.Text.StringBuilder sb = new System.Text.StringBuilder();

// build file headers

sb.Append(@"MSH|^&\~|NAPPY|||4|" + curDate + @"||DFT^P03|0|P|2.2" + "\r\n");

sb.Append(@"EVN|P03|" + curDate + @"||OVR" + "\r\n");

foreach (DataRow dr in dt.Rows)
{
      string PID = dr["PID"].ToString();
      
      // if this is a different patient than the last one, we need a patient header

      if (PID != oldPID)
      {

            // and if its not the first patient we need a patient trailer record
            // (need to determine where the A230 value comes from)

            if (msgCount > 0)
            {
                  sb.Append(@"FT1||||" + curDate + @"||C|ZZ^Load Lab Results^^"
                        + @"N^N|||1|||A230||||||" + "586.6" + "\r\n");
            }

            sb.Append(@"PID|||" + PID + @"||" + dr["lastname"].ToString().Trim()
                  + @"^" + dr["firstname"].ToString().Trim() + "\r\n");
            msgCount++;
            oldPID = PID;
      }

      // determine which test it is (could just substitiue test name from
      // database if that's appropriate

      switch (dr["test"].ToString().Trim())
      {
            case "URR" :
                  sb.Append(@"OBX||ST|URR^URR||");
                  break;
            case "HGB" :
                  sb.Append(@"OBX||ST|HCT^HGB||");
                  break;
      }

      sb.Append(dr["result"].ToString().Trim() + @"||||||R" + "\r\n");
}

// add trailer for last patient and file trailers with patient count

sb.Append(@"FT1||||" + curDate + @"||C|ZZ^Load Lab Results^^"
      + @"N^N|||1|||A230||||||" + "586.6" + "\r\n");

sb.Append(@"BTS|" + msgCount.ToString() + "\r\n");
sb.Append(@"FTS|" + msgCount.ToString() + "\r\n");

// show generated string

textBox1.Text = sb.ToString();

It seems to work ok, but there's a few things to consider. Firstly it's setup for SQl Server, which may not be the databse you're using but that's easily fixed. Also it's assuming the records for multiple patients will be in the format:

Header
Patient Header
Patient Test
Patient Test
Patient Trailer
Patient Header
Patient Test
Patient Trailer
e.t.c.
Trailer

it will need rearranging a little if this is not the case. Finally you haven't said where the A230 value comes from. This will obviously need adding to the SQL query.
0
 

Author Comment

by:jung1975
ID: 17170948
Thank you very much.. A230 is comming from the database as well
Can I save the result to text file? result.txt instead of showing in the textbox?
0
 
LVL 5

Accepted Solution

by:
tokabi earned 2000 total points
ID: 17171263
something simple like:

System.IO.StreamWriter writer = new System.IO.StreamWriter(@"C:\result.txt",false);
writer.Write(sb.ToString());
writer.Close();

will write the output to a file, if the output is huge it will be safer to break it up into smaller blocks but for a few thousand characters it should be fine.

Is A230 a column in patients or is it another test in Lab result? or something else altogether?
0
 

Author Comment

by:jung1975
ID: 17171661
It's  comming from the patient table( unit_id).

I will try your code and let you know if I have a questions in another Q, so I can give you more points.
thank you for all your help
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month9 days, 6 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question