Solved

Generate HL7 Message

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ConsoleSql 1 34
How to measure sizes and angles in scanned images ? 3 51
What is Thread.Sleep? 5 45
Anyway to make "All" the default in the dropdown? 6 34
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

16 Experts available now in Live!

Get 1:1 Help Now