How to display column data as row header in Crystal Reports - c#

product Table
I have 2 tables, product table with productID and productName :
Product id productName
-----------------------
1 BUR
2 bariis
3 moos
and branch table with branch Id and branchName:
branchid branchName
----------------------
B1 Sb1
B2 Cb2
I want to display a report in Crystal Reports like this
branch BUR bariis moos
--------------------------
Sb1 3 4 4
Cb2 2 1 5
Look at the image for clear understanding

Simply use the option to Insert, CrossTab...

Related

Getting ID (PK) from Table based on Selected Columns in ComboBox

I have a table of STATUS and it contains these columns:
ID ColA ColB ColC
and I want these 3 columns to be shown in ComboBox of a WPF Form. Fine! I will do it by getting Distinct ColA,ColB,ColC respectively (Kindly correct me if I am wrong)
Now I want to insert an entry in another table based on these comboboxes.
This table, STATUS_HISTORY has following structure (not related to question I think):
ID EmpID (FK from Emp) StatusID (FK from Status) UpdationDate
Now how would I get the ID of status based on the selected ComboBoxes? Should I use a query like this:
SELECT ID from STATUS WHERE ColA LIKE '#SelectedColAValue' AND ColB LIKE '#SelectedColBValue' AND ColC LIKE '#SelectedColCValue'
It sounds a very trivial way to me. Is there any better way to do it?
UPDATE
STATUS Table has rows like this:
ID ColA ColB ColC
1 A1 B1 C1
2 A1 B1 C2
3 A1 B2 C1
4 A1 B2 C2
5 A1 B3 C1
6 A1 B3 C2
7 A2 B1 C1
8 A2 B1 C2
Now if I select A1 in ComboBox1, it corresponds to multiple rows (and so on..)
I hope it explains it further
The easier way to do is create Status .NET object and bind it to the comboboxes.
You could bind the same object to all the comboboxes and display which-ever value you want to display in the combo-box.
Check DisplayMemberPath , SelectedItem ,SelectedValue and SelectedValuePath
And Just pass the SelectedItem ID to the database.
Make the ID column of STATUS a String and generate it like ColA + ColB + ColC. It must be unique for all rows. So then user select values from ComboBoxes make the same concatination of ComboBoxA.Value + ComboBoxB.Value + ComboBoxC.Value. That will be PK to insert in STATUS_HISTORY.

Datatable - Using linq I want to update a row with the count

I have a C# datatable with:
Id ParentId Name
======== =========== =====
1 A
2 1 b
3 1 c
4 2 d
6 5 e
I want to add a column or just the result that will identify any ParentIds that do not have an Id. Basically in the above table, I want to find Id=6 since ParentId 5 is invalid.
I want to identify anywhere a parentId is not listed as an Id
I thought this could be accomplish with Linq.
This will return all rows where the ParentId does not appear in any row as Id
rows.Where(r1 => !rows.Any(r2 => r1["ParentId"] == r2["Id"]))

Selecting Data from another table SQL Select Query

Here is 2 Tables that are joined by the StaffID
Job Table
=========
JobID AssignedTo(StaffID) Created By(StaffID)
1 2 1
2 3 2
Staff Table
============
StaffID Name
1 May
2 Bob
3 Mary
I need An SQL Statement to get the job details with the corresponding staff name but have problems doing so as i'm unable to differentiate the columns as they are using the same table. The end result should look like this
JobID Assigned To Created By
1 Bob May
2 Mary Bob
You need to join Staff table twice
select J.JobId, S1.Name AS AssignedTo, S2.Name AS CreatedBy
from Job J
inner join Staff S1 on S1.StaffID = J.AssignedTo
inner join Staff S2 on S2.StaffID = J.CreatedBy

select two columns from two different tables in the same db using mysql + use the output of query in C#

