Click here to Skip to main content
15,892,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to add the content of log files to data table using the following code


C#
string[] fileContents = null;                 
                 List<string> words = new List<string>();
                 List<string> words1 = new List<string>();
                 DataTable workTable = new DataTable();
                
                 try
                 {
                     // Open and read the entire Data Log File contents.
                     fileContents = File.ReadAllLines(file);
                 }
                 catch (Exception ex)
                 {
                     throw new Exception("The Input log file was not found or is not in correct format\nDetails: " + ex.Message);
                 }

                 if (fileContents == null)
                     throw new Exception("The Input log file was not found or is not in correct format");

                 // Process each line of the Data Log File and filter out the CAN Msg Id's corresponding to each CAN Bus.
                 for (int Index = 0; Index < fileContents.Length; Index++)
                 {
                     string CANMsgId = string.Empty;
                     string[] spaceSeperator = new string[] { " " };
                     string[] lineWords = (fileContents[Index].Trim()).Split(spaceSeperator, StringSplitOptions.RemoveEmptyEntries);
                     if (lineWords.Length < (2 + 1))

                         continue;

                     // If a CAN Msg Id is valid, it should end with 'x'. If it doesnot end with 'x',
                     // then skip the entry and go to the next line of log file
                     if (lineWords[2].EndsWith("x"))
                     {
                         int i = 1;
                         //CANMsgId = lineWords[2].TrimEnd('x');
                         //words.Add(lineWords[0]);
                         //words1.Add(lineWords[1]);
                         workTable.Columns.Add(lineWords[2].TrimEnd('x'));
                         //workTable.Columns.Add("CustFName", typeof(String));
                         workTable.Columns.Add(lineWords[0]);

                        i++;
                     }
                     
                 }



I am getting exception at the following line of code

workTable.Columns.Add(lineWords[2].TrimEnd('x'));

Exception:
System.Data.DuplicateNameException

Thanks
John
Posted
Comments
Sunny_Kumar_ 19-Dec-13 4:13am    
error message simply says there's a duplicate colun name found. Post your log file so that we can check.
Member 10408451 19-Dec-13 4:19am    
Hi,
Yes, you are correct

This is the content of log file I am reading

date Mon Aug 19 05:41:35 pm 2013
base hex timestamps absolute
no internal events logged
// version 7.6.0
// PGN=EF00 SA=B DA=FA
157.692673 1 18EFFA0Bx Rx d 8 01 00 13 DC 00 00 00 00 Length = 282000 BitCount = 145
// PGN=EF00 SA=B DA=FA
157.693679 1 18EFFA0Bx Rx d 8 02 00 00 00 00 00 00 00 Length = 286000 BitCount = 147
// PGN=F004 SA=0 DA=FF EEC1_E
157.693954 1 CF00400x Rx d 8 F4 A8 A9 92 23 FF FF A8 Length = 266000 BitCount = 137
// PGN=EF00 SA=B DA=FA
157.694670 1 18EFFA0Bx Rx d 8 02 00 09 FB 00 00 00 00 Length = 280000 BitCount = 144
// PGN=0 SA=B DA=0 TSC1_100_ABM_REX
157.694968 1 C00000Bx Rx d 8 FC FF FF 7D FF FF FF FF Length = 286000 BitCount = 147
// PGN=FE6C SA=EE DA=FF TCO1_TCO
157.695589 2 CFE6CEEx Rx d 8 4B 02 C0 C5 19 2D 30 59 Length = 262000 BitCount = 135
// PGN=EF00 SA=B DA=FA
157.695670 1 18EFFA0Bx Rx d 8 03 00 09 FB 0D 43 00 29 Length = 272000 BitCount = 140
// PGN=F002 SA=3 DA=FF ETC1_HMS
157.695947 1 CF00203x Rx d 8 CD A4 2C FF FD AE 23 03 Length = 268000 BitCount = 138
// PGN=EF00 SA=B DA=FA
157.696673 1 18EFFA0Bx Rx d 8 03 00 00 53 00 00 00 00 Length = 282000 BitCount = 145
// PGN=F004 SA=0 DA=FF EEC1_E
157.697610 2 CF00400x Rx d 8 F4 A8 A9 92 23 FF FF A8 Length = 266000 BitCount = 137
// PGN=F004 SA=0 DA=FF EEC1_E
157.697611 3 CF00400x Rx d 8 F4 A8 A9 92 23 FF FF A8 Length = 266000 BitCount = 137
// PGN=F004 SA=0 DA=FF EEC1_E
157.697650 4 CF00400x Rx d 8 F4 A8 A9 92 23 FF FF A8 Length = 266000 BitCount = 137
// PGN=F01D SA=27 DA=FF SAS_K
157.697745 1 18F01D27x Rx d 8 FF FE FE FF FF FE FF 71 Length = 274000 BitCount = 141
// PGN=F01D SA=27 DA=FF SAS_K
157.698014 4 18F01D27x Rx d 8 FF FE FE FF FF FE FF 71 Length = 274000 BitCount = 141
// PGN=F01D SA=E4 DA=FF SAS_Prop_SIU
157.698958 5 8F01DE4x Rx d 8 FF FF 3F 00 0D E7 01 F2 Length = 274000 BitCount = 141
// PGN=FFB1 SA=1E DA=FF CUVInformation_V
157.699876 2 18FFB11Ex Rx d 8 00 00 01 30 FD FF D0 00 Length = 278000 BitCount = 143
// PGN=FF80 SA=27 DA=FF DLN1_K
157.700177 1 CFF8027x Rx d 8 0C FA 00 FF F0 FF FF FF Length = 278000 BitCount = 143
// PGN=FE6C SA=EE DA=FF TCO1_TCO
157.700181 3 CFE6CEEx Rx d 8 4B 02 C0 C5 19 2D 67 5A Length = 262000 BitCount = 135
// PGN=FE6C SA=EE DA=FF TCO1_TCO
157.700226 4 CFE6CEEx Rx d 8 4B 02 C0 C5 19 2D 67 5A Length = 262000 BitCount = 135
// PGN=EF00 SA=B DA=FA
157.700719 1 18EFFA0Bx Rx d 8 00 00 74 8B 74 5E 73 0C Length = 268000 BitCount = 138


