[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

11/04/2009 at 06:44PM PST, ID: 24873329 | Points: 500
[x]
Attachment Details

.NET 2.0 TableAdapter Fill in Called Library Class Returns Oracle ORA-12154 Sometimes But OK Others

Asked by phildtm in C# Programming Language, Oracle CRM, Programming for ASP.NET

Tags: .NET TableAdapter C# Oracle

I have a C# library class that includes a TableAdapter whose Fill method populates a Datasource associated with a ReportingServices report.  I have two separate ASP.NET programs that use this library module, passing in appropriate values to the class constructor used to format the desired report output.  One of them works fine and the report generates as expected.  When the SAME module is called from a second program (also ASP.NET and similar in most ways to  the first), the very same, unchanged called library module - when attempting to execute the Fill method of the TableAdapter - returns an Oracle ORA-12154 errror - "TNS - could not resolve service name".  The arguments passed in both cases are of the same kind, using the same constructor.  Essentially here is the mystery: how could the very same code be returning ORA-12154 when called from one program, and have no difficulty connecting to the database when called from a different program?   I will post code if anyone feels it would be helpful.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515:
516:
517:
518:
519:
520:
521:
522:
523:
524:
525:
526:
527:
528:
529:
530:
531:
532:
533:
534:
535:
536:
537:
538:
539:
540:
541:
542:
543:
544:
545:
546:
547:
548:
549:
550:
551:
552:
553:
554:
555:
556:
557:
558:
559:
560:
561:
562:
563:
564:
565:
566:
567:
568:
569:
570:
571:
572:
573:
574:
575:
576:
577:
578:
579:
580:
581:
582:
583:
584:
585:
586:
587:
588:
589:
590:
591:
592:
593:
594:
595:
596:
597:
598:
599:
600:
601:
602:
603:
604:
605:
606:
607:
608:
609:
610:
611:
612:
613:
614:
615:
616:
617:
618:
619:
620:
621:
622:
623:
624:
625:
626:
627:
628:
629:
630:
631:
632:
633:
634:
635:
636:
637:
638:
639:
640:
641:
642:
643:
644:
645:
646:
647:
648:
649:
650:
651:
652:
653:
654:
655:
656:
657:
658:
659:
660:
661:
662:
663:
664:
665:
666:
667:
668:
669:
670:
671:
672:
673:
674:
675:
676:
677:
678:
679:
680:
681:
682:
683:
684:
685:
686:
687:
688:
689:
690:
691:
692:
693:
694:
695:
696:
697:
698:
699:
700:
701:
702:
703:
704:
705:
706:
707:
708:
709:
710:
711:
712:
713:
714:
715:
716:
717:
718:
719:
720:
721:
722:
723:
724:
725:
726:
727:
728:
729:
730:
731:
732:
733:
734:
735:
736:
737:
738:
739:
740:
741:
742:
743:
744:
745:
746:
747:
748:
749:
750:
751:
752:
753:
754:
755:
756:
757:
758:
759:
760:
761:
762:
763:
764:
765:
766:
767:
768:
769:
770:
771:
772:
773:
774:
775:
776:
777:
778:
779:
780:
781:
782:
783:
784:
785:
786:
787:
Code that calls the library module:
AJ_PrintContainerLabel labelprint = new AJ_PrintContainerLabel(container_for_filename, getEssUserId(), this.connectString);
                
                this.txtDefaultPrinter.Text = labelprint.ProcessLabel();
 
Library module:  (2 separate .cs files)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Neodynamic.SDK;
using Ess.Framework.DA;
using System.IO;
//using AJ_ContainerLabelPrint;
 
namespace AJ_ContainerLabelPrint
{
    /// <summary>
    /// Summary description for AJ_PrintContainerLabel
    /// </summary>
    public class AJ_PrintContainerLabel
    {
 
        ContainerLabelRSTableAdapters.DataTable1TableAdapter ta = new ContainerLabelRSTableAdapters.DataTable1TableAdapter();
        ContainerLabelRS.DataTable1DataTable dt = new ContainerLabelRS.DataTable1DataTable();
        
        string Applpath = AppDomain.CurrentDomain.BaseDirectory.ToString().Replace(@"Aerojet\ContainerLabelRS", "Aerojet");
        string Querypath = String.Empty;
        private string ess_user_id;
        private string ess_connect_string;
        private string[] containers_to_process;
        
        public AJ_PrintContainerLabel(string container_num, string user_id, string connect_string)
        {
            //
            // TODO: Add constructor logic here
            //
            string[] selected_containers = new string[1];
            selected_containers[0] = container_num;
            containers_to_process = selected_containers;
            ess_connect_string = connect_string;
            ess_user_id = user_id;
            ta.Fill(dt);
        }
 
        public AJ_PrintContainerLabel(string[] selected_containers, string user_id, string connect_string)
        {
            
            //
            // TODO: Add constructor logic here
            //
            containers_to_process = selected_containers;
            ess_connect_string = connect_string;
            ess_user_id = user_id;
        }
 
        public string ProcessLabel()
        {
            //ta.Connection = TAFormatConnectString(ess_connect_string, "xxx");
            //throw new Exception("Our connect string is " + ess_connect_string);
            string config_connstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            config_connstr = TAFormatConnectString(ess_connect_string, config_connstr);
            ta.Connection.ConnectionString = config_connstr;
            ta.Fill(dt);
            
            BarcodeProfessional.LicenseOwner = "AeroJet-Ultimate Edition-Corporate License";
            BarcodeProfessional.LicenseKey = "ECMFSPYWDDN8CNC5JHVPACPVPYZHNLBKBP8C8MYRLBU8L2HEG2PA";
            BarcodeProfessional bcp = new BarcodeProfessional();
            //Barcode settings
            bcp.Symbology = Symbology.Code39;
            bcp.BarHeight = 0.35f;
            bcp.AddChecksum = false;
            dt = dt_mod(dt);
            string print_label_rc = String.Empty;
            foreach (string container_num in containers_to_process)
            {
                if (container_num.Length > 4)
                  print_label_rc = PrintLabel(container_num, ess_user_id, dt);
            }
            //return "Successful Label Print";
            return print_label_rc;
        }
 
        public ContainerLabelRS.DataTable1DataTable dt_mod(ContainerLabelRS.DataTable1DataTable dt)
        {
            string container_for_filename = String.Empty;
            string hold_container_num = "xxx";
            bool new_container = false;
            int same_container_pass = 0;
            string[] hazard_text = new string[5];
            string[] comp_label = new string[24];
            string[] matl_label = new string[24];
            string[] maj_min = new string[24];
            string[] lower_hdg = new string[24];
            string[] upper_hdg = new string[24];
            string[] lower_val = new string[24];
            string[] upper_val = new string[24];
            string[] comp_mat_name = new string[24];
            string matl_hdg = "__________________________Material______________________________";
            // initialize string arrays to spacek
            string spacek = String.Empty;
            bool print_hazard_label = true;
            bool print_components = true;
            Querypath = Applpath + @"sql\";
            BarcodeProfessional bcp = new BarcodeProfessional();
            //Barcode settings
            bcp.Symbology = Symbology.Code39;
            bcp.BarHeight = 0.35f;
            bcp.AddChecksum = false;
            DataTable dt_components = new DataTable();
            int dt_rowcount = 0;
            // loop through all selected containers and print one at a time
            //int l = containers
            ContainerLabelRS.DataTable1DataTable dtprint =
                   new ContainerLabelRS.DataTable1DataTable();
            for (int j = 0; j < containers_to_process.Length; j++)
            {
                dtprint = (ContainerLabelRS.DataTable1DataTable)dt.Copy();
                foreach (ContainerLabelRS.DataTable1Row row in dtprint.Rows)
                {
                    if (!(isSelectedContainers(row.CONTAINER_NUM, containers_to_process)))
                    {
                        row.Delete();
                        continue;
                    }
 
                    // Determine if we're looking at a new container
                    if (!(row.CONTAINER_NUM.Equals(hold_container_num)))
                    {
                        new_container = true;
                        same_container_pass = 1;
                        for (int h = 0; h < 5; h++)
                        {
                            hazard_text[h] = String.Empty;
                        }
                    }
                    else // looking at more records for same container - only diff is hazard text
                    {
                        same_container_pass += 1;
                    }
 
                    if (same_container_pass > 1)
                        goto BYPASS_COMPONENT_RETRIEVE;
                    // initialize and retrieve component values
                    string filename = Querypath + "waste_container_label_components.sql";
                    string sql = getFileAsString(filename);
                    sql = sql.Replace("#CONTAINER_NUM#", row.CONTAINER_NUM);
                    //sql = sql.Replace(@"where CONTAINER_NUM = '#CONTAINER_NUM#'", String.Empty);
                    System.Data.DataSet DS;
                    //string connStr = base.connectString;
                    string connStr = ess_connect_string;
                    daReader reader = new daReader(connStr);
                    DS = reader.getDataSet(sql);
                    dt_components = DS.Tables[0];
                    dt_rowcount = dt_components.Rows.Count;
 
                    //Set the value to encode
                    //bcp.Code = row.ProductID.ToString();
                    bcp.Code = row.CONTAINER_NUM.ToString();
                    if (container_for_filename.Equals(String.Empty))
                    {
                        container_for_filename = bcp.Code;
                    }
                    //Generate the barcode image and store it into the Barcode Column
                    row.BARCODE = bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png);
 
                    print_hazard_label = true;
                    print_components = true;
                    bool first_major = true;
                    bool first_minor = true;
                    if (dt_rowcount > 0)
                    {
                        for (int i = 0; i < 24; i++)
                        {
                            comp_label[i] = spacek;
                            matl_label[i] = spacek;
                            maj_min[i] = spacek;
                            lower_hdg[i] = spacek;
                            upper_hdg[i] = spacek;
                            lower_val[i] = spacek;
                            upper_val[i] = spacek;
                        }
                        j = 0;
                        int max_rows = dt_rowcount;
                        if (max_rows > 24)
                        {
                            max_rows = 24;
                        }
                        for (int i = 0; i < max_rows; i++)
                        {
                            string curr_maj_min = dt_components.Rows[i]["MAJ_MIN"].ToString();
                            if (curr_maj_min.Equals("MAJ") && (first_major))
                            {
                                comp_label[j] = "Major Components:";
                                matl_label[j] = matl_hdg;
                                lower_val[j] = dt_components.Rows[i]["COMP_HDG4"].ToString();
                                upper_val[j] = dt_components.Rows[i]["COMP_HDG3"].ToString();
                                first_major = false;
                                j++;
                            }
                            else if (curr_maj_min.Equals("MIN") && (first_minor))
                            {
                                // create blank line before minor component group
                                comp_label[j] = spacek;
                                matl_label[j] = spacek;
                                lower_val[j] = spacek;
                                upper_val[j] = spacek;
                                j++;
                                // now print minor values
                                comp_label[j] = "Minor Components:";
                                matl_label[j] = matl_hdg;
                                lower_val[j] = dt_components.Rows[i]["COMP_HDG4"].ToString();
                                upper_val[j] = dt_components.Rows[i]["COMP_HDG3"].ToString();
                                first_minor = false;
                                j++;
                            }
                            comp_label[j] = spacek;
                            matl_label[j] = dt_components.Rows[i]["COMP_MAT_NAME"].ToString();
                            lower_val[j] = dt_components.Rows[i]["LODI_VAL"].ToString();
                            upper_val[j] = dt_components.Rows[i]["OPRAH_VAL"].ToString();
                            j++;
                            //throw new Exception("In array loop");
                            if (j == 24)
                                break;
                        }
                    }
 
                BYPASS_COMPONENT_RETRIEVE:
                    // end component value initialize
                    //Set the value to encode
                    //bcp.Code = row.ProductID.ToString();
                    bcp.Code = row.CONTAINER_NUM.ToString();
                    //gahuga = "in row processing loop";
                    row.VLD_WST_CLASS = row.VLD_WST_CLASS.ToUpper();
                    row.MAT_NAME = row.MAT_NAME.ToUpper();
                    //string spacek = String.Empty;
                    int k = 0;
                    if ((dt_rowcount > 0) && (print_components))
                    {
                        row.COMP_LIT01A = comp_label[k];
                        row.COMP_LIT01B = matl_label[k];
                        row.COMP_LIT01C = upper_val[k];
                        row.COMP_LIT01D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT02A = comp_label[k];
                        row.COMP_LIT02B = matl_label[k];
                        row.COMP_LIT02C = upper_val[k];
                        row.COMP_LIT02D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT03A = comp_label[k];
                        row.COMP_LIT03B = matl_label[k];
                        row.COMP_LIT03C = upper_val[k];
                        row.COMP_LIT03D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT04A = comp_label[k];
                        row.COMP_LIT04B = matl_label[k];
                        row.COMP_LIT04C = upper_val[k];
                        row.COMP_LIT04D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT05A = comp_label[k];
                        row.COMP_LIT05B = matl_label[k];
                        row.COMP_LIT05C = upper_val[k];
                        row.COMP_LIT05D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT06A = comp_label[k];
                        row.COMP_LIT06B = matl_label[k];
                        row.COMP_LIT06C = upper_val[k];
                        row.COMP_LIT06D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT07A = comp_label[k];
                        row.COMP_LIT07B = matl_label[k];
                        row.COMP_LIT07C = upper_val[k];
                        row.COMP_LIT07D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT08A = comp_label[k];
                        row.COMP_LIT08B = matl_label[k];
                        row.COMP_LIT08C = upper_val[k];
                        row.COMP_LIT08D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT09A = comp_label[k];
                        row.COMP_LIT09B = matl_label[k];
                        row.COMP_LIT09C = upper_val[k];
                        row.COMP_LIT09D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT10A = comp_label[k];
                        row.COMP_LIT10B = matl_label[k];
                        row.COMP_LIT10C = upper_val[k];
                        row.COMP_LIT10D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT11A = comp_label[k];
                        row.COMP_LIT11B = matl_label[k];
                        row.COMP_LIT11C = upper_val[k];
                        row.COMP_LIT11D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT12A = comp_label[k];
                        row.COMP_LIT12B = matl_label[k];
                        row.COMP_LIT12C = upper_val[k];
                        row.COMP_LIT12D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT13A = comp_label[k];
                        row.COMP_LIT13B = matl_label[k];
                        row.COMP_LIT13C = upper_val[k];
                        row.COMP_LIT13D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT14A = comp_label[k];
                        row.COMP_LIT14B = matl_label[k];
                        row.COMP_LIT14C = upper_val[k];
                        row.COMP_LIT14D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT15A = comp_label[k];
                        row.COMP_LIT15B = matl_label[k];
                        row.COMP_LIT15C = upper_val[k];
                        row.COMP_LIT15D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT16A = comp_label[k];
                        row.COMP_LIT16B = matl_label[k];
                        row.COMP_LIT16C = upper_val[k];
                        row.COMP_LIT16D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT17A = comp_label[k];
                        row.COMP_LIT17B = matl_label[k];
                        row.COMP_LIT17C = upper_val[k];
                        row.COMP_LIT17D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT18A = comp_label[k];
                        row.COMP_LIT18B = matl_label[k];
                        row.COMP_LIT18C = upper_val[k];
                        row.COMP_LIT18D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT19A = comp_label[k];
                        row.COMP_LIT19B = matl_label[k];
                        row.COMP_LIT19C = upper_val[k];
                        row.COMP_LIT19D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT20A = comp_label[k];
                        row.COMP_LIT20B = matl_label[k];
                        row.COMP_LIT20C = upper_val[k];
                        row.COMP_LIT20D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT21A = comp_label[k];
                        row.COMP_LIT21B = matl_label[k];
                        row.COMP_LIT21C = upper_val[k];
                        row.COMP_LIT21D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT22A = comp_label[k];
                        row.COMP_LIT22B = matl_label[k];
                        row.COMP_LIT22C = upper_val[k];
                        row.COMP_LIT22D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT23A = comp_label[k];
                        row.COMP_LIT23B = matl_label[k];
                        row.COMP_LIT23C = upper_val[k];
                        row.COMP_LIT23D = lower_val[k];
                        k++;
                        // NEXT LINE
                        row.COMP_LIT24A = comp_label[k];
                        row.COMP_LIT24B = matl_label[k];
                        row.COMP_LIT24C = upper_val[k];
                        row.COMP_LIT24D = lower_val[k];
                        k++;
                    }
 
                    //Generate the barcode image and store it into the Barcode Column
                    row.BARCODE = bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png);
 
                    if (!(row.IsNull("GEN_PROPERTY_PARAM")) && print_hazard_label)
                    {
                        //throw new Exception("null test was true");
                        row.HAZLIT_LBL = "Hazard Characteristics:";
                        switch (same_container_pass)
                        {
                            case 1:
                                row.HAZLIT1 = row.GEN_PROPERTY_PARAM;
                                row.HAZLIT2 = spacek;
                                row.HAZLIT3 = spacek;
                                row.HAZLIT4 = spacek;
                                row.HAZLIT5 = spacek;
                                hazard_text[0] = row.GEN_PROPERTY_PARAM;
                                break;
                            case 2:
                                row.HAZLIT1 = hazard_text[0];
                                row.HAZLIT2 = row.GEN_PROPERTY_PARAM;
                                row.HAZLIT3 = spacek;
                                row.HAZLIT4 = spacek;
                                row.HAZLIT5 = spacek;
                                hazard_text[1] = row.GEN_PROPERTY_PARAM;
                                break;
                            case 3:
                                row.HAZLIT1 = hazard_text[0];
                                row.HAZLIT2 = hazard_text[1];
                                row.HAZLIT3 = row.GEN_PROPERTY_PARAM;
                                row.HAZLIT4 = spacek;
                                row.HAZLIT5 = spacek;
                                hazard_text[2] = row.GEN_PROPERTY_PARAM;
                                break;
                            case 4:
                                row.HAZLIT1 = hazard_text[0];
                                row.HAZLIT2 = hazard_text[1];
                                row.HAZLIT3 = hazard_text[2];
                                row.HAZLIT4 = row.GEN_PROPERTY_PARAM;
                                row.HAZLIT5 = spacek;
                                hazard_text[3] = row.GEN_PROPERTY_PARAM;
                                break;
                            case 5:
                                row.HAZLIT1 = hazard_text[0];
                                row.HAZLIT2 = hazard_text[1];
                                row.HAZLIT3 = hazard_text[2];
                                row.HAZLIT4 = hazard_text[3];
                                row.HAZLIT5 = row.GEN_PROPERTY_PARAM;
                                //hazard_text[4] = row.GEN_PROPERTY_PARAM;
                                break;
                        }
                    }
                    else
                    {
                        row.HAZLIT_LBL = spacek;
                        row.HAZLIT1 = spacek;
                        row.HAZLIT2 = spacek;
                        row.HAZLIT3 = spacek;
                        row.HAZLIT4 = spacek;
                        row.HAZLIT5 = spacek;
                    }
 
                    //print_hazard_label = false;
                    //print_components = false;
                    hold_container_num = row.CONTAINER_NUM;
                    new_container = false;
 
                }
 
                dtprint.AcceptChanges();
 
                // 2nd Pass through data table to delete duplicate records for container
                hold_container_num = "xxx";
                for (int del = dtprint.Rows.Count - 1; del >= 0; del--)
                {
                    if (!(dtprint.Rows[del]["CONTAINER_NUM"].ToString().Equals(hold_container_num)))
                    {
                        hold_container_num = dtprint.Rows[del]["CONTAINER_NUM"].ToString();
 
                    }
                    else // delete because not the last record for container
                    {
                        dtprint.Rows[del].Delete();
                        //throw new Exception("row delete on table backscan");
                    }
                }
 
                dtprint.AcceptChanges();
            }
            return dtprint;
        }
            
        
        
        public string PrintLabel(string container_num, string user_id, ContainerLabelRS.DataTable1DataTable dt)
        {
            // TODO: setup table adapter code
            // TODO: barcode fields
            // TODO: connect string
            // TODO: generate datasource
            foreach (ContainerLabelRS.DataTable1Row row in dt.Rows)
            { 
                if (!(row.CONTAINER_NUM.Equals(container_num)))
                {
                    row.Delete();
                    continue;
                }
            }
            dt.AcceptChanges();
 
            Microsoft.Reporting.WebForms.ReportDataSource rptDataSource = new Microsoft.Reporting.WebForms.ReportDataSource("ContainerLabelRS_DataTable1", dt);
            AJ_PrintLocal_DirectToPrinter printlocal = new AJ_PrintLocal_DirectToPrinter();
            //string rpt_userid = getEssUserId();
 
            string sql = "select AJ_CONTACT_CONTAINER_PRINTER from VLD_CONTACT where CONTACT_IDEN = ";
            sql += "'" +  user_id + "'";
            //throw new Exception("the printer lookup sql is " + sql);
            daReader reader = new daReader(ess_connect_string);
            DataSet DS = reader.getDataSet(sql);
            string container_printer = "No container printer has yet been assigned for direct hardcopy output.";
            if (DS.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < DS.Tables[0].Rows.Count; i++)
                {
                    container_printer = DS.Tables[0].Rows[i][0].ToString();
 
                    if (!(container_printer.Contains(@"\")))
                    {
                        return "No container printer has yet been assigned for direct hardcopy output.";
                    }
                }
            }
            //string rc = printlocal.Process_Report_Service("containerlabelrs.rdlc", "T07089", @"\\SACPRINT\51_148_4730", container_num, ref rptDataSource);
            string rc = printlocal.Process_Report_Service("containerlabelrs.rdlc", user_id, container_printer, container_num, ref rptDataSource);
            printlocal.Dispose();
            return rc;
        }
 
        private bool isSelectedContainers(string container_num, string[] selected_containers)
        {
            //throw new Exception("selected_containers lgth is " + selected_containers.Length.ToString());
            // now we do the actual update loop through the grid
            // Enumerate the GridViewRows
            for (int index = 0; index < selected_containers.Length; index++)
            {
                if (selected_containers[index].Equals("xxx"))
                    break;
                if (selected_containers[index].Equals(container_num))
                {
                    return true;
                }
            }
            return false;
 
        }
 
        /// <summary>
    /// getFileAsString uses the StreamReader class to retrieve the contents of a file
    /// (.sql) into a string.
    /// </summary>
    /// <param name="fileName"></param>
    /// <returns></returns>
    private static string getFileAsString(string fileName)
    {
        System.IO.StreamReader sReader = null;
        string contents = null;
        try
        {
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            sReader = new StreamReader(fileStream);
            contents = sReader.ReadToEnd();
        }
        finally
        {
            if (sReader != null)
            {
                sReader.Close();
            }
        }
        return contents;
    }
 
    private string TAFormatConnectString(string before_connect_string, string ta_format_connect_string)
    {
        // connect string format as found in web.config: 
        // Provider=OraOleDB.Oracle;chunksize=8000;User ID=user_id;Password=passwd;Data Source=ehsxxxx 
        //Provider=OraOleDB.Oracle;chunksize=8000;User ID=T07089;Password=T07089; Data Source=ehs10dev
        // connect string format as required by OracleConnection is minus Provider and chunksize
        // User ID=user_id;Password=passwd;Data Source=ehsxxxx 
        //string ta_format_connect_string=
        //throw new Exception("ta connect string is " + ta_format_connect_string);
        string datasrce = before_connect_string.Substring(before_connect_string.IndexOf("Data Source=") + 12);
        //before_connect_string.IndexOf(";Pass") - before_connect_string.IndexOf("ID=") - 3);
        string userid = before_connect_string.Substring(before_connect_string.IndexOf("User ID=") + 8,
            before_connect_string.IndexOf(";Pass") - before_connect_string.IndexOf("ID=") - 3);
        string passwd = before_connect_string.Substring(before_connect_string.IndexOf("Password=") + 9,
            before_connect_string.IndexOf("Data Sou") - before_connect_string.IndexOf("word=") - 7);
        //throw new Exception("Password is " + passwd);
        ta_format_connect_string = ta_format_connect_string.Replace("userid", "pwsys");
        ta_format_connect_string = ta_format_connect_string.Replace("datasrce", datasrce);
        ta_format_connect_string = ta_format_connect_string.Replace("passwd", "pwsys");
        //throw new Exception("returning string is " + ta_format_connect_string);
        return ta_format_connect_string;
        //return a_dbconn.ConnectionString;
    }
 
    }
}
 
2nd .cs File:
using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Collections.Generic;
using Ess.Framework.WebControls;
using Ess.Framework.DA;
using Microsoft.Reporting.WebForms;
using System.Security.Principal;
using Ess.Framework.Security;
using System.Diagnostics;
//using local.gencorp.aerojet.sacehsdev;
 
namespace AJ_ContainerLabelPrint
{
    /// <summary>
    /// Summary description for AJ_PrintLocal_DirectToPrinter
    /// </summary>
    public class AJ_PrintLocal_DirectToPrinter : IDisposable
    {
        private int m_currentPageIndex;
        private IList<Stream> m_streams;
        private string emf_files;
        private int emf_cnt;
        private bool process_ok;
        //string EMF_Filepath = AppDomain.CurrentDomain.BaseDirectory.ToString() + @"tempfiles\";
        string EMF_Filepath = @"c:\temp\emffiles\";
        string compound_filename_parts;
 
        public AJ_PrintLocal_DirectToPrinter()
        {
            //
            // TODO: Add constructor logic here
            //
        }
 
 
        // Routine to provide to the report renderer, in order to
        //    save an image for each page of the report.
        //private MemoryStrean memstrom;
        private Stream CreateStream(string name,
          string fileNameExtension, Encoding encoding,
          string mimeType, bool willSeek)
        {
            string name_renamed = name.Replace("containerlabelrs", compound_filename_parts);
            Stream stream = new FileStream(EMF_Filepath + name_renamed +
               "." + fileNameExtension, FileMode.Create);
            m_streams.Add(stream);
            emf_files += EMF_Filepath + name_renamed +
               "." + fileNameExtension + " ";
            emf_cnt += 1;
            //throw new Exception("File is " + whatis_file);
 
            return stream;
        }
 
        private string ConstructFileName(string container_num, string report, string username, string printerName)
        {
            // Construct file name using file report type, user, container, and printer by deconstructing file name
            // _274152_CLBL_E99999_$$SACPRINT$51_148_4730.emf - container label for user E99999 for printer \\SACPRINT\51_148_4730 for cntr 274152
            // _274152_CPKP_E99999_$$SACPRINT$51_148_4730.emf - container pickup rqst as above
            // _274152_CDLV_E99999_$$SACPRINT$51_148_4730.emf - container delivery rqst as above
            printerName = printerName.Replace(@"\", "$");
            return "_" + container_num + "_" + "CLBL" + "_" + username + "_" + printerName + "#";
        }
 
        // Export the given report as an EMF (Enhanced Metafile) file.
        private void Export(LocalReport report)
        {
            string deviceInfo =
              "<DeviceInfo>" +
              "  <OutputFormat>EMF</OutputFormat>" +
              "  <PageWidth>8.5in</PageWidth>" +
              "  <PageHeight>11in</PageHeight>" +
              "  <MarginTop>0.25in</MarginTop>" +
              "  <MarginLeft>0.0in</MarginLeft>" +
              "  <MarginRight>0.0in</MarginRight>" +
              "  <MarginBottom>0.25in</MarginBottom>" +
              "</DeviceInfo>";
            Warning[] warnings;
            m_streams = new List<Stream>();
            report.Render("Image", deviceInfo, CreateStream,
               out warnings);
            foreach (Stream stream in m_streams)
                stream.Position = 0;
        }
        // Handler for PrintPageEvents
        private void PrintPage(object sender, PrintPageEventArgs ev)
        {
            Metafile pageImage = new
               Metafile(m_streams[m_currentPageIndex]);
            ev.Graphics.DrawImage(pageImage, ev.PageBounds);
            m_currentPageIndex++;
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
        }
 
        private string Print(string printerName)
        {
            using (new ImpersonationScope("T07089", Environment.UserDomainName, "$Radha69", AJ_ContainerLabelPrint.ImpersonationScope.LogonType.LOGON32_LOGON_INTERACTIVE, AJ_ContainerLabelPrint.ImpersonationScope.LogonProvider.LOGON32_PROVIDER_DEFAULT))
            {
                if (m_streams == null || m_streams.Count == 0)
                    return "empty report";
                PrintDocument printDoc = new PrintDocument();
                printDoc.PrinterSettings.PrinterName = printerName;
                printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
                printDoc.Print();
                return "Successful Print";
            }
 
        }
 
        // Create a local report for Report.rdlc, load the data,
        //    export the report to an .emf file, and print it.
        public string Process_Report_Service(string str_rdlc, string ess_userid, string printerName, string container_num, ref ReportDataSource rptDataSource)
        {
            
            LocalReport report = new LocalReport();
            report.ReportPath = str_rdlc;
            report.DataSources.Add(rptDataSource);
            //string printerName = @"\\SACPRINT\51_148_4730";
            System.Random RandNum = new System.Random();
            int MyRandomNumber = RandNum.Next(1986, 5005);
            compound_filename_parts = ConstructFileName(container_num, "CLBL", ess_userid, printerName);
            Export(report);
            m_currentPageIndex = 0;
            Dispose();
            // monitor progress of print files processed in Windows Service
            bool all_processed_ok = false;
            bool print_problems_found = false;
            int accum_process_time = 0;
            string return_message = "Some output may still be printing. No problems yet encountered.";
            string[] stringArray1;
            char[] charArray1;
            charArray1 = new char[] { ' ' };
            stringArray1 = emf_files.Split(charArray1);
 
            while ((!(all_processed_ok)) && (!(print_problems_found)) && (accum_process_time < 61000))
            {
                System.Threading.Thread.Sleep(1000);
                accum_process_time += 1000;
                bool not_processed = false;
                foreach (string emf_file in stringArray1)
                {
                    if (emf_file.Length < 8)
                        continue;
                    string processed_emf_file = emf_file.Replace("emffiles", "emffiles_processed");
                    FileInfo fi = new FileInfo(processed_emf_file);
                    if (!(fi.Exists))
                    {
                        //throw new Exception("new file name is " + new_file_name);
                        not_processed = true;
                    }
                    // check for problems folder entries
                    string problem_emf_file = emf_file.Replace("emffiles", "emffiles_problems");
                    FileInfo fi2 = new FileInfo(problem_emf_file);
                    if (fi2.Exists)
                    {
                        //throw new Exception("new file name is " + new_file_name);
                        print_problems_found = true;
                        return_message = "Print problem encountered for " + problem_emf_file;
                    }
                }
                if ((not_processed == false) && (!(print_problems_found)))
                {
                    all_processed_ok = true;
                    return_message = "Successful Print Completed";
                }
            }
            //return "Print job successful";
            return return_message;
        }
 
        public void Dispose()
        {
            if (m_streams != null)
            {
                foreach (Stream stream in m_streams)
                    stream.Close();
                m_streams = null;
            }
        }
 
    }
}
[+][-]11/05/09 10:36 AM, ID: 25752436

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-91 - Hierarchy / EE_QW_3_20080625