I have two different Excel files (.xls). There is a column named KATIP in excel1.xls, and there is a column named SAVCI in excel2.xls. I want to get these columns and merge them into a table named Nobet with 2 columns: SAVCI and KATIP.
Then I want to show them in a Gridview Control in ASP.NET. I have working code, but when I run it, it's getting the first column named KATIP then getting the column named SAVCI. They are not merged, and they appear as different columns.
OleDbConnection DBConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +
Server.MapPath("~/App_Data/excel1.xls") + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes\"");
DBConnection.Open();
string SQLString = "SELECT * FROM [Page1$]";
OleDbCommand DBCommand = new OleDbCommand(SQLString, DBConnection);
OleDbDataAdapter da = new OleDbDataAdapter(DBCommand);
DataSet ds = new DataSet("Nobet");
da.Fill(ds,"Nobet");
DBConnection.Close();
DBConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +Server.MapPath("~/App_Data/excel2.xls") + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes\"");
DBConnection.Open();
DBCommand = new OleDbCommand(SQLString, DBConnection);
da = new OleDbDataAdapter(DBCommand);
da.Fill(ds,"Nobet");
GridView1.DataSource = ds.Tables["Nobet"];
GridView1.DataBind();
DBConnection.Close();
I solved my problem... here is what I did:
OleDbConnection DBConnection = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +
Server.MapPath("~/App_Data/savcilik_katip.xls") + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes\"");
DBConnection.Open();
OleDbCommand DBCommand = new OleDbCommand("SELECT * FROM [Sayfa1$]", DBConnection);
OleDbDataAdapter da = new OleDbDataAdapter(DBCommand);
DataSet ds = new DataSet("Nobet");
da.Fill(ds, "Nobet");
DBConnection.Close();
DBConnection = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +
Server.MapPath("~/App_Data/savcilik_savci.xls") + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes\"");
DBConnection.Open();
DBCommand = new OleDbCommand("SELECT * FROM [Sayfa1$]", DBConnection);
da = new OleDbDataAdapter(DBCommand);
da.Fill(ds, "Nobetci");
DBConnection.Close();
for (int i = 0; i < ds.Tables["Nobet"].Rows.Count; i++)
{
ds.Tables["Nobet"].Rows[i]["SAVCI"] = ds.Tables["Nobetci"].Rows[i]["SAVCI"];
}
GridView1.DataSource = ds.Tables["Nobet"];
GridView1.DataBind();
Related
I have a gridview and it is taking the data from an excel sheet. In visual studio, from the properties menu of the gridview I have just set paging and sorting enabled. But this is not working. Is it about the excel source or should I add some extra code ?
string whereClause = SearchTextBox.Text;
String name = "Sheet0";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
"C:\\Users\\BerkayS\\Desktop\\VSCLOGN.xls" +
";Extended Properties='Excel 12.0 XML;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select " + selectedCriteria + " From [" + name + "$] where USER_ID = '" + whereClause + "'", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
GridView2.DataSource = data;
GridView2.DataBind();
GridView2.Visible = true;
Note: Ignore some variable names in the code, they are assigned before
I need to insert more than 1000 records into SQL Server. But using my code I am able to insert only 1000 records. Please help me.
using (SqlBulkCopy s = new SqlBulkCopy(dbConnection,SqlBulkCopyOptions.UseInternalTransaction, null))
{
s.DestinationTableName = TableName;
s.BatchSize = 10000;
s.BulkCopyTimeout = 1800;
foreach (var column in dt.Columns)
{
s.ColumnMappings.Add(column.ToString(), column.ToString());
}
s.WriteToServer(dt);
}
Below is the real time working code which i used in my project to insert the bulk data from excel to the SQL server
C# code:
public static DataSet Bindgrid_StoreInSQL(string path)
{
string strFileType = Path.GetExtension(path).ToLower();
string connString = "";
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "SELECT * FROM [Sheet1$]";
OleDbConnection conn = new OleDbConnection(connString);
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable Exceldt = ds.Tables[0];
//creating object of SqlBulkCopy
SqlBulkCopy objbulk = new SqlBulkCopy(OneStopMethods_Common.constring_Property);
//assigning Destination table name
objbulk.DestinationTableName = "Tern_boq";
objbulk.ColumnMappings.Add("ID", "ID");
objbulk.ColumnMappings.Add("Bill_No", "Bill_No");
objbulk.ColumnMappings.Add("Page_No", "Page_No");
objbulk.ColumnMappings.Add("ItemNo", "ItemNo");
objbulk.ColumnMappings.Add("Description", "Description");
objbulk.ColumnMappings.Add("BOQ_Qty", "BOQ_Qty");
objbulk.ColumnMappings.Add("UNIT", "UNIT");
objbulk.ColumnMappings.Add("Category1", "Category1");
objbulk.ColumnMappings.Add("Category2", "Category2");
objbulk.ColumnMappings.Add("Category3", "Category3");
objbulk.ColumnMappings.Add("Estimated_UnitRate", "Estimated_UnitRate");
objbulk.ColumnMappings.Add("Estimated_Amount", "Estimated_Amount");
//inserting Datatable Records to DataBase
conn.Open();
objbulk.WriteToServer(Exceldt);
SqlDatabase obj = new SqlDatabase(OneStopMethods_Common.constring_Property);
string selquery = " select * from Tern_boq";
return obj.ExecuteDataSet(CommandType.Text, selquery);
}
Its works fine,Hope this can give you some idea,Please let me know your your thoughts or suggestions
Hi I have following code that uploads excel file and displays the rows.
if (fileExtension == ".xls")
{
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
// lblMessage.Text = "Only files with .xlsx or .xls extensions are allowed";
// lblMessage.ForeColor = System.Drawing.Color.Red;
}
else if (fileExtension == ".xlsx")
{
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
Datatable dtExcelRecords = new Datatable();
con.Open();
Datatable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
cmd.CommandText = "SELECT * FROM[" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dtExcelRecords);
con.Close();
GridView1.DataSource = dtExcelRecords;
GridView1.DataBind();
On following line
Datatable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
I get compilation error
Error 36 Cannot implicitly convert type 'System.Data.DataTable' to 'Datatable'
Please let me know how to fix it.
Thanks
if (filenam.ToString() == ".xls")
{ constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathnam + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; }
else if (filenam.ToString() == ".xlsx")
{ constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathnam + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; }
else { Response.Write("<script>alert('Load Excel file Only')</script>"); }
string Qry = "SELECT [Customer], [Project], [Contact], [Designation], [Phone], [EmailID],[Region] FROM [Sheet1$]";
OleDbConnection conn = new OleDbConnection(constr);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
OleDbCommand cmd = new OleDbCommand(Qry, conn);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
if (dt != null && dt.Rows.Count > 0)
{
gv_upload.DataSource = dt;
gv_upload.DataBind();
}
da.Dispose(); conn.Close(); conn.Dispose();
}
It is Working Code try it
C# is case sensitive. Datatable is not the same as DataTable.
The version with the uppercase is the right one.
You need to fix all of your DataTable declarations
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
Hi there I am trying to establish a connection to a data source and extract the information and display it in a grid view. The problem is that i always get null value for ada. Is it possible to have mistyped the query or is there something wrong with the adapter?
Furthermore I am using the myInt variable to insert different data sources because i hav to process more than one file, maybe this could be problematic as well.
try
{
//establish connectioin
OleDbConnection conn = new OleDbConnection(("provider=Microsoft.Jet.OLEDB.4.0; " + ("data source=" + myInt + ";" + "Extended Properties=Excel 8.0;")));
OleDbDataAdapter ada = new OleDbDataAdapter("SELECT * FROM MarkingSheet$]", conn);
DataSet ds = new DataSet();
ada.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
conn.Close();
}
ANSWER
Thats what worked for me
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + myPath + ";Excel 12.0;HDR=YES;"); ;
conn.Open();
OleDbDataAdapter ada = new OleDbDataAdapter("select * from [Marking Sheet$]", conn); ;
DataSet ds = new DataSet();
ada.Fill(ds);
Change the sql to
"SELECT * FROM [MarkingSheet$]"
since there's a missing opening bracket.
The Fault in your code is that you havent opened a connection when you attempt to fill the Adapter. Your SQL Statement is also wrong. You may also wan wish to bind the DataTable to the DataGridView too like this :-
try
{
OleDbConnection conn = new OleDbConnection(("provider=Microsoft.Jet.OLEDB.4.0; " + ("data source=" + myInt + ";" + "Extended Properties=Excel 8.0;")));
OleDbDataAdapter ada = new OleDbDataAdapter("SELECT * FROM [MarkingSheet$]", conn);
DataSet ds = new DataSet();
conn.Open();
ada.Fill(ds.Tables[0]);
conn.Close();
BindingSource bs = new BindingSource();
bs.Datasource = ds.Tables[0];
dataGridView1.DataSource = bs;
}
catch(OledbException x)
{
// Handle Exception
}
EDIT **
Try Changing your connection string to :-
string connString = "provider=Microsoft.Jet.OLEDB.4.0;Data source=" + myInt + ";Extended Properties=Excel 8.0;HDR=Yes;IMEX=1\";";
I am trying to bind excel file to dataGridView
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=E://Org.xls;"
+ "Extended Properties=" + (char)34
+ "Excel 8.0;HDR=Yes;" + (char)34;
OleDbConnection conn = new OleDbConnection(strConn);
textBox1.Text = "test";
OleDbCommand command = new OleDbCommand("Select * from [Sheet1$]", conn);
conn.Open();
dataGridView1.DataSource = command.ExecuteReader();
conn.Close();
But grid view doesn't show anything. It doesn't give error either
Heres how to do it, just need to change the path for the excel file and the reference to the grid.
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c://Org.xls;Extended Properties=" + (char)34 + "Excel 8.0;HDR=Yes;" + (char)34);
DataSet myExcelData=new DataSet();
conn.Open();
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [Sheet1$]", conn);
myDataAdapter.Fill(myExcelData);
ultraGrid1.DataSource = myExcelData;
conn.Close();
Change that code
ultraGrid1.DataSource = myExcelData;
to this
dataGridView1.DataSource = myExcelData.Tables[0];