Solved

Generate HL7 Message

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

680 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