Link to home
Start Free TrialLog in
Avatar of jung1975
jung1975

asked on

HL7 message

I've used following code to generate HL7 message


{
 // show generated string
        // retrieve a datatable with all required data from database

        SqlConnection sqlcon = new SqlConnection("server=test; database=Lab; uid=sa; pwd=sa");

        string sqlcmd = "select  (substring(clinic,4,1) + substring(clinic,2,2) )as clinic,[External Code] as pid, firstname, lastname, test, result from Patients a inner join Lab_result b on a.[external code] = b.MRN order by PID, draw_date";



        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd, sqlcon);
        DataSet ds = new DataSet();
        sqlda.Fill(ds, "Patients");
        GridView1.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(@"FHS|^&\~|4||hhh ver. 3.4.05|4|20060705003912" + "\r\n");

                sb.Append(@"BHS|^&\~|4||hhh ver. 3.4.05|4|20060705003912||||20060075003912" + "\r\n");
                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();
         
            string clinic = dr["clinic"].ToString();
         
            // if this is a different patient than the last one, we need a patient header

            if (PID != oldPID)
            {
             

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


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


                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(@"BTS|" + msgCount.ToString() + "\r\n");
        sb.Append(@"FTS|1" + "\r\n");
        TextBox1.Text = sb.ToString();

}


How can I repeat the message header every time there is a new PID

MSH
EVN
~~

also, FT1 is not write in the last PID not sure why??  
In the last PID
PID
OBX||ST|URR^URR||54.0||||||R
OBX||ST|HCT^HGB||12.8||||||R
~~noFT1 here
BTS|1123
FTS|1



and how can I binding the clinicID into clinc field in FT1 line?



the output should  looks like
FHS
BHS
MSH
EVN
PID
OBX
OBX
OBX
FT1
MSH
EVN
PID
OBX
OBX
OBX
FT1
---
etc


BTS|1123
FTS|1

Avatar of tokabi
tokabi

Try replacing the foreach loop with the following for loop, I have moved the MSH & EVN headers inside the loop and added a second section to produce a FT1 record, when the patient ID changes and when the last record has been read. This version will also add the clinic and unitid to the FT1 records (is that 586.6 value the unit_id? if it is the SQL select will need amending to include it):



      for (int i = 0; i < dt.Rows.Count; i++)
      {

            string PID = dt.Rows[i]["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

                  if (msgCount != 0)
                  {    
                        sb.Append(@"FT1||||" + curDate + @"||C|ZZ^Load Lab Results^^"
                              + @"N^N|||1|||" + dt.Rows[i-1]["clinic"] + "||||||"
                              + dt.Rows[i-1]["unit_id"] + "\r\n");
                  }

                  // add patient headers

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

                  sb.Append(@"PID|||" + PID + @"||" + dt.Rows[i]["lastname"].ToString().Trim()
                        + @"^" + dt.Rows[i]["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 (dt.Rows[i]["test"].ToString().Trim())
            {
                  case "%URR":
                        sb.Append(@"OBX||ST|URR^URR||");
                        break;
                  case "HGB":
                        sb.Append(@"OBX||ST|HCT^HGB||");
                        break;
            }

            sb.Append(dt.Rows[i]["result"].ToString().Trim() + @"||||||R" + "\r\n");

            // add trailer for last patient

            if (i == dt.Rows.Count - 1)
            {
                  sb.Append(@"FT1||||" + curDate + @"||C|ZZ^Load Lab Results^^"
                        + @"N^N|||1|||" + dt.Rows[i]["clinic"] + "||||||"
                        + dt.Rows[i]["unit_id"] + "\r\n");
            }
      }
Avatar of jung1975

ASKER

Hi! tokabi,

Actually I fogot to mention about the Lab_date. The query has to be group by pid and labdate

clinic          pid                                labdate                                                 test          result
B76      1000006195      06/12/06          John      doe      %URR          70.5
B76      1000006195      06/12/06                       John      doe       HGB           11.3
B76      1000006195      06/26/06                       John      doe      HGB           11.9

,so there is more than one lab test like above , it has to write another message for 06/26/06 lab date

MSH
EVN
PID
OBX %UPR
OBX  HGB
FT1
MSH
EVN
OBX HGB
FT1
..
..

Alo, I would like to write the incremental number between OBX||ST
, so if there are two OBX,

OBX|1|ST
OBX|2|ST

..

and
I'd like to put the labdate next to FT1 segment

so, it looks FT1||||20060612||

How can I do this?

 
ASKER CERTIFIED SOLUTION
Avatar of tokabi
tokabi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks, i will try it and let you know if i have a question..
tokabi,

the output returns slightly different result than it supposed be..

I put the question on here
https://www.experts-exchange.com/questions/21931822/HL7-message.html