I have a DataTable object which is read from Excel speradsheet. Since the spread sheet has blank columns. I want to get rid of the blank columns in my DataTable retaining only the columns which have header data. Any better approaches rather than reading though each column?
I use C# 3.5.
You can better use "OleDbConnection" rather than looping through each columns to fetch data.
It will avoid the blank columns
OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0");
OleDbDataAdapter da = new OleDbDataAdapter("select * from YourTable", con);
DataTable dt = new DataTable();
da.Fill(dt);
Related
I'm displaying information from a dataset in a datagridview. How do I update the dataset with the data in the datagridview after it's modified? (automaticly or through button) Using winforms,in visual studio 2010, c#
The first, you should create a new datatable to contains data from Datagridview
After that, in the buttun are:(i'm connecting with database is accsess(sql is the same))
DataTable dt = new DataTable();
dt = (DataTable)dataGridView1.DataSource;
OleDbDataAdapter adp = new OleDbDataAdapter("Your query", con);
OleDbCommandBuilder cmdb = new OleDbCommandBuilder(adp);
adp.Update(dt);
// con: the variable name to connecting with database
if I use:
NpgsqlCommand cmd = new NpgsqlCommand("select * from table", conn)
DataTable dt = new DataTable();
using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd))
{
da.Fill(dt);
}
To get data from the server to a DataTable,
Is there a way to similarly put it back after I'm done with the data?
Like emptying the table and inserting the whole DataTable into it.
You could execute a command to drop the table (or just empty all rows if the table structure won't be modified) and iterate through the rows in your DataTable to insert the modified rows from the DataTable into the new table in your Database.
I'm having a Data Table who gets values from some excel file. I use button as File dialog, find file somewhere in file system and then I parse needed values there.
Later in application I'm gonna need just one column from that Data Table. It is column named max t on picture attached.
Below is the code I'm using to get values from file:
string pathConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
OleDbConnection connection = new OleDbConnection(pathConnection);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [DAYTIME CONFORT INDEX$]", connection);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
myDataGridView.DataSource = dt;
As you can see on picture I have few columns but only need some of them. For example I need to get values from column max t into some list of doubles.
I've tried few things but non of them didn't worked. Since I'm a beginner with this can someone help me with easiest way to do this.
Thank you. :)
List columns you need in query. E.g. getting only columns M and max t:
"Select [M],[max t] from [DAYTIME CONFORT INDEX$]"
This will give you an array values from the max t Column from your dataTable
DataView view = new DataView(dt);
DataTable distinctValues = view.ToTable(true, "max t");
DataRow[] myRows = distinctValues.Select();
private void btnSearchDB_Click(object sender, EventArgs e)
{
OleDbConnection accessConnect = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\ECM\ECM\ECM\ECM.mdb");
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM ECMeasurements WHERE [Job Number] LIKE " + txtJobNumber.Text, accessConnect);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
There are also choices that can be searched by Date.....(txtDate.Text) and a comboBox (cbAlloyyTemper.Text). Do I write three different queries or can all the search criteria be together?
First: never use strings in the queries, but use Parameters.
Second: I would create 3 different queries, for easy debugging after. But all depends how much queries you can have, and your own comfort feeling.
This is not the way I would do this at all.
If you are looking at the same table each time you generate each query then you don't want to be querying the database each time. Instead you should load the data into a Data Table, and then query the Data Table in memory rather than the Database each time.
You need to create a Binding Source, and then set the data source for this object to your Data Table object.
For you gridView, you set the datasource for this object to your Binding Source.
Like so :-
OleDbConnection accessConnect = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\ECM\ECM\ECM\ECM.mdb");
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM ECMeasurements", accessConnect);
da.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
Now, any change in the state of the datatable reflects through to your gridview automatically.
To Query the DataTable you can use a DataView. DataView's allow you to search the Datatable with parameters like an SQL Statment. The syntax is off the top of my head, but its the method that you need to look at.
Look at Querying DataTables, using DataViews. There are many examples on the Internet.
In your scenario, its best suited.
Good Luck.
I am trying to get data from Excel File to DataTable.
Here's my code-snippet :
FilePath = WebConfig.SavePath + "Book2.xls";
// Create the connection object
OleDbConnection oledbConn = new OleDbConnection(WebConfig.ExcelConnection(FilePath));
// Open connection
oledbConn.Open();
// Create OleDbCommand object and select data from worksheet Sheet1 //WebConfig.SheetNameFirstExcel
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + "Sheet1" + "$]", oledbConn);
// Create new OleDbDataAdapter
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
// Create a DataSet which will hold the data extracted from the worksheet.
DataTable dt = new DataTable();
// Fill the DataSet from the data extracted from the worksheet.
oleda.Fill(dt);
Problem with this is that, data of some cells is exported to data-table while some other is NOT.
format of excel is something like :
1st Row Heading
2nd Some text
3rd Row blank
4th onwards a table
of 10 columns & 298 rows.
What is missing in above code, or any suggestion for extracting such excel(.xlsx) to datatable in asp.net 3.5
Given all the problems you have I suspect the standard oledb driver just can't read your excel file correctly due to the rows of text prior to the table data.
How about move away and just code it manually using this library http://epplus.codeplex.com/ for reading the xlsx file and create your datatable or db records