E
engahmedbarbary
Guest
Problem
How to check column name before i get data from it to avoid exception happen if columns name not same in code .
Details
I need to check column name excel before i get data from it
what actually need
if (First column name != رقم الاستمارة && second column name != رقم العدا د && third column name !=قراءة العداد
give message box error column name not correct
I do that because more people write column name in excel sheet wrong and this is give me exception so that i need to avoid it .
my code as below
public DataTable Showdataprint()
{
string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", txtpath.Text);
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
DataTable dt = new DataTable();
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dt.Rows[0]["TABLE_NAME"].ToString();
OleDbCommand com = new OleDbCommand();
com.Connection = con;
//com.CommandText = @"SELECT CStr([رقم الاستمارة]) as [UnitCode],[قراءة العداد]as[CurrentMeterReading] FROM [" + SheetName + "] ";
com.CommandText = @"SELECT [رقم الاستمارة] as [UnitCode],[رقم العدا د]as[CounterNumber],[قراءة العداد]as[CurrentMeterReading] FROM [" + SheetName + "] ";
OleDbDataAdapter oledbda = new OleDbDataAdapter();
oledbda.SelectCommand = com;
DataSet ds = new DataSet();
oledbda.Fill(ds);
dt = ds.Tables[0];
con.Close();
return dt;
}
Continue reading...
How to check column name before i get data from it to avoid exception happen if columns name not same in code .
Details
I need to check column name excel before i get data from it
what actually need
if (First column name != رقم الاستمارة && second column name != رقم العدا د && third column name !=قراءة العداد
give message box error column name not correct
I do that because more people write column name in excel sheet wrong and this is give me exception so that i need to avoid it .
my code as below
public DataTable Showdataprint()
{
string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", txtpath.Text);
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
DataTable dt = new DataTable();
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dt.Rows[0]["TABLE_NAME"].ToString();
OleDbCommand com = new OleDbCommand();
com.Connection = con;
//com.CommandText = @"SELECT CStr([رقم الاستمارة]) as [UnitCode],[قراءة العداد]as[CurrentMeterReading] FROM [" + SheetName + "] ";
com.CommandText = @"SELECT [رقم الاستمارة] as [UnitCode],[رقم العدا د]as[CounterNumber],[قراءة العداد]as[CurrentMeterReading] FROM [" + SheetName + "] ";
OleDbDataAdapter oledbda = new OleDbDataAdapter();
oledbda.SelectCommand = com;
DataSet ds = new DataSet();
oledbda.Fill(ds);
dt = ds.Tables[0];
con.Close();
return dt;
}
Continue reading...