How to fill the dataset with C# from oracle database - c#

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

Related

How to search between 2 dates on access database vs?

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

C# MYSQL - Search filtering a datagridview with a combobox and textbox

Hi I'm trying to search filter a datagridview by using a combobox and textbox.
I have successfully done so but it only works properly when I search for the ID column. Other columns just crash display the following message:
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 'Name LIKE 'd%'' at line 1
The d letter in that error message is just the letter I was trying to filter the search with.
Could somebody please help me solve this issue?
My code is below
string myConnection = "datasource=localhost;port=3306;username=root;password=;";
MySqlConnection conDatabase = new MySqlConnection(myConnection);
try
{
if (comboBoxSrchPatient.Text == "ID")
{
MySqlCommand cmd = new MySqlCommand("select * from clinic_inventory_system.patient WHERE ID LIKE '" + txtSearchPatient.Text + "%'", conDatabase);
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
dataPatientGridView.DataSource = dbdataset;
}
else if (comboBoxSrchPatient.Text == "FIRST NAME")
{
MySqlCommand cmd = new MySqlCommand("select * from clinic_inventory_system.patient WHERE First Name LIKE '" + txtSearchPatient.Text + "%'", conDatabase);
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
dataPatientGridView.DataSource = dbdataset;
}
else if (comboBoxSrchPatient.Text == "LAST NAME")
{
MySqlCommand cmd = new MySqlCommand("select * from clinic_inventory_system.patient WHERE Last Name LIKE '" + txtSearchPatient.Text + "%'", conDatabase);
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
dataPatientGridView.DataSource = dbdataset;
}
else if (comboBoxSrchPatient.Text == "AGE")
{
MySqlCommand cmd = new MySqlCommand("select * from clinic_inventory_system.patient WHERE Age LIKE '" + txtSearchPatient.Text + "%'", conDatabase);
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
dataPatientGridView.DataSource = dbdataset;
}
else if (comboBoxSrchPatient.Text == "CONTACT NUMBER")
{
MySqlCommand cmd = new MySqlCommand("select * from clinic_inventory_system.patient WHERE Contact Number LIKE '" + txtSearchPatient.Text + "%'", conDatabase);
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
dataPatientGridView.DataSource = dbdataset;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Your field names contains spaces.
To use them in a query your need to enclose them between backticks (ALT+096)
MySqlCommand cmd = new MySqlCommand(#"select * from
clinic_inventory_system.patient WHERE `Last Name` LIKE ....";
Said that, consider, as soon as possible, to change your queries to use a parameterized query
using(MySqlCommand cmd = new MySqlCommand(#"select * from
clinic_inventory_system.patient
WHERE `First Name` LIKE #name", conDatabase);
{
cmd.Parameters.Add("#name", MySqlDbType.VarChar).Value = txtSearchPatient.Text + "%";
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
dataPatientGridView.DataSource = dbdataset;
}
In this way your code is safer because it is no more possible to build an Sql Injection attack against your db and, if the First Name contains a single quote, you don't have a syntax error again
First of all, with First Name, Last Name and Contact Number, you need to escape the columns correctly.
Since you're using MariaDB, you should use backticks (`) to escape the column names.
Secondly, your Age query fails because you can't perform a LIKE on a numeric column. You should use = (equals).
Hope that helps.
Also, considering switching to prepared statements if you're using data the user has provided directly in your SQL. At the moment, you're open to SQL Injection.
you should listen to Huw Jones.
you dont want to get audited by a security firm and have sql injection problems. Parameterized your query is mySql supports it.

A field or property with the name <variable> was not found on the selected data source

It's my first time to implement INNER JOIN query in SQL.NET and C#.NET. I get this error:
A field or property with the name 'Prep_By' was not found on the selected data source.
I don't understand what is the problem, the field 'Prep_By' is existing in my database.
Here's what I got:
private void LoadFeedback()
{
con = new SqlConnection(Connectiontxt);
con.Open();
SqlCommand cmd;
if (seardata == "")
{
cmd = new SqlCommand("SELECT [Articles_Tbl].[Article_ID], [Articles_Tbl].[Title], [Articles_Tbl].[Mod_Date], [Users_Tbl].[Name] FROM [Articles_Tbl] INNER JOIN [Users_Tbl] ON [Users_Tbl].[User_ID] = [Articles_Tbl].[Prep_By] where [Articles_Tbl].[Status] = 'Approved' and (Article_ID = '')", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "ss");
gridrfqheader0.DataSource = ds.Tables["ss"];
gridrfqheader0.DataBind();
}
else {
cmd = new SqlCommand("SELECT [Articles_Tbl].[Article_ID], [Articles_Tbl].[Title], [Articles_Tbl].[Mod_Date], [Users_Tbl].[Name] FROM [Articles_Tbl] INNER JOIN [Users_Tbl] ON [Users_Tbl].[User_ID] = [Articles_Tbl].[Prep_By] where [Articles_Tbl].[Status] = 'Approved' and (Article_ID LIKE '%" + seardata + "%' or Title LIKE '%" + seardata + "%')", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "ss");
gridrfqheader0.DataSource = ds.Tables["ss"];
gridrfqheader0.DataBind();
}
}
It means that gridrfqheader0 contains a binding reference to Prep_By, but you are not including it in your SELECT statement.
Try adding it:
cmd = new SqlCommand("SELECT [Articles_Tbl].[Prep_By], [Articles_Tbl].[Article_ID])...
As a side note, your conditional statements contain a lot of duplicate code. Consider moving the code that gets data into one location so that you do't have duplicate code. For example:
if (isNullOrEmpty(seardata))
{
cmd = new SqlCommand(your query);
}
else
{
cmd = new SqlCommand(your other query);
}
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "ss");
gridrfqheader0.DataSource = ds.Tables["ss"];
gridrfqheader0.DataBind();
It can be further refactored, but this is a good start.
You are using Dataset to bind Grid, and in Grid you are using [Prep_By] field which not in your dataset.
So Simply just
Add "[Articles_Tbl].[Prep_By]" in your select statenment.
cmd = new SqlCommand("SELECT [Articles_Tbl].[Prep_By], [Articles_Tbl].[Article_ID], [Articles_Tbl].[Title], [Articles_Tbl].[Mod_Date], [Users_Tbl].[Name] FROM [Articles_Tbl] INNER JOIN [Users_Tbl] ON [Users_Tbl].[User_ID] = [Articles_Tbl].[Prep_By] where [Articles_Tbl].[Status] = 'Approved' and (Article_ID = '')", con);
make change in both sql queries..
If the grid has the column created with the source name Prep_By you have to select that column from the database.

dynamic binding of chart from database in VS2010 in C#

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

adapter.update not writing to access

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

Categories

Resources