Getting two different types of value from two tables and show in gridview template [closed] - asp.net

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a little confusing scenario here.I have got 2 tables.One table stores the new users who have registered(tblAllUsers) and another one stores the new events created(tblAllEvents).Now i want to display the combination of both tables in a gridview ORDER BY Registration datetime such that:
if the latest value comes in from tblAllUsers,it should show
"Michael just registered on the website" where Michael is the firstname of the user in tblallusers
and if value comes in from tblAllEvents,it should show
"Piano Exhibition was created by User34" where Piano Exhibition is the EventName in tblAllEvents
By far I've tried to create a union of two tables like :
Select FirstName,RegDateTime from tblAllUsers
UNION ALL
Select EventName,RegDateTime from tblAllEvents
ORDER BY RegDateTime DESC
but the above gives out FirstName and Title under column FirstName and will be hard to distinguish.
How do i design my gridview template for this?
Thanks in advance.

You could change your query like this:
SELECT FirstName, NULL AS EventName, RegDateTime
FROM tblAllUsers
UNION ALL
SELECT NULL, EventName, RegDateTime
FROM tblAllEvents
ORDER BY RegDateTime DESC
So now you have three columns and FirstName or EventName is filled depending on which table it comes from.
In your GridView you can test which value is empty (EventName or FirstName) and change the text accordingly.
EDIT:
You could even return only the first row (with the latest RegDateTime):
SELECT TOP 1 *
FROM (SELECT FirstName, NULL AS EventName, RegDateTime
FROM tblAllUsers
UNION ALL
SELECT NULL, EventName, RegDateTime
FROM tblAllEvents) AS a
ORDER BY RegDateTime DESC

You can optionally add an event type column and use it to check how to handle it on the grid view
Select 'User' as EventType, FirstName,RegDateTime from tblAllUsers
UNION ALL
Select 'Event' as EventType, EventName,RegDateTime from tblAllEvents
ORDER BY RegDateTime DESC
You might need to add the user name as well in the 'Event' since you also show the name of the user who created the event

Related

