I have to create charts with dynamic datasource, I have a code. It does not show error but the graph is also not visible on runtime.
Here out_table is the name of my table and ADX is one of its column.
code:
OleDbConnection con1 = new OleDbConnection(#"PROVIDER=Microsoft.ACE.OLEDB.12.0;DATA SOURCE=RS.accdb");
String sqlo = "Select ADX from " + out_table + "";
OleDbCommand myCommand = new OleDbCommand(sqlo, con1);
myCommand.Connection.Open();
OleDbDataReader myreader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
chart1.DataBindTable(myreader, "ADX");
thanks for helping me. I have solved this problem, and for others, here is the solution.
here, ds is dataset
OleDbConnection con1 = new OleDbConnection(#"PROVIDER=Microsoft.ACE.OLEDB.12.0;DATA SOURCE=RS.accdb");
String sqlo = "Select * from " + out_table + "";
OleDbDataAdapter da1 = new OleDbDataAdapter(sqlo, con);
DataSet ds = new DataSet();
da1.Fill(ds, in_table);
DataView firstView = new DataView(ds.Tables[0]);
chart1.Series[0].Points.DataBindXY(firstView, "ID", firstView, "ADX");
Related
I'm trying this code but it shows an error on da.Fill(dt)
No value given for one or more required parameters.
Why does it show that error? I clearly check all names of databases and tables and fields, they all are correct and I'm using date/time field for datetime.
Can you help me with this?
string conn = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ahmed\OneDrive\Documents\shop.accdb";
OleDbConnection ccc = new OleDbConnection(conn);
ccc.Open();
string css = "SELECT * from tbl3 Where dateitem between '" + dateTimePicker1.Value.ToString() + "%' AND '" + dateTimePicker2.Value.ToString()+"%'";
OleDbCommand non = new OleDbCommand(css, ccc);
OleDbDataAdapter da = new OleDbDataAdapter(non);
DataTable dt = new DataTable();
da.Fill(dt);
count = Convert.ToInt32(dt.Rows.Count.ToString());
dataGridView1.DataSource = new BindingSource(dt, null);
As others have mentioned you should use the parameters instead of hard coding the values.
using (OleDbConnection conn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ahmed\OneDrive\Documents\shop.accdb"))
{
conn.Open();
// DbCommand also implements IDisposable
using (OleDbCommand cmd = conn.CreateCommand())
{
var param1 = new OleDbParameter("#DateTimePicker1", OleDbType.DBDate); //you may have to play with different types
param1.Value = dateTimePicker1.Value;
cmd.Parameters.Add(param1);
var param2 = new OleDbParameter("#DateTimePicker2", OleDbType.DBDate);
param2.Value = dateTimePicker2.Value;
cmd.Parameters.Add(param2);
cmd.CommandText = "SELECT * from tbl3 Where datetime >= #DateTimePicker1 and datetime <= #DateTimePicker2";
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
count = Convert.ToInt32(dt.Rows.Count.ToString());
dataGridView1.DataSource = new BindingSource(dt, null);
}
}
I'm trying to get an list of items from some table of SQL Server. I'm doing it with Visual Studio 2010.
That's my code:
static string filter = "25";
DataSet Datos = new DataSet();
SqlConnection MyConnection = default(SqlConnection);
SqlDataAdapter MyDataAdapter = default(SqlDataAdapter);
MyConnection = new SqlConnection("Initial Catalog=MyDataBase;Data Source=MyServer;Integrated Security=false;User ID=SQLUser;Password=SQLPass;");
MyDataAdapter = new SqlDataAdapter("SELECT Comment FROM MyTable WHERE [No_] = "+filter+" , MyConnection);
MyDataAdapter.SelectCommand.CommandType = CommandType.Text;
MyDataAdapter.Fill(Datos);
MyDataAdapter.Dispose();
MyConnection.Close();
I'm getting this error:
Argument 1: cannot convert from 'string' to 'System.Data.SqlClient.SqlCommand'
On this line:
MyDataAdapter = new SqlDataAdapter("SELECT Comment FROM MyTable WHERE [No_] = "+filter+" , MyConnection);
So, how can I put the string into my SqlConnection?
Thanks in advance.
This works for me:
string filter = "10";
DataSet Datos = new DataSet();
string connectionString = "<your connection string here>";
string selectCommand =
"SELECT * FROM myTable WHERE Id = " + filter;
using (var MyConnection = new SqlConnection(connectionString))
using (var MyDataAdapter = new SqlDataAdapter(selectCommand, MyConnection))
{
MyDataAdapter.SelectCommand.CommandType = CommandType.Text;
MyDataAdapter.Fill(Datos);
}
This is basically your code, with the using syntax. Give this a try and post if you still have problems. Good luck!
Can anyone tell me why the below code would not commit the data to the access database table?
If I view the dtAccess datatable it shows correctly what I want to be written to Access but when I go Access and view the table, nothing has been written.
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"C:\\Database.accdb\";Persist Security Info=False;");
string queryString = "SELECT * from " + lblTable.Text;
//OleDbDataAdapter adapter = new OleDbDataAdapter(queryString, myConnection);
OleDbDataAdapter adapter = new OleDbDataAdapter();
DataTable dtAccess = new DataTable();
DataTable dtCSV = new DataTable();
dtCSV = ds.Tables[0];
// OleDbCommand cmd = new OleDbCommand("INSERT INTO " + lblTable.Text + "(ASIN) VALUES (1234)", myConnection); //this command works when used with cmd.executenonquery();
adapter.SelectCommand = new OleDbCommand("SELECT * from " + lblTable.Text, myConnection);
adapter.InsertCommand = new OleDbCommand("INSERT INTO " + lblTable.Text + " ([ASIN], [MAP Retail], [Style Number], [MSRP Retail]) VALUES (?,?,?,?)", myConnection);
myConnection.Open();
adapter.Fill(dtAccess);
dtAccess.Merge(dtCSV);
adapter.Update(dtAccess);
myConnection.Close();
I want to read Excel file in c# using following code
string excelFileName = "Book2.xls";
string excelConnectString = #"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Book2.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
//string excelConnectString = #"Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + excelFileName + ";" + "Extended Properties = Excel 8.0; HDR=Yes;IMEX=1";
OleDbConnection objConn = new OleDbConnection(excelConnectString);
OleDbCommand objCmd = new OleDbCommand("Select * From [Sheet1$]", objConn);
OleDbDataAdapter objDatAdap = new OleDbDataAdapter();
objDatAdap.SelectCommand = objCmd;
DataSet ds = new DataSet();
objDatAdap.Fill(ds);
Everything is working fine.Now my requirement is to read the excel file something like below
SELECT A,B,D From [Sheet1];
The Select-command should look like this if you want to read A1 to D1:
SELECT * FROM [SHEETNAME_HERE$A1:D1]
Whole Code:
OleDbConnection con = new OleDbConnection(
"provider=Microsoft.Jet.OLEDB.4.0;data source="
+ XLS_FILE_NAME_AND_PATH_HERE
+ ";Extended Properties=Excel 8.0;");
StringBuilder stbQuery = new StringBuilder();
stbQuery.Append("SELECT * FROM [" + SHEETNAME_HERE + "$A1:D1]");
OleDbDataAdapter adp = new OleDbDataAdapter(stbQuery.ToString(), con);
DataSet dsXLS = new DataSet();
adp.Fill(dsXLS);
DataView dvEmp = new DataView(dsXLS.Tables[0]);
dataGridView1.DataSource = dvEmp;
DataTable Contents = new DataTable();
using (OleDbDataAdapter adapter = new OleDbDataAdapter("Select * From [Sheet1$]", objConn))
{
adapter.Fill(Contents);
}
Console.WriteLine(Contents.Rows[0][0]);
You can select a particular cell by passing the proper index.
You can just constuct use query like that:
SELECT FirstName, LastName, Mobile FROM [Sheet1$]
i.e. use first row values as column names.
I am trying to fill the oracle dataset == NULL;
I am using it with a .NET Framework 2.0 with C#
here is a system.data.oracleclient example
http://msdn.microsoft.com/en-us/library/system.data.oracleclient.oracledataadapter%28v=vs.71%29.aspx (this example is 1.1 but will work the same with 2.0)
(snippet from link)
OracleConnection conn = new OracleConnection("Data Source=Oracle8i;Integrated Security=yes");
Conn.Open;
OracleCommand cmd = conn.CreateCommand();
cmd.CommandText = "sp_pkg.getdata";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter("a1", OracleType.Cursor)).Direction = ParameterDirection.Output;
cmd.Parameters.Add(new OracleParameter("a2", OracleType.Cursor)).Direction = ParameterDirection.Output;
DataSet ds = new DataSet();
OracleDataAdapter adapter = new OracleDataAdapter(cmd);
adapter.Fill(ds);
here is an ODP (recommended) example:
http://www.oracle.com/technology/sample_code/tech/windows/odpnet/DSPopulate/ViewProducts.cs.html
(snippet from link)
//Instantiate OracleDataAdapter to create DataSet
productsAdapter = new OracleDataAdapter();
//Fetch Product Details
productsAdapter.SelectCommand = new OracleCommand("SELECT " +
"Product_ID , " +
"Product_Name , " +
"Product_Desc , " +
"Category, " +
"Price " +
"FROM Products",conn);
//Instantiate DataSet object
productsDataSet = new DataSet("productsDataSet");
//Fill the DataSet with data from 'Products' database table
productsAdapter.Fill(productsDataSet, "Products");
//setting 'productsDataSet' as the datasouce and 'Products' table
//as the table to which the 'productsDataGrid' is Bound.
productsDataGrid.SetDataBinding(productsDataSet,"Products");