Dear Friends,
i want to select two columns from two different tables in the same db using mysql and set the output of the query to a variable in c#.
currently my code is as shown below:
MySqlCommand logcmdCheck = new MySqlCommand(query, connectionCheck);
string query = "SELECT DB.table1.column1,DB.table1.column2,DB.table2.column1,DB.table2.column2,DB.table2.column3 FROM DB.table1 WHERE DB.table1.column1=?x,DB.table2 WHERE DB.table2.column1=?y";
logcmdCheck.Parameters.AddWithValue("?x",UserName);
logcmdCheck.Parameters.AddWithValue("?y",emailID);
MySqlDataReader ldr = logcmdCheck.ExecuteReader();
A = ldr[0].ToString();
B = ldr[1].ToString();
C = ldr[2].ToString();
D = ldr[3].ToString();
E = ldr[4].ToString();
Error: Mysql query syntax is wrong.
Kindly please help me out with the mysql command to perform the requirement.
Thanks in advance
Suraj
You're going to have to use a SQL Join. Check it out here http://www.w3schools.com/sql/sql_join.asp. You need to have a foreign key in one of the tables that allows you to connect to the primary key of the other table. Every good database should be set up with tables that have foreign keys.
For example:
Table 1:
OrderNumber Name Order Total
1 John Smith 10.00
2 Sally Smith 5.00
3 Berry Jones 25.00
Table 2:
Item Number ItemTotal OrderNumber
1 5.00 1
2 5.00 1
3 2.50 2
4 2.50 2
5 25.00 3
In table 2 the OrderNumber is the foreign key that is able to join to table one. So your syntax would be:
SELECT * FROM table1 JOIN table2 ON table2.OrderNumber = table1.OrderNumber
That will give you one table which you can read from.

Create SQLite Database From Other SQLite Database C#

I have an SQLite database which a set of tables. All data in these tables can be isolated into a groups of sets by some id. For example:
Table A
ID value1 value2
1 asd fgh
2 sdf ghj
Table B
ID ID2 value4
1 10 vbn
2 11 bnm
Table C
ID2 value5 value6
10 asdfg qwer
11 tyui hjkl
Where each ID column will map the other ID and each ID2 will map to the other ID2.
I want to take this database, and generate a series of smaller databases, each of which have the same structure, but will only contain data from 1 ID:
Database1.sqlite
Table A
ID value1 value2
1 asd fgh
Table B
ID ID2 value4
1 10 vbn
Table C
ID2 value5 value6
10 asdfg qwer
Database2.sqlite
Table A
ID value1 value2
2 sdf ghj
Table B
ID ID2 value4
2 11 bnm
Table C
ID2 value5 value6
11 tyui hjkl
I could just create the tables one by one, gather all data per ID through a series of SELECT statements, then add it through a series of INSERT statements, but I think there has to be a better way.
My other idea is that I can create a series of views, each of which isolates the data into the format above. From there, I could just write these series of views an sqlite file as a database.
My question is how realistic is my view generation idea? Would it be possible to generate a series of views that mimic each table's structure, but for say where ID = 1 and then save those views as an sqlite file? All of this will need to be done in C#. Is there a better way to do what I am trying to do?
Some More Info
These tables can have multiple rows with the same IDs. There will also need to be some primary key / foreign keys for each table. Ideally, we could then take these smaller tables, and then compress them all into a larger table in the future.
It is possible to combine INSERT and SELECT.
Together with ATTACHed databases, this allows to do the copying with one statement per table:
ATTACH 'C:\some\where\Database1.sqlite' AS db1;
CREATE TABLE db1.A(ID, value1, value2);
CREATE TABLE db1.B(ID, ID2, value4);
CREATE TABLE db1.C(ID2, value5, value6);
INSERT INTO db1.A SELECT * FROM main.A WHERE ID = 1;
INSERT INTO db1.B SELECT * FROM main.B WHERE ID = 1;
INSERT INTO db1.C SELECT * FROM main.C
WHERE ID2 IN (SELECT ID2 FROM B WHERE ID = 1);
ATTACH 'C:\some\where\Database2.sqlite' AS db2;
CREATE TABLE db2.A(ID, value1, value2);
CREATE TABLE db2.B(ID, ID2, value4);
CREATE TABLE db2.C(ID2, value5, value6);
INSERT INTO db2.A SELECT * FROM main.A WHERE ID = 2;
INSERT INTO db2.B SELECT * FROM main.B WHERE ID = 2;
INSERT INTO db2.C SELECT * FROM main.C
WHERE ID2 IN (SELECT ID2 FROM B WHERE ID = 2);

Categories