I want to implement a limited time period membership in asp.net MVC. For that, I manually introduced a new column ExpiryDate in the AspNetUsers table. When a new user registers itself this field will automatically generate an expiry date with the help of the getdate() method.
CREATE TABLE [dbo].[AspNetUsers] (
[Id] NVARCHAR (128) NOT NULL,
[Email] NVARCHAR (256) NULL,
[EmailConfirmed] BIT NOT NULL,
[PasswordHash] NVARCHAR (MAX) NULL,
[SecurityStamp] NVARCHAR (MAX) NULL,
[PhoneNumber] NVARCHAR (MAX) NULL,
[PhoneNumberConfirmed] BIT NOT NULL,
[TwoFactorEnabled] BIT NOT NULL,
[LockoutEndDateUtc] DATETIME NULL,
[LockoutEnabled] BIT NOT NULL,
[AccessFailedCount] INT NOT NULL,
[UserName] NVARCHAR (256) NOT NULL,
[RegisterDate ] DATETIME DEFAULT (getdate()) NULL,
[ExpiryDate] DATETIME DEFAULT (getdate()+(60)) NULL,
CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
);
Now, I want to check that expiry date value everytime when that user tries to login. Where and what logic should I implement in my code?
Related
I'm creating a meeting program in c# using SQL. How would i go about adding multiple users from the user table to one meeting and vice versa (multiple meetings to one user from the table)
Sorry I'm new to SQL
SQL DB Format: User
[UserID] VARCHAR (50) NOT NULL,
[FirstName] VARCHAR (50) NOT NULL,
[LastName] VARCHAR (50) NOT NULL,
[Username] VARCHAR (50) NOT NULL,
[Password] VARCHAR (50) NOT NULL,
[Exc1] BIT NULL,
[Exc2] BIT NULL,
[Exc3] BIT NULL,
[Exc4] BIT NULL,
[Exc5] BIT NULL,
[Importance] INT NOT NULL,
[Meetings] VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([UserID] ASC),
CONSTRAINT [Meetings] FOREIGN KEY ([Meetings]) REFERENCES [dbo].[Meeting] ([MeetingID])
Meeting
[MeetingID] VARCHAR (50) NOT NULL,
[Title] VARCHAR (50) NOT NULL,
[Date] DATE NOT NULL,
[StartTime] TIME (7) NOT NULL,
[EndTime] TIME (7) NOT NULL,
[Location] VARCHAR (50) NOT NULL,
[Exc1] BIT NULL,
[Exc2] BIT NULL,
[Exc3] BIT NULL,
[Exc4] BIT NULL,
[Exc5] BIT NULL,
[Summary] VARCHAR (MAX) NULL,
[UserID] VARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([MeetingID] ASC),
CONSTRAINT [UserID] FOREIGN KEY ([UserID]) REFERENCES [dbo].[Users] ([UserID])
In this case it is a n to n relation and you need an additional table like
usermeetings
[UsermeetingsID]
[UserID]
[MeetingID]
And you can remove the userid columns from meeting table and meetings columns from user table
My create table code like :
CREATE TABLE [dbo].[DeTai](
[ID] [int] IDENTITY(1,1) NOT NULL,
[MaDeTai] AS ('DT'+right('000000'+CONVERT([varchar](10),[ID]),(6))) PERSISTED NOT NULL,
[TenDeTai] [nvarchar](255) NOT NULL,
[LinhVuc] [nvarchar](255) NOT NULL,
[Nam] [int] NOT NULL,
[MaGV] [varchar](8) NOT NULL,
)
When I use linq to sql to insert a values this messages come out
The primary key column of type 'VarChar(8)' cannot be generated by the server.
what should i do ?
you have to designate the primary key, setting identity is not enough
CREATE TABLE [dbo].[DeTai](
[ID] [int] IDENTITY(1,1) NOT NULL,
[MaDeTai] AS ('DT'+right('000000'+CONVERT([varchar](10),[ID]),(6))) PERSISTED NOT NULL,
[TenDeTai] [nvarchar](255) NOT NULL,
[LinhVuc] [nvarchar](255) NOT NULL,
[Nam] [int] NOT NULL,
[MaGV] [varchar](8) NOT NULL,
constraint PK_DeTail_ID primary key (ID)
)
I have created a junction table between two of my tables to create a many to many relationship between them.I am able to save data to them, but can't access that data again. This is written in MVC ASP.NET by the way.
My first table:
CREATE TABLE [dbo].[UserInfo] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[FirstName] NVARCHAR (50) NULL,
[LastName] NVARCHAR (50) NULL,
[Email] NVARCHAR (256) NOT NULL,
[Image] VARBINARY (MAX) NULL,
[Approved] BIT NOT NULL,
[Color] NVARCHAR (10) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
My second table:
CREATE TABLE [dbo].[Events] (
[Id] NVARCHAR (128) NOT NULL,
[Name] NVARCHAR (100) NOT NULL,
[StartDate] DATETIME NULL,
[EndDate] DATETIME NULL,
[Approved] BIT NOT NULL,
[room_id] INT NULL,
[color] NVARCHAR (10) NOT NULL,
[Owner] INT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Events_Rooms] FOREIGN KEY ([room_id]) REFERENCES [dbo].[Rooms] ([_key])
);
My junction table:
CREATE TABLE [dbo].[UserToEvent] (
[UserId] INT NOT NULL,
[EventId] NVARCHAR (128) NOT NULL,
CONSTRAINT [UserId_EventId_pk] PRIMARY KEY CLUSTERED ([UserId] ASC, [EventId] ASC),
CONSTRAINT [FK_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[UserInfo] ([Id]),
CONSTRAINT [FK_Event] FOREIGN KEY ([EventId]) REFERENCES [dbo].[Events] ([Id])
);
Code to add new relationship:
Event e = await db.Events.FindAsync(id);
string email = User.Identity.Name;
UserInfo user = db.UserInfoes.Where(x => x.Email == email).First();
user.Events.Add(e);
e.UserInfoes.Add(user);
db.Entry(user).State = EntityState.Modified;
db.Entry(e).State = EntityState.Modified;
await db.SaveChangesAsync();
Code to access relationship:
UserInfo user = await db.UserInfoes.FindAsync(id);
List<Events> events = user.Events;
I would expect events to be filled with all events associated with user, but it never has any events.
I have found a work-around which is sufficient to my needs. I added another attribute to my junction table, which then allowed me to access it within my controllers (Since there weren't only two primary keys). From here I was able to gather the data from it just like any other table. Not sure why the above method does not work however. I have used that method before without a hitch. If anyone has an answer, I would love to hear it.
I need a way to get column definition from query result. Right now I'm using SQL Server 2012.
Here is the example scenario, I have two tables which are Event and Attendant whose definitions are below :
CREATE TABLE [dbo].[Event] (
[Id] INT NOT NULL,
[Name] NVARCHAR (50) NULL,
[Description] NVARCHAR (50) NULL,
[StartDate] DATETIME NULL,
[EndDate] DATETIME NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[Attendant] (
[Id] INT NOT NULL,
[EventId] INT NOT NULL,
[Name] NVARCHAR (50) NULL,
[Company] NVARCHAR (50) NULL
PRIMARY KEY CLUSTERED ([Id] ASC)
);
And then I have query such as :
SELECT Event.Name as EventName, Attendant.Name as GuestName
FROM Event
INNER JOIN Attendant ON Event.Id = Attendant.EventId
How Can I get the column definition for above example query result? My objective is to generate poco class to represent each record of any query result using c#.
you can use sp_columns sproc to retrieve information about the column definition of a specified table ... like this:
exec sp_columns Attendant
Use sp_columns, it returns column information for the specified objects(tables).
Refer this link for details.
select * from information_schema.columns where table_name = '<tablename>'
I have two Windows forms. One is Insert new member and other one is Edit member.
When inserting a new member, I can select the Membership Date. I would like to get the selected Membership Date into the Edit member form, where I have a datetimepicker.
Example: I create a new member with an ID A-15 and their membership date of 15/05/2013. When I want to edit this member, I will type the Membership ID into the Edit member form and run the program. This will load all details into textboxes and the Membership Date into the datetimepicker.
I am having trouble with the datetimepicker. How do I select the date and display it in the datetimepicker from the information in the MySQL table?
Code:
CREATE TABLE [DBO].[DONORINFO] ( [MEMBERSHIP_NO] [VARCHAR](10) PRIMARY KEY NOT NULL,
[NAME_STATE] [VARCHAR](10) NULL, [FULL_NAME] [VARCHAR](100) NULL, [STREET_NO] [VARCHAR]
(50) NULL, [STREET] [VARCHAR] (50) NULL, [CITY] [VARCHAR] (50) NULL, [TEL] [VARCHAR]
(50) NULL, [EMAIL] [VARCHAR] (50) NULL, [DIOCESE] [VARCHAR] (50) NULL, [CUS_TYPE]
[VARCHAR] (50) NULL, [MEMBERSHIP_DATE] [DATE], );
you need to get membership date value from the datatable and set it as selected date of datetimepicker
DateTime membershipdate = YourDataTable.Row[0].Field<DateTime>("MEMBERSHIP_DATE");
datetimepicker.Value= membershipdate;