I need to separate the data and save into data table even though it is duplicate. How can I solve this problem
Member 10408451 19-Dec-13 4:56am    
Hi,
I tried something like this
But still getting null exception

try
{

workTable.Columns.Add(lineWords[2].TrimEnd('x'));
}
catch (System.Data.DuplicateNameException)
{
ColumnName.Append(" ");

}

1 solution

try to store log data in rows, not in columns
C#
string[] fileContents = null;                 
                 List<string> words = new List<string>();
                 List<string> words1 = new List<string>();
                 DataTable workTable = new DataTable();
                
                 try
                 {
                     // Open and read the entire Data Log File contents.
                     fileContents = File.ReadAllLines(file);
                 }
                 catch (Exception ex)
                 {
                     throw new Exception("The Input log file was not found or is not in correct format\nDetails: " + ex.Message);
                 }
 
                 if (fileContents == null)
                     throw new Exception("The Input log file was not found or is not in correct format");
 
                 // Process each line of the Data Log File and filter out the CAN Msg Id's corresponding to each CAN Bus.
workTable.Columns.Add("login");
workTable.Columns.Add("code");
                 for (int Index = 0; Index < fileContents.Length; Index++)
                 {
                     string CANMsgId = string.Empty;
                     string[] spaceSeperator = new string[] { " " };
                     string[] lineWords = (fileContents[Index].Trim()).Split(spaceSeperator, StringSplitOptions.RemoveEmptyEntries);
                     if (lineWords.Length < (2 + 1))
 
                         continue;
 
                     // If a CAN Msg Id is valid, it should end with 'x'. If it doesnot end with 'x',
                     // then skip the entry and go to the next line of log file
                     if (lineWords[2].EndsWith("x"))
                     {
                         int i = 1;
                         //CANMsgId = lineWords[2].TrimEnd('x');
                         //words.Add(lineWords[0]);
                         //words1.Add(lineWords[1]);
workTable.Rows.Add(lineWords[2].TrimEnd('x'),lineWords[0]);
                         //workTable.Columns.Add(lineWords[2].TrimEnd('x'));
                         //workTable.Columns.Add("CustFName", typeof(String));
                         //workTable.Columns.Add(lineWords[0]);
 
                        i++;
                     }
                     
                 }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900