Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to import data from an excel file into a temp table in sql and then display it in a gridview in asp.net.

What I have tried:

C#
SqlDataAdapter da = new SqlDataAdapter();

da.InsertCommand = new SqlCommand("SELECT * into #InsertQueues FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;HDR=NO;IMEX=1;Database= " + filePath + "'" + @"SELECT * FROM[" + getExcelSheetName + @"')");


da.SelectCommand = new SqlCommand("Select * from #InsertQueues");

vid.Open();

da.InsertCommand.ExecuteNonQuery();

da.SelectCommand.ExecuteNonQuery();

DataSet ds = new DataSet();

da.Fill(ds);

GridView2.DataSource = ds;

GridView2.DataBind();

vid.Close();

Error: ExecuteNonQuery: Connection property has not been initialized.
Posted
v2

1 solution

ExecuteNonQuery: Connection property has not been initialized.

C#
da.SelectCommand = new SqlCommand("Select * from #InsertQueues", vid);

And no need to open and close the connection. SqlDataAdapter does this internally. Also, ExecuteNonQuery is not required here, everything is handled by Fill method.
 
Share this answer
 

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