Change a Column to Primary Key in sqlite [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a table in my app
CREATE TABLE VisitDetails (VisitId INTEGER, UserId TEXT, VisitNumber
TEXT, JobId INTEGER, JobNumber TEXT, StatusId INTEGER, Status TEXT,
SignatureRequired BOOL, StatusDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP
,StartDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP, EndDate TIMESTAMP
DEFAULT CURRENT_TIMESTAMP, IsPPMJob BOOL)
and it was working fine but now I need to alter this table to make already existing column 'VisitId' to become the primary key. Can somebody help me please? It might work by adding Unique constraint to VisitId column of the table but I am trying to put some efficient solution !
You can't change SQLite tables field's primary key once the table is created,
Possible solution is,
Create new table with desired primary key
Copy all data
Drop old table
Here is the documentation link : https://www.sqlite.org/omitted.html

Last Function in Query

So I currently have a database that keeps tracks of projects, project updates, and the update dates. I have a form that with a subform that displays the project name and the most recent update made to said project. It was brought to my attention however, that the most recent update to a project does not display correctly. Ex: shows the update date of 4/6/2017 but the actual update text is from 3/16/2017.
Doing some spot research, I then learned that Access does not store records in any particular order, and that the Last function does not actually give you the last record.
I am currently scouring google to find a solution but to no avail as of yet and have turned here in hopes of a solution or idea. Thank you for any insight you can provide in advance!
Other details:
tblProjects has fields
ID
Owner
Category_ID
Project_Name
Description
Resolution_Date
Priority
Resolution_Category_ID
tblUpdates has these fields:
ID
Project_ID
Update_Date
Update
there is no built-in Last function that I am aware of in Access or VBA, where exactly are you seeing that used?
if your sub-form is bound directly to tblUpdates, then you ought to be able to just sort the sub-form in descending order based on either ID or Update_date.
if you have query joining the two tables, and are only trying to get a single row returned from tblUpdates, then this would do that, assuming the ID column in tblUpdates is an autonumber. if not, just replace ORDER BY ID with ORDER BY Update_Date Desc
SELECT a.*,
(SELECT TOP 1 Update FROM tblUpdates b WHERE a.ID = b.PROJECT_ID ORDER BY ID DESC ) AS last_update
FROM tblProjects AS a;

How to avoid duplicate records insertion in database table using stored procedure? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a database table with these columns:
Banner_Title varchar(500)
Existing_Banner_Image varchar(500)
Banner_URL varchar(500)
Banner_ALT varchar(500)
CheckBoxText varchar(500)
[for these 3 checkboxes have taken chkArticles,chkFittness,chkHealthArticle
but I want to check checkboxes also while insertion of data.For that I added one extra column in datatable named as sectionId for chkArticle checkbox I will assign 10 as a sectionId ,for chkFittness sectionId 11 and for chkHealthArticle as 12 so when I insert banner_title='article1', click chkArticle checkbox and fill other field data as mentioned in table format from then data should insert but next time when I try banner_title='article1' and click same checkbox then data not allowed to insert.but when I give banner_name='article1' and click another checkbox chkFittness it should allowed though the name of banner is same but maintained sectionid for checkboxes is different then what changes need to do stored procedure?? Basically I want to maintained unique banner_title for each checkbox click and to differentiate I maintained sectionid.plz help me
Create Unique constraint:
USE YourDatebase;
GO
ALTER TABLE YourTable
ADD CONSTRAINT YourConstraintName UNIQUE (Banner_Title);
GO
Check if banner title exists before insert attempt:
IF NOT EXISTS(SELECT Banner_Title FROM YourTable WHERE Banner_Title =
'TitleYourAreAttemptingToInsert')
BEGIN
INSERT .....
END

Login credentials in almost 5 different tables and how to go about it [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am about to start creating a login page for my website.
There are various types of users like Student, employee, manager, librarian etc and I have created different tables for all. The problem comes when I try to login them because I need to go through all the tables to validate that user to log in.
I am not sure how can that be done though, I am just starting to guess.
I understand that i should have one table which stores just the login credentials of all types of user which makes it easier. But cant i just have it the way i want and that is by having separate tables for all.
There is no code to post since i am just looking for a logic so dont start downvoting me for nothing. Any help will be appreciated.
Just a follow-up to the comment thread above, you may consider creating a view to union all of the records together for the login credentials. Then you can write your login logic against a single entity.
This still leaves the issue of what if these credentials need to change (ie: password change/reset). For that, you may consider a trigger against the view so that you can issue UPDATE statements against the single entity as well.
CREATE VIEW v_Users
AS
SELECT
[UserId],
[Username],
[Password]
FROM [Student]
UNION ALL
SELECT
[UserId],
[Username],
[Password]
FROM [employee]
UNION ALL
SELECT
[UserId],
[Username],
[Password]
FROM [manager]
UNION ALL
SELECT
[UserId],
[Username],
[Password]
FROM [librarian]
GO
CREATE TRIGGER tg_UpdateUser ON v_Users
INSTEAD OF UPDATE
AS
BEGIN
SET NOCOUNT ON
-- Update student
UPDATE TGT SET
[Username] = INSERTED.[Username],
[Password] = INSERTED.[Password]
FROM [Student] TGT
INNER JOIN INSERTED
ON TGT.[UserId] = INSERTED.[UserId]
-- Update employee
UPDATE TGT SET
[Username] = INSERTED.[Username],
[Password] = INSERTED.[Password]
FROM [employee] TGT
INNER JOIN INSERTED
ON TGT.[UserId] = INSERTED.[UserId]
-- ... REPEAT FOR EACH OF YOUR TABLES
SET NOCOUNT OFF
END
GO

last record from database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Get the last record from a Microsoft SQL database table using ASP.net (VB) onto a web form.
I'm assuming he's trying to retrieve the last inserted record. As Ariel pointed out, the question is rather ambiguous.
SELECT TOP 1 * FROM Table ORDER BY ID DESC
If you have an identity column called ID, this is easiest. If you don't have an identity PK column for example a GUID you wont be able to do this.
Here is a basic solution:
var order = (from i in db.orders
where i.costumer_id.ToString() == Session["costumer_id"]
orderby i.order_id descending
select i).Take(1).SingleOrDefault();
You need to be more specific with actually putting it onto a web form, but the SQL to get the last record is:
SELECT *
FROM TABLE_NAME
WHERE ID = (SELECT MAX(ID) FROM TABLE_NAME)
Where ID is your ID and TABLE_NAME is your table name.

Resources