Creating editable table from multiple sql database tables in ASP.Net - c#

I have a problem with displaying and editing some data from database to ASP.Net webpage.
Data from database consist from few different tables:
Table Files, witch contains file_id and a file name
| file_id | file_name |
--------------------------
| GUID | VARCHAR |
file is saved in database as a collection of lines for one file_id,
Table Items,witch contains basically all the lines from all the files
| item_id | file_id | step | number |
---------------------------------------------------
| GUID | VARCHAR | BIGINT | BIGINT |
Table Wording, witch contains all distinctive wording
| wording_id | wording_eng | description |
-----------------------------------------
| GUID | VARCHAR | VARCHAR |
And M_N_table witch contains connection between items and wording
| item_id | wording_id |
----------------------------
| GUID | GUID |
my final display table should look like this and should be editable
| | file1 | file2 | file3 |.........| fileN | | |
| sortorder --------------------------------------------------------------------------| wording_eng | desription |
| | step | number | step | number | step | number |.........| step | number | | |
------------------------------------------------------------------------------------------------------------------
| 1 | 10 | 15 | | | | |.........| | | something1 | something2 |
------------------------------------------------------------------------------------------------------------------
| 2 | | | | | 25 | 15 |.........| 5 | 17 | something3 | something4 |
------------------------------------------------------------------------------------------------------------------
| 3 | 2 | 4 | 5 | 16 | 17 | 3 |.........| 1 | 15 | something5 | something6 |
So for every file from some selected list,witch means variable number of files, I need to read all the wordings and if a wording is connected to item in M_N_table and that item exist in selected files I have to write value of step and number to that files column as shown, if they don't exist it will be written blank value.
Problem I'm having is creating Table or DataList or GridView where I can have connection to item_id and wording_id when I'm editing data. I managed to create "pivot" datatable in C# in backcode, using all of these tables and created gridview with dynamic number of columns but problem is I don't have information about what item_id I'm actually editing it just goes back to changing value in datatable.
What is the best way to display this data? I tried making dynamic pivot table in SQL to display it directly with gridview but without aggregation it's impossible,so I had to do it with datatable's in C# back-code. And how can I make it so that when I edit data in displayed table I get the same changes in database?

Related

Openxml excel c# does the row has merged cells

I have a spreadsheet which has two rows with three columns,
--------------------------------------
| title | id | roles
------------------------------------------
| DOC | 1 | pediatrician
-----------------------------------------
| NURSE | 2 | A&E
| | | pediatrics
| | | orthopedic
-----------------------------------------
| DOC | 3 | orthopedic
-----------------------------------------
In which row 2 (NURSE ) column 3 (roles) have a merged cell
using C# (openxml) I want to read column (roles) as one single values
can some one help?

Most efficient way to query for multiple rows and their specific sub-items (e.g. A `Sale` table and its `Items`)

I have the following tables:
Sales Table:
+----+------------+
| ID | Date |
+----+------------+
| 1 | 11/20/2018 |
| 2 | 11/21/2018 |
+----+------------+
Items Table:
+----+------------+----------+-------+----------+
| ID | FK_Sale_ID | Quantity | Price | Subtotal |
+----+------------+----------+-------+----------+
| 1 | 1 | 5 | 100 | 500 |
| 2 | 1 | 3 | 50 | 150 |
| 3 | 2 | 5 | 60 | 300 |
+----+------------+----------+-------+----------+
Currently, I query for the Sale rows and then run another query for each to retrieve all of its Items after which, I initialize them as objects in my C# Program. My problem is that I frequently run into scenarios where I'm dealing with hundreds of sales at the same time. Because of this, I have to run hundreds of queries as well.
My question is, is there a more efficient way to query for all Sales and their Items or is this it? I thought of trying a query like
SELECT *
FROM items_table
WHERE FK_Sale_ID = '1' OR FK_Sale_ID = '2'
And then manually sorting which Item belonged to which sale for initialization but this query quickly got messy after dealing with more than a few sales. Any ideas?
You can use join clause to combine multiple tables data in your query:
SELECT *
FROM items_table
JOIN sales_table ON items_table.FK_Sale_ID = sales_table.ID
WHERE sales_table.Date = #somedate
With this query when #somedate = 11/20/2018 you basically will get this data in one go:
+----+------------+----+------------+----------+-------+----------+
| ID | Date | ID | FK_Sale_ID | Quantity | Price | Subtotal |
+----+------------+----+------------+----------+-------+----------+
| 1 | 11/20/2018 | 1 | 1 | 5 | 100 | 500 |
| 1 | 11/20/2018 | 2 | 1 | 3 | 50 | 150 |
+----+------------+----+------------+----------+-------+----------+
Use SELECT items_table.* to get all fields of items table only

