C# : display SQL query on a chart using SqlDataReader - c#

This is my query I want to display it like this: sum of commands by command date (each date should have a Bar that shows how many sales (commands) by that particular date (day)
select
date_comm as 'jourdetravail',
sum(TotalCom) as 'recettedujour'
from
Commande_art
group by
date_comm

You can use this script to fill a datatable and then use the datatable as source for the chart and also you will need the chart to be indexed on x axis
SqlConnection con = new SqlConnection("Data Source=<Your Server>;Initial Catalog=<YourDatabase>;Persist Security Info=True;User ID=<yourID>;Password=<yourPassword>");
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select [date_comm] as 'jourdetravail', sum([TotalCom]) as 'recettedujour' from [dbo].[Commande_art] group by [date_comm]";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
and then your "dt":
<yourChart>.DataSource = dt;
You will also need to set the 2 series of the chart with the same name of the column of the quesry in order to show them correctly

Related

How to query two access database(accdb) files that have different schema?

I need your help.
Suppose there are two access database(accdb) files which have some columns with same names but some are not. For example:
start
Then how to make a query to both database at the same time?
OleDbConnection Conn = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=..\\DATABASE_A.accdb;");
OleDbCommand command = new OleDbCommand();
OleDbDataAdapter adapter = new OleDbDataAdapter();
DataTable dt = new DataTable();
Con.Open();
string query = "Select [JOB],[DATE] From TABLE_A WHERE [NAME] = ?";
command.CommandText = query;
command.Parameters.AddWithValue("#NAME", $someDate);
command.Connection = Conn;
adapter.SelectCommand = command;
adapter.Fill(dt);
Conn.Close();
However, how about DATABASE_B.accdb to fill the same DataTable dt but TABLE_B's column has different name?I want to form a datatable like that:
result
Thanks for any suggestion.
In SQL you would use a union query.
In C# you could use LINQ Union.

Getting empty result in output when using Select statement in Grid-View

Here what I have done
I make a GridView and choose the data key name as id and in the basis of id I want to show the DetailsView. Here the CS code
using (SqlConnection con1 = new SqlConnection("Data Source= IA; initial catalog =aip; integrated Security=true;"))
{
con1.Open();
SqlCommand cmd = new SqlCommand("select * from Pm where user_id='" +(String)Session["uid"]+ "'", con1);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con1.Close();
}
Then I make DetailsView method on SelectedIndexChanged:
But it is showing empty DetailsView in output on selecting 'Select' option
here is the code image
enter image description here
You have used the DataSet not DataTable. So you could either do this:
da.Fill(ds,"tbl");
GridView1.DataSource = ds.Tables[0];
Or use a DataTable instead:
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
Also you should always use parameterized queries to avoid SQL Injection. Something like this:
SqlCommand cmd = new SqlCommand("select * from Pm where user_id=#userId", con1);
cmd.Parameters.AddWithValue("#userId", (String)Session["uid"]);
Also have a look at this: Can we stop using AddWithValue() already?
you need to set DataTable instead of DataSet
GridView1.DataSource = ds.Tables["Pm"];
#PIYUSH Itspk please check query and replace
SqlCommand cmd = new SqlCommand("select * from Pm where user_id=" +Session["uid"].tostring()+ ", con1);
i think its help you if any other query please notify me

The value of TIME(2) column is shown with extra precision zeros while the query returns only 2

I am running a simple sql query using the following code. In my query there is a column called duration with type TIME(2). When I run the query normally on my MySQL Workbench the value of duration is correctly shown with only 2 precision points. But when I fill my DataSet using a SqlDataAdapter like the following this value is shown with 6 extra zeros on my DataGridView:
using (IDbConnection conn = dataFactory.GetDbConnection())
{
conn.ConnectionString = Common.Conf.ConnectionString;
MySqlDataAdapter daRunHistory = new MySqlDataAdapter();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "SELECT Name,Duration FROM MyTable;";
cmd.Connection = conn;
dsRunHistory = new DataSet();
daRunHistory.SelectCommand = cmd;
conn.Open();
daRunHistory.Fill(dsRunHistory);
conn.Close();
}
And it is not just the DataGridView, the zeros are in my DataSet as well.
I ended up changing my query as follows to format my time:
SELECT Name, TIME_FORMAT(Duration, '%H:%i:%s') AS 'Duration'
FROM MyTable

Insert and restore and search in combox?

Salam Alekom .
I have comboBox filling it from database as
SqlConnection con = new SqlConnection(strcon);
con.Open();
SqlCommand scm = new SqlCommand();
scm.Connection = con;
scm.CommandText = "select * from com";
SqlDataAdapter adpt = new SqlDataAdapter(scm);
DataTable dt = new DataTable();
adpt.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = dt.Columns["com_name"].ToString();
comboBox1.ValueMember = dt.Columns["com_id"].ToString();
Where filling with all data that store in the table from database.
This comboBox use also to insert to another table in database
This also poses no problem
The problem when making search in table to get some data. I need to get the text value that store in table and equal to text value of comboBox and sort it in position 0 of this comboBox
I made the search no problem but what property or method that allow me to put the value in position 0 with out affecting the other values that be in combobox
SqlConnection con = new SqlConnection(strcon);
con.Open();
using(SqlCommand scm2 = new SqlCommand())
{
scm2 .Connection = con;
scm2.CommandType = CommandType.StoredProcedure;
scm2 .CommandText = "SP_retrieve_data";
scm2.Parameters.AddWithValue ("#id", textBox1.Text);
SqlDataReader dr = scm2.ExecuteReader();
while (dr.Read())
{
comboBox1. = dr["com_name"].ToString();//what code accept the value
}
}

DataGridView don't go into Edit Mode

I fill DataGridView this way:
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.MARKETConnectionString))
{
conn.Open();
String SQL = #"select INN, Idx, Adr, 'P' as Src from AdrP where Idx is null
union all
select INN, Idx, Adr, 'K' as Src from AdrK where Idx is null";
SqlCommand cmd = new SqlCommand(SQL, conn);
var RD = cmd.ExecuteReader();
if (RD.HasRows) dt.Load(RD);
dataGridView2.DataSource = dt;
}
But DataGridView don't go into edit mode by double click. If I remove 'Union All' in SQL query then double click works properly (switches the DataGridView in edit mode). But I need select from two tables with Union.
I try to call dataGridView2.BeginEdit(true) in DataGridView DoubleClick event. But it does not help.
What is the right way to edit DataGridView with Data Source selected from two tables?
Don't use a DataReader in this case. Use an Adapter instead:
using (SqlDataAdapter adp = new SqlDataAdapter(SQL, conn)) {
adp.Fill(dt);
}

Categories