My code is
string PathConn = "Provider= Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox10.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
OleDbConnection conn = new OleDbConnection(PathConn);
OleDbDataAdapter dbDataAdapter = new OleDbDataAdapter("Select * From [" + textBox1.Text + "$]", conn);
DataTable dt = new DataTable();
dbDataAdapter.Fill(dt);
dataGridView1.DataSource = dt;
I get the error:
System.Data.OleDb.OleDbException: ''$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long
What am I doing wrong?
Could it possibly be the "$"
in
("Select * From [" + textBox1.Text + "$]", conn);
Try
("Select * From "+ textBox1.Text , conn);
or
("Select * From ["+ textBox1.Text +"]", conn);
which looks like a more correct sql statement to me.
And if I might make a suggestion, try giving your text boxes names that mean more so that your code is more maintainable in the future.
Related
I'm using c# and this error is becoming headache for me. I do not know how to solve this error .
can anyone help me to solve this. Here is the code
try
{
string MyConnection2 = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\DELL\Documents\db1.mdb";
//Display query
string Query = "select riq_num , department, item_name , item_unit , no_of_stock_out , itemtype from outputdet1 where riq_num = " + textBox2.Text + " or department= '" + comboBox1.Text + " ' or item_name= '" + textBox4.Text + "' or item_unit= '" + comboBox2.Text + "' or no_of_stock_out = " + textBox6.Text + " or itemtype = '" + comboBox3.Text + "' ; ";
OleDbConnection MyConn2 = new OleDbConnection(MyConnection2);
OleDbCommand MyCommand2 = new OleDbCommand(Query, MyConn2);
MyConn2.Open();
//For offline connection we will use MySqlDataAdapter class.
OleDbDataAdapter MyAdapter = new OleDbDataAdapter();
MyAdapter.SelectCommand = MyCommand2;
DataTable dTable = new DataTable();
MyAdapter.Fill(dTable);
// here i have assign dTable object to the dataGridView1 object to display data.
dataGridView1.DataSource = dTable;
MyConn2.Close();
}
// OleDbCommand MyCommand2 = new OleDbCommand(Query, MyConn2);
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
I assumed that textBox2.Text & textBox6.Text return a string from textbox control, so that OleDbCommand will throwing exception when it contains empty value or any non-numeric string since it will form invalid SQL statement. Use parameterized query like this example:
string Query = #"select riq_num, department, item_name, item_unit, no_of_stock_out, itemtype
from outputdet1
where riq_num = #riq_num
or department= #department
or item_name= #item_name
or item_unit= #item_unit
or no_of_stock_out = #no_of_stock_out
or itemtype = #itemtype";
using (OleDbConnection MyConn2 = new OleDbConnection(MyConnection2))
{
using (OleDbCommand MyCommand2 = new OleDbCommand(Query, MyConn2))
{
MyConn2.Open();
MyCommand2.Parameters.Add("#riq_num", textBox2.Text);
MyCommand2.Parameters.Add("#department", comboBox1.Text);
MyCommand2.Parameters.Add("#item_name", textBox4.Text);
MyCommand2.Parameters.Add("#item_unit", comboBox2.Text);
MyCommand2.Parameters.Add("#no_of_stock_out", textBox6.Text);
MyCommand2.Parameters.Add("#itemtype", comboBox3.Text);
// execute the query here
}
}
Remember that using statements used to dispose OLEDB connection immediately after it has closed so that GC can free up resources.
Additional note:
OleDbParameter works with parameter order instead of named parameters, hence ensure that the parameters are declared in their proper order from first to last.
While I am trying to import data from excel, some data of my excel sheet is missing in the DataGridView
string pathcpnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + textBox1.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
OleDbConnection con = new OleDbConnection(pathcpnn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from[" + textBox2.Text + "$]", con);
myDataAdapter.Fill(dt);
DAtagridview1.datasource=dt;
string pathcpnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + textBox1.Text + ";Extended Properties=\""Excel 8.0;HDR=Yes;IMEX=1;";";
OleDbConnection con = new OleDbConnection(pathcpnn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from[" + textBox2.Text + "$]", con);
myDataAdapter.Fill(dt);
DAtagridview1.datasource=dt;
I have been struggling to find an answer to this. I am importing an Excel spreadsheet into C# using Oledb. This works fine, however while importing I wish to join two of the Excel columns together i.e concatenate them.
This is the code I currently have:
string PathConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" + textBox1.Text + ";Extended Properties=\"Excel 12.0 XML;HDR=Yes;\";";
OleDbConnection conn = new OleDbConnection(PathConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select [ID], [Subject], [Catalog], [Last], [First Name], [Descr], [Mark] from[" + textBox2.Text + "$]", conn);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
dataGridView1.DataSource = dt;
The code above is working fine. The two columns I wish to join are Subject and Catalog and for this to be called Module. Subject is a string of three letters and Catalog four numbers. Is there a way to do this within the select statement or an alternative method?
Many thanks in advance.
Change your OleDbAdapter query like this:
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select [ID], [Subject] + ' ' + [Catalog] AS [Module], [Last], [First Name], [Descr], [Mark] from[" + textBox2.Text + "$]", conn);
Notice that [Subject] + ' ' + [Catalog] AS [Module] are combined into a single string and placed under Module alias.
You can "CONCAT" in the query:
https://msdn.microsoft.com/es-es/library/hh231515(v=sql.120).aspx
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select [ID], CONCAT( [Subject], [Catalog] ) AS subject_catalog, [Last], [First Name], [Descr], [Mark] from[" + textBox2.Text + "$]", conn);
I am very new to sqlite and c# and trying exporting a csv file to sqlite database using dataset.
but I get this error.
SQL logic error or missing database
no such column: P17JAW
code:
string strFileName = "I:/exploretest.csv";
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\"");
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + System.IO.Path.GetFileName(strFileName), conn);
DataSet ds = new DataSet("Temp");
adapter.Fill(ds);
DataTable tb = ds.Tables[0];
SQLiteConnection m_dbConnection;
m_dbConnection = new SQLiteConnection("Data Source= C:/Users/WebMobility.db; Version=3;");
m_dbConnection.Open();
var dt = ds.Tables[0];
foreach (DataRow dr in dt.Rows)
{
var Id = dr["Id"].ToString();
var VRM = dr["VehicleRegistration"].ToString();
var Points = Convert.ToInt32(dr["TicketScore"].ToString());
string sql = "insert into NaughtyList (Id,VRM,Points) values ( '" + Id + "'," + VRM + "," + Points + ")";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
m_dbConnection.Close();
}
CSV FILE CONTAINS
Id,VehicleRegistration,TicketScore
21,P17JAW,1
22,K1WOM,1
23,m4npr,4
25,G7EPS,4
Your problem is with the single quotes missing for VRM your query should be like:
string sql = #"insert into NaughtyList (Id,VRM,Points) values
( '" + Id + "','" + VRM + "'," + Points + ")";
//^^ ^^
From the values it appears that ID is of integer type, you can remove single quotes around ID.
You should parameterized your query that will save your from these errors. I am not sure if parameters are supported by SQLiteCommand if they are you can do something like:
string sql = #"insert into NaughtyList (Id,VRM,Points) values
(#Id,#VRM,#Points)";
and then
command.Parameters.AddWithValue("#id", Id);
command.Parameters.AddWithValue("#VRM", VRM);
command.Parameters.AddWithValue("#Points", Points);
I am new to C# and I have a C# program and an excel file exported from another program. How can I convert it to DateTime? Here is the code, where dtFrom and dtTo are DateTimePicker variables:
System.Data.DataTable dtExcel = new System.Data.DataTable();
dtExcel.TableName = "MyExcelData";
string SourceConstr = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + this.textBox1.Text + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
OleDbConnection con = new OleDbConnection(SourceConstr);
string query = "Select Customer, SUM(Wt) AS Weight from [Sheet0$] WHERE (CONVERT(datetime, [Date]) >= " + this.dtFrom.Value.Month + ") GROUP BY Customer order by Customer";
OleDbDataAdapter data = new OleDbDataAdapter(query, con);
data.Fill(dtExcel);
I think it's probably not your only problem, but to answer your question, if you replace
string query = "Select Customer, SUM(Wt) AS Weight from [Sheet0$] WHERE (CONVERT(datetime, [Date]) >= " + this.dtFrom.Value.Month + ") GROUP BY Customer order by Customer";
OleDbDataAdapter data = new OleDbDataAdapter(query, con);
with
string query = "Select Customer, SUM(Wt) AS Weight from [Sheet0$] WHERE (CONVERT(datetime, [Date]) >= #Date) GROUP BY Customer order by Customer";
OleDbCommand command = new OleDbCommand(query);
command.Parameters.Add("#Date", OleDbType.DBTimeStamp).Value = this.dtFrom.Value;
OleDbDataAdapter data = new OleDbDataAdapter(command, con);
you'll probably be on the right track. Not 100% sure exactly which OleDbType you need, but you can see the available options easily enough.