Restructure Attendance Table Logic

Attendance Table
id | rollno | faculty | date | PresentAbsent
---|---------|---------|------------|--------------
1 | RP1201 | ABC | 12/12/2016 | P
2 | RP1202 | ABC | 12/12/2016 | A
Leave Table
Lid | rollno | startdate | enddate | full-half-day | time
----|--------|------------|------------|---------------|---------------
1 | RP1201 | 11/12/2016 | 12/12/2016 | fullday | Not applicable
2 | RP1202 | 12/12/2016 | 12/12/2016 | halfday | 10.30-11.30
Required Report
rollno | totallecture | totalpresent | totalabsent | totalleave | withoutLeave% | withLeave%
-------|--------------|--------------|-------------|------------|---------------|-----------
RP1201 | 12 | 6 | 6 | 2 | 50% | 66.66%
From above table logic at the starting it works fine but table entry increase it takes more time to calculate report.
Please help me to change table logic which help me to execute report
in few second
Please any other suggestion would be appreciated
In your Leave table, use a trigger. Whenever there is a new Leave inserted, this trigger will update the Attendance table for PresentAbsent=Awith FD or HD.
Now while creating report, consider these FD and HD to calculate the TotalAbsent and TotalLeave.
If you are able to figure out the report query, this addition will do some modification on it. Hope I am clear. Let me know in comments if you have any question.
Do something like this.
Create Leave table, with total number of days for leave. For half day, it can be 0.5 which uses roll number are foreign key.
Something like this
Lid | rollno | startdate | enddate | full-half-day | time |numberofdays
----|--------|------------|------------|---------------|---------------|------------
1 | RP1201 | 11/12/2016 | 12/12/2016 | fullday | Not applicable|2
2 | RP1202 | 12/12/2016 | 12/12/2016 | halfday | 10.30-11.30 |0.5
After that query it like this (I'm writing a pseudo code).
select sum(days-of-leave) from LeaveTable where rollnumber=rp1001
for total absent and present do this.
select count(presentabsent) as absent from attendence
where rollnumber=rp1001 and presentabsent=A

assign unique group id to related values between two columns

I have a challenging problem.I am developing a web app (.net(C#)) that takes an specific excel file and export an delimited file with specifications explained below.
Here is the excel to be imported:
Loan_ID |Owner_Id | RelPerson_Id | Loan_Class
------------------------------------------------
L131231 | 1234 | 5678 | 0
L135148 | 2345 | 6789 | 2
L417128 | 1234 | 5432 | 1
L271237 | 5678 | 1112 | 5
L213781 | 2345 | 1113 | 4
L098932 | 1117 | 6789 | 6
L012213 | 1122 | 3311 | 2
L398721 | 2221 | 1112 | 3
L098766 | 5552 | | 1
Desired output:
Cust_ID | Cust_class | Gr_ID | Gr.Members | Gr_Class
---------------------------------------------------------------------
1234 | 1 | 1 | (1234,5678,5432,1112,2221) | 5
2345 | 4 | 2 | (2345,1117,6789,1113) | 6
5678 | 5 | 1 | (1234,5678,5432,1112,2221) | 5
1117 | 6 | 2 | (2345,1117,6789,1113) | 6
1122 | 2 | 3 | (1122,3311) | 2
6789 | 6 | 2 | (2345,1117,6789,1113) | 6
5432 | 1 | 1 | (1234,5678,5432,1112,2221) | 5
1112 | 5 | 1 | (1234,5678,5432,1112,2221) | 5
1113 | 4 | 2 | (2345,1117,6789,1113) | 6
3311 | 2 | 3 | (1122,3311) | 2
2221 | 3 | 1 | (1234,5678,5432,1112,2221) | 5
5552 | 1 | 4 | (5552) | 4
So,i will explain the case in order for you to understand.In the excel file, the first column represents the loan with unique id (no duplicates), the second column represent the Owner of the loan which is an id to identify the customer(Customer_Id).The id of Related person is also a Customer_Id which is the guarantor of the owner in this loan. A customer may have 2 or more loans and can be the guarantor in 2 or more other loans. The guarantor may be the guarantor in 2 or three loans but also may be the owner of another loan.So the owner_id and Rel_PersonId may have duplicates and the Rel_PersonId may have blank values . Class column is the rating or classification of the loan Id (Based on days in delay).
From this file i want the above output.
Cust_id is the distinct Customer Id which will be taken from both columns, Owner, and Rel Persons and will be assigned the highest class(Column :Cust_Class) of the loan he is part of(whether owner or guarantor).I already found a solution for the first two columns of the output, i am stuck with grouping.
Grouping logic:Two customers involved in one loan are grouped together, but if any of this customers is involved in another loan(wether like an Owner or a guarantor) with another Cutomer X, and in another with another customer Y both customers will be in a group with the first two, and if customer x or y are involved in other loans with other customers z and a those are added to the first group with the others too, and this cycle continues till there are no more customer connected with those that are already in the group.See the desired output for understanding.The group id will be generated automatically and sequentially(1,2,3,4) .The group class will be the class of the customer with the highest Cust_class of the group.
I am using sql server to import the excel.Any idea on how to build a query to gain this output or any other solutions?
Hope you can understand, feel free to ask for any further clarification.
Thank you.

MS SQL Table structure for dynamic app

I am building a dynamic app.
The basic of the app is i can create a question. A question may have many columns. These columns will be displayed in table structure. Then the user will fill the details. Below is the sample.
Question:
1. List the projects available and their deadlines?
Columns:
1. Project - Textbox
2. Deadline - Textbox
So i will show this data in the table like below.
The user will fill the row and click on submit button. Another empty row will get added to add the another response like below.
The user will submit any no of responses.
Like this the no of columns will be dynamic and also the each column will have some validation like Unique,AcceptsNull etc...
How to create table structure in ms sql server to save the question,column and the responses.
Please help thanks in advance..
Your scenario is similar with the way in which Microsoft Project Server stores its data in MSSQL. In MSPE for each project, a task can have a dynamic number of columns. In your case, if you can make all your column types as VARCHAR, than your scenario simplifies even more.
The proposed structure would be like this:
This is the table with the questions:
tbl_Questions
+------------------------------------------------------------------+
| Id | Question |
+----------------------------------------------------------------- +
| RandomGUID1 | "List the projects available and their deadlines?" |
| RandomGUID2 | "List the projects available?" |
+------------------------------------------------------------------+
tbl_RelationTable
+---------------------------------------------------------------------------+
| Id | QuestionId | Column1 | Column2 | ... |Column 1000 |
+---------------------------------------------------------------------------+
| 1 | RandomGUID1 | RandomGUID111 | RandomGUID112 | | null |
| 2 | RandomGUID2 | RandomGUID113 | null | | null |
+---------------------------------------------------------------------------+
In this table you store all relations between tbl_Questions and the other tables. You define in this table a sufficient number of columns, here I proposed 1000, but maybe in your case 10-15 columns would be enough. One important aspect is that you need to use GUIDs for Question ID in order to be unique.
Now we define the real data tables.
This is the table where you define your answer columns:
tbl_AnswerColumns
+-----------------------------------------------+
|Id| RelationTableId | QuestionId | ColumnName |
+-----------------------------------------------+
| 1| RandomGUID111 | RandomGUID1 | Title |
| 2| RandomGUID112 | RandomGUID1 | Answer |
| 3| RandomGUID113 | RandomGUID2 | Title |
+-----------------------------------------------+
This is the table where you store the answers values:
tbl_AnswerValues
+-----------------------------------------------+
|Id| RelationTableId | QuestionId | Answer |
+-----------------------------------------------+
| 1| RandomGUID111 | RandomGUID1 | "Answer1" |
| 2| RandomGUID112 | RandomGUID1 | "Answer2" |
| 3| RandomGUID113 | RandomGUID2 | "Answer3" |
+-----------------------------------------------+
You need to define foreign keys in all tables in order to make the data retrieval much faster. This is why each table should contain a foreign key to the QuestionId.
By the information you've given I'm thinking something like this?
Questions
+---------------------------------------------------------+
| Id | Question |
+----+--------------------------------------------------- +
| 1 | "List the projects available and their deadlines?" |
+---------------------------------------------------------+
Answers
+-----------------------------------------------------------------------------------+
| Id | QuestionId | Order | Title | Anwser | Unique | Nullable | Values |
+----+------------+-------+------------+-------------+--------+----------+----------+
| 1 | 1 | 0 | "Project" | "Project 1" | True | False | "P1, P2" |
| 2 | 1 | 1 | "Deadline" | "Apr-15" | False | True | NULL |
+-----------------------------------------------------------------------------------+
Edit: I've added the validations. If you want the same validations for all anwers for the same question, you can also move the validation columns to the Questions-table.

Categories