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

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.

Related

How to pivot a list of data which has table meta data?

I have designed 2 tables to store data.
If I want to just show a list of tables with 2 columns and the column value, how should I write my LINQ query in C#?
I can't even pivot it using SQL. This my query
SELECT FieldValue, recordid
FROM [TableMetadatas] t inner join TableMetadataTemplates t2
on t.[TableMetadataTemplateId] = t2.id
where (FieldName = 'application_name' or fieldname = 'application_category')
and TableName = 'application'
group by t.FieldValue, t.recordid
order by recordid
Problem is each row is each field value instead of each record.
Application 1
application_type value 1
Application 2
application_type value 2
The expected result should be below:
Application 1 | application_type value 1
Application 2 | application_type value 2

How to display column data as row header in Crystal Reports

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...

Stored Procedure for date ranges in a single column

Finding a solution to an issue in my project
I have stages associated with contracts. That is, a contract can be in either Active stage, Process stage or Terminated stage.
I need to get the no the days the contract was in each stage.
For example, if a contract C1 was in Active stage from 20/10/2013 to 22/10/2013, then in the Process stage from 22/10/2013 to 25/10/2013 and finally in Terminated stage from 25/10/2013 to 26/10/2013 and then again in Active from 26/10/2013 to 28/10/2013, then I should get as result
Active = 4days
Process = 3days
Terminated = 1day /likewise something
My table is created with these columns:
EntryId (primary key)
StageId (foreign key to Stage table)
ContractId (foreign key to contract table)
DateofStageChange
How to do this in SQL Server?
As asked pls find the table entries:
EntryID | Stage ID | Contract ID | DateChange
1 | A1 | C1 |20/10/2013
2 | P1 | C1 |22/10/2013
3 | T1 | C1 |25/10/2013
4 | A1 | C1 |26/10/2013
5 | P1 | C1 |28/10/2013
6 | T1 | C1 |Null(currently in this stage)
Need to use group by on Stage ID
it is important to check and make sure how data is populated in your table.Based on just your sample data and also note that if your entryid is not in sequence then you can create one sequence using row_number.
declare #t table(EntryId int identity(1,1), StageId int,ContractId varchar(10),DateofStageChange date)
insert into #t values
(1,'C1','2013-10-20'),(1,'C1','2013-10-22'),(2,'C1','2013-10-22'),(2,'C1','2013-10-25')
,(3,'C1','2013-10-25'),(3,'C1','2013-10-26'),(1,'C1','2013-10-26'),(1,'C1','2013-10-28')
Select StageId,sum([noOfDays]) [totalNofDays] from
(select a.StageId,a.ContractId,a.DateofStageChange [Fromdate],b.DateofStageChange [ToDate]
,datediff(day,a.DateofStageChange,b.DateofStageChange) [noOfDays]
from #t a
inner join #t b on a.StageId=b.StageId and b.EntryId-a.EntryId=1)t4
group by StageId
You can't with your current structure.
You can get the latest one by doing datediff(d, getdate(), DateOfStageChange)
but you don't have any history so you can't get previous status
This can be done in SQL with CTE.
You didnt provide your tablenames, so you'll need to change where I've indicated below, but it would look like this:
;WITH cte
AS (
SELECT
DateofStageChange, StageID, ContractID,
ROW_NUMBER() OVER (ORDER BY ContractID, StageId, DateofStageChange) AS RowNum
FROM
DateOfStageChangeTable //<==== Change this table name
)
SELECT
a.ContractId,
a.StageId,
Coalesce(sum(DATEDIFF(d ,b.DateofStageChange,a.DateofStageChange)), 'CurrentState`) as Days
FROM
cte AS A
LEFT OUTER JOIN
cte AS B
ON A.RowNum = B.RowNum + 1 and a.StageId = b.StageId and a.ContractId = b.ContractId
group by a.StageId, a.ContractId
This really is just a self join that creates a row number on a table, orders the table by StageID and date and then joins to itself. The first date on the first row of the stage id and date, joins to the second date on the second row, then the daterange is calculated in days.
This assumes that you only have 2 dates for each stage, if you have several, you would just need to do a min and max on the cte table.
EDIT:
Based on your sample data, the above query should work well. Let me know if you get any syntax errors and I'll fix them.
I added a coalesce to indicate the state they are currently in.

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

GridView with columns and rows swapped

I am using a GridView. My requirement is like -
Name C1 C2 C3 C4
Age 24 25 26 27
Gender M F M F
I didn't get any solution. Please suggest correct solution.
Do I need to create a new UserControl for this?
Try this
http://www.aspdotnet-suresh.com/2012/09/how-to-convert-rows-to-columns-in-sql.html
Or try to create a view to bind data with ur gridview .

Categories