AShalata AShalata - 11 months ago 42
C# Question

how to select which row to start with it in import Excel to database

when i import data from Excel to database the code start with the first row it should start with the second row

MaamoonKhalidIssueEntities db = new MaamoonKhalidIssueEntities();
DataTable table = result.Tables[1];

foreach (DataRow dr in table.Rows)
{
string input = Convert.ToString(dr[0]);
var reg = new Regex(pattern2, RegexOptions.IgnoreCase);
Match match = reg.Match(input);

string input2 = Convert.ToString(dr[1]);
var reg2 = new Regex(pattern, RegexOptions.IgnoreCase);
Match match2 = reg2.Match(input2);


if (match.Success && match2.Success)
{
Person addtable = new Person()
{
ncode = Convert.ToString(dr[0]),
nname = Convert.ToString(dr[1])
};

if (ValidateNewPerson(addtable, db))
db.People.Add(addtable);
}
}
db.SaveChanges();
excelreader.Close();
stream.Close();


here i start with the first row ,the first row is a header row, so it shouldn't enter into database,how can i skip it?

Answer Source

You can iterate through the table.Rows list and start with index 1 instead of 0:

for (int i = 1; i < table.Rows.Count; i++)
{
    DataRow dr = table.Rows[i];

    string input = Convert.ToString(dr[0]);
    var reg = new Regex(pattern2, RegexOptions.IgnoreCase);
    Match match = reg.Match(input);

    string input2 = Convert.ToString(dr[1]);
    var reg2 = new Regex(pattern, RegexOptions.IgnoreCase);
    Match match2 = reg2.Match(input2);

    if (match.Success && match2.Success)
    {
        Person addtable = new Person()
        {
            ncode = Convert.ToString(dr[0]),
            nname = Convert.ToString(dr[1])
        };

        if (ValidateNewPerson(addtable, db))
            db.People.Add(addtable);
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download