C# Doughnut to datatable - c#

In my MySQL database I have a table with customers, two columns are named HoursUsed & HoursAvailable. With an SQL query I get the selected customer and fill it in a datatable.
Now I want to display in the dougnut chart the HoursUsed & HoursAvailable but it fills up the whole chart with 100% instead of 2 pieces.
How can I achieve this, First I created only one Series, then I tried to add another one but I get the same result.
I tried the following code:
chartHoursCustomer.DataSource = WorkData; //Datatable
chartHoursCustomer.Series["HoursAvailable"].YValueMembers = "HoursAvailable";
chartHoursCustomer.Series["HoursUsed "].XValueMembers = "HoursUsed ";
chartHoursCustomer.DataBind();
Datatable: (Only 1 row because my SQL query is Select * FROM Customers WHERE ID = CustomerID)
HoursAvailable HoursUsed
50 44

Try this:
int ColumnCount = WorkData.Columns.Count;
string[] XPointMember = new string[ColumnCount];
int[] YPointMember = new int[ColumnCount];
for(int cnt = 0; cnt < ColumnCount; cnt++)
{
// This is assuming that you actually retrieve one row.
XPointMember[cnt] = WorkData.Rows[0][cnt].ToString();
YPointMember[cnt] = Convert.ToInt32(WorkData.Rows[0][cnt]);
}
Chart1.Series[0].Points.DataBindXY(XPointMember, YPointMember);

Related

Adding Array to Existing DataTable

I have a DataTable, dtHOURS, that consists of two columns of data passed from an SQL database and I am trying to include a third column that consists of calculated values within the array, hours. I've looked at many similar asked questions, but none of the solutions seem to work in my case. I've confirmed that the correct values are being stored in the array, but I can't seem to populate those values within column "Total Hours". All the row data comes out blank for this column.
Any help would be appreciated.
Thank you in advance.
//Create new column and row for the "Total Hours" in data table
DataColumn TotalHoursCol;
DataRow TotalHoursRow;
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
TotalHoursCol = new DataColumn();
TotalHoursCol.DataType = System.Type.GetType("System.Double");
TotalHoursCol.ColumnName = "Total Hours";
// Add the Column to the Table.
dtHOURS.Columns.Add(TotalHoursCol);
//This loop calculates the total hours for all jobs and adds them to the data table
for (int i = 1; i < numberOfRecordsHours; i++)
{
//Console.WriteLine(dtHOURS.Rows[i]["ID"]);
//Console.WriteLine(dtHOURS.Rows[i]["ACT_RUN_HRS"]);
if ((Convert.ToString(dtHOURS.Rows[i]["ID"])) == (Convert.ToString(dtHOURS.Rows[i-1]["ID"])))
{
hours[i] = (Convert.ToDouble(dtHOURS.Rows[i]["ACT_RUN_HRS"])) + (hours[i-1]);
//Console.WriteLine(hours[i]);
}
else
{
hours[i] = 0;
//Console.WriteLine("NEW JOB");
}
TotalHoursRow = dtHOURS.NewRow();
TotalHoursRow["Total Hours"] = hours[i];
dtHOURS.Rows.Add(TotalHoursRow);
//Console.WriteLine(dtHOURS.Rows[i]["Total Hours"]);
}
If I am understanding the problem correctly, it looks like you are adding a new row instead of assigning to your new column.
Instead of
TotalHoursRow = dtHOURS.NewRow();
TotalHoursRow["Total Hours"] = hours[i];
dtHOURS.Rows.Add(TotalHoursRow);
Just put
dtHOURS.Rows[i]["Total Hours"] = hours[i];

How to insert a datatable into an existing Database and overwrite duplicates?

