Solved

Generate HL7 Message

Posted on 2006-07-24
5
500 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 500 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
 

Author Comment

by:jung1975
ID: 17172309
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now