OleDb Error: fill select command.connection property has not been initialized in ASP.Net

Last Reply 2 months ago By dharmendr

Posted 2 months ago

when run shows error as follows

fill selectcommand.connection property has not been initialized

how to fix this error. please let me know.

if (e.CommandName == "UploadFarmData")
{
string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", CurrentFilePath);
Econ = new OleDbConnection(constr);
Econ.Open();
DataTable dtExcelSchema;
dtExcelSchema = Econ.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
DataSet ds = new DataSet();
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]"; 
da.SelectCommand = cmdExcel;
da.Fill(ds);
Econ.Close();
DataTable dt = new DataTable();
dt = ds.Tables[0];
bool s = false;
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
dal.DAL_Insert_Farm_Data(dt.Rows[i]["Farmer Code"].ToString(), dt.Rows[i]["Farm No"].ToString(),
dt.Rows[i]["Type of Farm"].ToString(), dt.Rows[i]["Seeded Date"].ToString(), dt.Rows[i]["Farm Visit No"].ToString());
s = true;
}


if (s)
{
showStatusTrue.InnerHtml = "Farm Details Uploaded Successfully";
dal.DAL_Data_Update();
}
Posted 2 months ago

Hi narasiman,

You need to assign connection to the command object.

OleDbCommand cmdExcel = new OleDbCommand();
// Add this line.
cmdExcel.Connection = Econ;
OleDbDataAdapter da = new OleDbDataAdapter();