replacing data in a dataset - c#

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

Related

Can't save dataset from excel to access database c#

I try to read excel file without open and fill it to dataset (for futher using), then I will save dataset to access database.
Here is my code
DataSet ds = new DataSet();
//Read data from excel file and store to dataset
var ExcelfileName = #"C:\Users\gsa81hc\Desktop\COEMInternalErrataTracking_withCQnumber.xlsx";
var ExcelcnnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelfileName + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
var Excelcnn = new OleDbConnection(ExcelcnnString);
Excelcnn.Open();
var cmd = Excelcnn.CreateCommand();
cmd.CommandText = "SELECT * FROM [GEN4 Proejects$] ";
var adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds);
//Save dataset to access database
string AccesscnnString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Tool_Development\CSharp\Dot2Wpf\Project_Database.accdb";
var Accesscnn = new OleDbConnection(AccesscnnString);
Accesscnn.Open();
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = new OleDbCommand("SELECT * FROM Table", Accesscnn);
var objCommandBuilder = new OleDbCommandBuilder(da);
objCommandBuilder.QuotePrefix = "[";
objCommandBuilder.QuoteSuffix = "]";
da.Update(ds, "Table");
When I debug, ds has data but I don't know why when da.update can't save ds to access file

Error "Not a legal OleAut date" while trying to import from excel

I'm trying to import data from excel 2007 file in a c# app and this is the code:
try
{
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=old.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
string qry = "select * from [margin$]";
OleDbCommand excommand = new OleDbCommand(qry, con);
OleDbDataAdapter da = new OleDbDataAdapter(excommand);
DataSet ds = new DataSet();
da.Fill(ds, "dsmargin");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "dsmargin";
ds.Dispose();
da.Dispose();
excommand.Dispose();
con.Close();
con.Dispose();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
The sheet I am trying to import doesn't has any dates at all. BUT other sheets will and I will need to import the dates as values not as text.
The only solution I have is when I changed the "HDR=YES;" to "HDR=NO;"
But what if I need the "HDR" to be a "YES"?
Sample of header and first raw:

How to populate a combobox from a Access dataset / Database

ive hit a brick wall on what to do next to populate a combobox from a data set currently i have. my database is call "PID2db.mdb" and the table is called "Customer"
string strCon = Properties.Settings.Default.PID2dbConnectionString;
OleDbConnection conn = new OleDbConnection(strCon);
try {
conn.Open();
string strSql = "Select forename,surname from customer where [customerID] ='" + txtName.Text + "'";
OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(strSql, conn));
DataSet ds = new DataSet();
adapter.Fill(ds);
cboName.DataSource = ds.Tables[0];
cboName.DisplayMember = "forename";
cboName.ValueMember = "surname";
}
finally {
conn.Close();
}
}
any help is appreciated, thanks
edit: added new code segments
Assuming you have a Form with:
Combobox named cboName
TextBox named txtName
Try this :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
LoadCustomerOnCombo();
}
private void LoadCustomerOnCombo()
{
string strCon = Settings.Default.PID2dbConnectionString;
try
{
using (OleDbConnection conn = new OleDbConnection(strCon))
{
conn.Open();
string strSql = "SELECT forename &\" \" & surname AS FullName, surname FROM customer"; //WHERE [customerID] ='" + txtName.Text + "'";
OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(strSql, conn));
DataSet ds = new DataSet();
adapter.Fill(ds);
cboName.DataSource = ds.Tables[0];
cboName.DisplayMember = "FullName";
cboName.ValueMember = "surname";
}
}
catch (Exception ex)
{
txtName.Text = ex.Message;
Console.WriteLine(ex.Message);
}
}
}
If worked so please tell me how is the where, left commented at the moment, else there are any errors, you'll see inside the textbox.
You need to create a new DataSet, fill it with Data and finally set the ComboBox's DataSource, DisplayMember and ValueMember properties. Here is the code:
using System.Data.OleDb;
string strCon = Properties.Settings.Default.PID2dbConnectionString;
OleDbConnection conn = new OleDbConnection(strCon);
try {
conn.Open();
string strSql = "Select forename,surname from customer where customer id ='" + txtName.Text + "'";
OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(strSql, conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
comboBox1.DataSource = ds.Tables[0];
comboBox1.DisplayMember = "forename";
comboBox1.ValueMember = "surname";
}
finally {
conn.Close();
}

Unable to read Excel worksheet with OLEDB driver

I have an excel file with one worksheet. I'm using MicroSoft.Office.Interop.Excel to read this file and then perform further execution.
Here is my code:
connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strNewPath + ";Extended Properties=Excel 8.0;";
conn = new OleDbConnection(connString);
if (conn.State == ConnectionState.Closed)
conn.Open();
System.Data.DataTable dt = null;
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
But, worksheet is not in the data table object.
Where you have mentioned the table(WorkSheet) name ??
DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new object[] {null, null, null, "TABLE"});
//Get the First Sheet Name
string firstSheetName = schemaTable.Rows[0][2].ToString();
//Query String
string sql = string.Format("SELECT * FROM [{0}],firstSheetName);
Refer here MSDN
In case if you want to play around refer Reading Excel files from C#
OleDbConnection oconn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + "; Extended Properties=Excel 12.0;");
//After connecting to the Excel sheet here we are selecting the data
//using select statement from the Excel sheet
oconn.Open();
DataTable dbSchema = oconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dbSchema == null || dbSchema.Rows.Count < 1)
{
throw new Exception("Error: Could not determine the name of the first worksheet.");
}
string firstSheetName = dbSchema.Rows[0]["TABLE_NAME"].ToString();
string tbstrat = "M1000";
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = oconn;
cmd.CommandText = "select * from [" + firstSheetName + "B8:" + tbstrat + "]";
OleDbDataAdapter adap = new OleDbDataAdapter();
DataTable dt = new DataTable();
adap.SelectCommand = cmd;
adap.Fill(dt);
oconn.Close();
Even you can try this
var adapter = new OleDbDataAdapter("SELECT * FROM [workSheetNameHere$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "NameHere");
DataTable data = ds.Tables["NameHere"];

Im having difficulty setting my OleDb connection

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

Categories