Im having difficulty setting my OleDb connection - c#

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\";";

Related

replacing data in a dataset

I am trying to replace data in a dataset so i can add it back to a .DBF file. currently i can gather the new dataset but i don't know how to find and replace certain data. For instance, I want to replace 'ABC_123_TEST' by 'DEF_456_TEST' in the first column.
try
{
string coonect = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\\Users\\; Extended Properties = dBase IV; User ID =; Password =";
OleDbConnection con = new OleDbConnection();
con.ConnectionString = coonect;
con.Open();
OleDbDataAdapter cmd = new OleDbDataAdapter("SELECT * FROM variable WHERE NAME LIKE #VAL1", con);
cmd.SelectCommand.Parameters.AddWithValue("#VAL1", "%" + textBox5.Text + "%");
DataSet ds = new DataSet();
cmd.Fill(ds);
DataSet newData = ds.Copy(); //What should i do with this dataset
con.Close();
dataGridView1.DataSource = ds.Tables[0];
}
catch (OleDbException exp)
{
MessageBox.Show("Error: " + exp.Message);
}

Read Excel File Data From OleDbConnection

I'm currently facing a problem whereby I don't know how to fix it.
I upload my precompiled project into IIS. Here is my purpose of this page:
User upload a excel file into a folder in the server. ex #"~/PlanQuantityFile/". It did upload successfully.
Problems faced:
It stops when my scripts trying to open the excel file (for extract data purpose) without showing any errors. At line 881 as shown in the image.
Here is few area I had seek for but it still couldn't solve my problem.
Possible Solution:
connection open but never close, so run out of connection. (but I did close it and the scripts stop running before the close statement)
32 bit program calling 64 bit office. (I had limited knowledge on hardware field, don't know what should I do to troubleshoot here)
permission problem. Need to set the permission of ASP.NET account. (I still finding object names for ASP.NET account)
Thanks for anyone who trying to help. Your advice is invaluable.
OleDbConnection oledbConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + SaveLocation + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
OleDbConnection connExcel = oledbConn;
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter oda = new OleDbDataAdapter();
DataTable dt = new DataTable();
cmdExcel.Connection = connExcel;
//Get the name of Sheet
try
{
connExcel.Open();// It stops here without showing errors.
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + ex.Message + "');", true);
}
The following code returns a datatable for selected filelocation source. Remember to rename the sheetname to "Sheet1".
Use Namespace: using System.Data.OleDb;
Function::::
public DataTable GetExcelinDatatable(string filelocation)
{
DataTable dt = new DataTable();
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 8.0;HDR=YES;\"";
OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString);
con.Open();
OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter("select * from [SHEET1$]", con);
da.Fill(dt);
con.Close();
return dt;
}
you can use mentioned function to read the Excel file you have to just pass the path of excel file
private List<DataTable> readExcel(string strXLS)
{
//DataTable dtExcel = getExcelSheetTable();
List<DataTable> SheetsData = new List<DataTable>();
DataTable dtExcel = new DataTable();
DataTable SocialMediaExcel = new DataTable();
string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strXLS + ";Extended Properties=Excel 12.0;";
try
{
OleDbDataAdapter adpt = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connStr);
adpt.Fill(dtExcel);
SheetsData.Add(dtExcel);
}
catch (Exception ex)
{
throw ex;
}
return SheetsData;
}

Syntax error when opening an Excel workbook using C#

When I try to open an Excel workbook I get a syntax error. Here is the code I'm using:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;"
+ "Data Source=" + fileName + ";"
+"Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
OleDbConnection objConn = new OleDbConnection(connectionString);
OleDbCommand objCommand = new OleDbCommand(#"SELECT * FROM Sheet1$", objConn);
OleDbDataAdapter odjAdp = new OleDbDataAdapter();
odjAdp.SelectCommand = objCommand;
DataTable dt1 = new DataTable();
odjAdp.Fill(dt1);
GridView2.DataSource = dt1;
GridView2.DataBind();
Why is this happening?
Because of the dollar symbol that sheet name needs to be escaped, enclose it in square brackets;
#"SELECT * FROM [Sheet1$]"

Binding excel file to datagridview

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];

Joining two Excel Files and Displaying in a GridView

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();

Categories