I currently have a data table which adds new rows based on the Access table rows. In the data table, I change the value of certain columns and update them back to the database. I want to write a line of code that overwrites the data if the primary key already exists. I already looked into a query INSERT INTO table (id, name, age) VALUES(1, "A", 19) ON DUPLICATE KEY UPDATE name="A", age=19 but I can't find a way to implement this since i'm using a data adapter.
I have made the following code:
for (int i = 1; i < 19; i++)
{
Normen.Clear();
Normen.Add(-1);
for (int j = 9; j < 18; j++)
{
Normen.Add(Convert.ToInt32(NormDb.Rows[j].Field<double>(i)));
}
newRow[i] = Convert.ToInt32(GlobalMethods.LeftSegmentIndex(Normen, Convert.ToInt32(newRow[i + 18])));
}
schoolDb.Rows.Add(newRow);
Normen is in this case a list with integers i use to calculate test scores which are then inserted into the empty columns i fetched from the database table.
when this code runs, i have a datatable with rows and columns which are identical to the database columns. When this code is done, the other method is triggered to update the data into the datatable like this:
using (var dbcommand = new OleDbCommand(ZoekQuery, connection))
{
using (var SchoolAdapter = new OleDbDataAdapter(ZoekQuery, connection))
{
OleDbCommandBuilder cb = new OleDbCommandBuilder(SchoolAdapter);
cb.GetInsertCommand();
SchoolAdapter.FillSchema(schoolDb, SchemaType.Source);
SchoolAdapter.Fill(schoolDb);
Normeringen.Normeer(exportDb.Rows.Count, schoolDb, exportDb, NormDb, normcode);
SchoolAdapter.Update(schoolDb);
MessageBox.Show(Success);
}
}
But when the primary key already exists i want to overwrite the record because in this use case it is possible to grade a test with 2 different values. Is there any way i can implement this?

How to insert multiple datatable data into one datatable c#

I have a function which will return multiple ItemIDs as
for (int i = 0; i < dt.Rows.Count; i++)
{
ItemID = int.Parse(dt.Rows[i]["item_Id"].ToString());
dtAtlr = prodctsDCCls.getItemIds(ItemID);
//dtItems = dtAtlr.Copy();
}
I want to keep on searching for all ItemIds from the same table and have to save all the data in one datatable.If I copy one datatable to another datatable, that is replacing the previous datatable. but I need all the data. Please anybody help me
Use DataTable.Merge to merge two data tables. So your code would be:
for (int i = 0; i < dt.Rows.Count; i++)
{
ItemID = int.Parse(dt.Rows[i]["item_Id"].ToString());
dtAtlr.Merge(prodctsDCCls.getItemIds(ItemID)); // For Merging
}
By using DataTable.Copy, your datatable dtAtlr will have the last returned DataTable against the ItemID
You can check DataTable.Merge
Merge the specified DataTable with the current DataTable.

Duplicacy check between DATABASE and DATATABLE

i have one column in my database by name PNUMSET (Primary Key) contains unique data.(approx 1L rows)
and in application i have one datatable with one column name NEWPNUM which contains data.
i want to check that no value is matching between existing database and current datatable values..
Note:- no of rows may or may not be same in database and datatable.
so far i tried.....
String query = "Select PNUMSET FROM DUMMYTABLE";
MySqlDataAdapter msda = new MySqlDataAdapter(query, connection);
msda.Fill(dt);
for (int k = 0; k < Class1.global_dataset.Tables[0].Rows.Count; k++)
{
if (dt.Rows.Contains(Class1.global_dataset.Tables[0].Rows[k][4].ToString()))
{
MessageBox.Show("Baj Gaya Ghanta!!!!");
}
}
You can use Linq-To-DataTable to join both tables on this column, for example:
var commonRows = from r1 in dt.AsEnumerable()
join r2 in Class1.global_dataset.Tables[0].AsEnumerable()
on r1.Field<int>(4) equals r2.Field<int>(4)
select r1;
if(commonRows.Any())
{
// do something with these rows
}
(assuming the 5th column and it's type int)
Note that although Enumerable.Join is quite efficient it might be better to compare this in the database instead of loading all into memory.

How to query a datatable

I am using a XML file and the data from the XML is set into a dataset and user selected table is stored as a datatable.A query is been generated with filter criteria, grouping, aggregate function, expressions etc.Is it possible to query the datatable?
I did come accross table.Select(filter criteria, sort) method.but kindly let me know how grouping, aggregate function and expression (eg: Column1 + Column2 as SumColumn) can be got.
You could query the data using LINQ - assuming you are using a .Net Framework version that supports it. Check out LINQ To Dataset
Unfortunately, table.Select(filterCriteria, sort) is your only option without LINQ (I'm not a LINQ guru, so don't ask me what it can do).
Anytime I need something specific, I create/add that column to the DataTable.
DataTable table = new DataTable();
// code that populates the table
DataColumn c = table.Columns.Add("Column1 + Column2", typeof(int));
int Sum = 0;
for (int i = 0; i < table.Rows.Count; i++) {
r = table.Rows[i];
int col1 = (int)r["Column1"];
int col2 = (int)r["Column2"];
int both = col1 + col2;
Sum += both;
r[c] = string.Format("{0}", both);
}
DataRow summaryRow = table.NewRow();
summaryRow[c] = (int)((float)Sum / table.Rows.Count + 0.5); // add 0.5 to round
table.Rows.Add(summaryRow);
HTH.

Categories

Resources