Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In this we add rows and column from .csv file to Data Table and want to apply validation that If any mismatch datatype is found in rows of any columns then messages Displayed which line and column have mismatched datatype..

C#
protected void InsertDataIntoSql(string csvfile)
       {
           string [] tempLines = File.ReadAllLines(Server.MapPath("../Documents/" + csvfile));
           DataTable dt=new DataTable();
           string []  colname = tempLines[0].Split(',');

           foreach (string t in colname)
           {
               dt.Columns.Add(t);
           }
           for (int i = 1; i < tempLines.Length; i++)
           {
               string[] line = tempLines[i].Split(',');
               dt.Rows.Add(line);
           }
Posted
Comments
ZurdoDev 2-Sep-14 9:49am    
Do you have a question?
Richard MacCutchan 2-Sep-14 10:27am    
You need to add some code which checks all the fields before you add each line, and verifies it against some list of rules.
ChauhanAjay 2-Sep-14 13:22pm    
what kind of validations are you looking at?

1 solution

You need to specify the data type of each column and check data against that.

This is one way of many for how you can do it. I hope it can help you on the way.
C#
dt.Columns.Add(t, typeof(int));
dt.Columns.Add(t, typeof(double)); 
etc.

As you are doing this in a loop you need an array with the matching data types or do it manually inside the loop.

C#
for (int i=0; i<colname.length;>{
    if (i == 0)      // Column 1 is of type int
        dt.Columns.Add(colname[i], typeof(int));
    else if (i == 1)      // Column 2 is of type double
        dt.Columns.Add(colname[i], typeof(double));
    // and so on
}


Not the most elegant way and if the number of columns change or their types, then you have to change the code.
The best option is to get the data type from the document, but without any knowledge of the structure of the CSV file I cannot suggest anything else.

The point is that if you have set the data type of each column you can use it to validate the data.
C#
int line = 0;
int col = 0;
try
{
    for (line = 1; line < tempLines.Length; line++)
    {
       string[] columns = tempLines[line].Split(',')
       for (col=0; col < columns.Length; col++)
       {
           // Exception will occur here if there is a mismatch
           object val = Convert.ChangeType(columns[col], dt.Columns[col].DataType);
           dt.Rows.Add(val);
       }
    }
}
catch (Exception ex);
{
    throw new Exception(String.Format("Error at line {0}, col {1}, {2}", line, col, ex.Message);
}
 
Share this answer
 
Comments
Member 11034643 3-Sep-14 3:02am    
thanx George its really helpfull :)
George Jonsson 3-Sep-14 3:59am    
You are welcome. I'm glad it helped you.
Member 11034643 3-Sep-14 4:10am    
but now problem is that ,this way is static i want dynamic way because i have too much columns
George Jonsson 3-Sep-14 4:18am    
Yes it is because I have no knowledge about your data.
How will you know what data type a specific column should have?
How many columns are we talking about?

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