Edison
Edison

Reputation: 79

C# read excel cannot get the value

I can read all of the value except 'one'.

This is my code:

OleDbConnection oledb_con = new OleDbConnection(strCon);

oledb_con.Open();

OleDbCommand oledb_com = new OleDbCommand("SELECT * FROM [sheet1$]", oledb_con);

OleDbDataReader oledb_dr = oledb_com.ExecuteReader();

while (oledb_dr.Read())
{                
       ActionList.Add(oledb_dr[0].ToString().Trim());
       ValueList.Add(oledb_dr[1].ToString().Trim());
}

oledb_dr.Close();
oledb_con.Close();

the ValueList[0] always show nothing,but other ValueList's member can read.

And theActionList[0] can read.

why cannot the first value be read.

And how can I solve it?

Upvotes: 0

Views: 981

Answers (1)

mishak
mishak

Reputation: 43

Have you checked if the value is not null? On null reading value as string fails.

oledb_dr.IsDbNull(1) ? "" : oledb_dr[1].ToString().Trim()

Upvotes: 1

Related Questions