Currently i have 2 tables, which is user_table and application_table.
The main table is application_table, but i need some information to display which only can find in user_table.
CREATE TABLE [dbo].[Application_Table] (
[Id] INT IDENTITY (0, 1) NOT NULL,
[Name] VARCHAR (50) NULL,
[Date] DATE NULL,
[Vehicle] VARCHAR (50) NULL,
[DestinationFrom] VARCHAR (50) NULL,
[DestinationTo] VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
here is the second table:
CREATE TABLE [dbo].[User_table] (
[Name] VARCHAR (50) NOT NULL,
[Email] VARCHAR (50) NULL,
[Password] VARCHAR (50) NULL,
[Department] VARCHAR (50) NULL,
CONSTRAINT [PK_User_table] PRIMARY KEY CLUSTERED ([Name] ASC)
);
I need to display all information from application_table and some information such as email, department from user_table based on Name in application_table in gridview.
Is that any simple way to do it? I am quite new in asp net and c#, please guide me as i am a slow learner.
You could achieve that using a multi level grid.
Refer to this for details
ASP Multi Level Grid
BTW you should add a foreign key to your second table.
Related
I was working with VisualStudio 2017 C# and localdb2016 I created a table in server explorer's database desginer window with 10 columns added X rows.
I have already set copy if newer so rows wont get deleted everytime
but when i try to add a new column (11th column) all of my rows (even in other tables!) get deleted !
why ?
Here is my Table code generated from desginer .... :
CREATE TABLE [dbo].[CustomerTable] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[name] NVARCHAR (50) NULL,
[company] NVARCHAR (50) NULL,
[email] NVARCHAR (50) NULL,
[phone1] NVARCHAR (50) NULL,
[phone2] NVARCHAR (50) NULL,
[address] NVARCHAR (50) NULL,
[fax] NVARCHAR (50) NULL,
[phone3] NVARCHAR (50) NULL,
[date] DATETIME DEFAULT (getdate()) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
UPDATE 1 :
ALTER TABLE dbo.CustomerTable ADD column_b VARCHAR(20) NULL, column_c INT NULL ;
does that code remove old columns (b and c) if they exists?
is it a good idea to do that everytime app starts? (for upgrading purpose)
Try this Transact-SQL query
ALTER TABLE dbo.CustomerTable ADD column_b VARCHAR(20) NULL, column_c INT NULL ;
This query will add 2 columns in your table -> First, b (VARCHAR[20]) & Second, c (INT).
To read more,
ALTER TABLE (Transact-SQL)
The query will not remove any existing column because it is an alter query that means it alter the table as you mention. Adding existing column doesn't alter table. So, no changes.
Info
I want to 'upgrade' my ASP.NET Web Page project to an ASP.NET MVC project by just starting over, it are only a few pages and it would be good for learning.
The Issue
The current project holds a few users for authentication
ASP.NET made a few tables in my database:
Roles
Profiles
Users
...
But when I use the MVC Project I see it makes different tables for authentication:
AspNetUserLogins
AspNetUserRoles
AspNetUser
...
The Question
How do I migrate my users or how do I tell ASP.net to use the old tables?
Greetings you may want to start with Database first Approach in MVC-Core but its little bit different from what you have seen so far, let me explain the steps for adding your database:
Install Entity Framework:
Run Install-Package Microsoft.EntityFrameworkCore.SqlServer
To enable reverse engineering from an existing database we need to install a couple of other packages too.
Run Install-Package Microsoft.EntityFrameworkCore.Tools –Pre
Run Install-Package Microsoft.EntityFrameworkCore.Design
Run Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design
Now Reverse engineer your model:
Run the following command to create a model from the existing
database. If you receive an error stating the term
'Scaffold-DbContext' is not recognized as the name of a cmdlet, then
close and reopen Visual Studio.
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=DataBaseName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
The reverse engineer process created entity classes and a derived context based on the schema of the existing database. The entity classes are simple C# objects that represent the data you will be querying and saving. And now you can create a DbContext class and start using your database, so far you issue about database is over, and now i suggest you read about Identity In Asp.NET Core.
I came across this site thanks to this blog
And edited to fit my needs:
/****** Object: Table [dbo].[AspNetRoles] Script Date: 11/14/2013 1:56:03 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID('dbo.AspNetUserRoles', 'U') IS NOT NULL
DROP TABLE [dbo].[AspNetUserRoles]
GO
IF OBJECT_ID('dbo.AspNetUserLogins', 'U') IS NOT NULL
DROP TABLE [dbo].[AspNetUserLogins]
GO
IF OBJECT_ID('dbo.AspNetUserClaims', 'U') IS NOT NULL
DROP TABLE [dbo].[AspNetUserClaims]
GO
IF OBJECT_ID('dbo.AspNetRoles', 'U') IS NOT NULL
DROP TABLE [dbo].[AspNetRoles]
GO
IF OBJECT_ID('dbo.AspNetUsers', 'U') IS NOT NULL
DROP TABLE [dbo].[AspNetUsers]
GO
CREATE TABLE [dbo].[AspNetUsers] (
[AccessFailedCount] INT NOT NULL,
[Email] NVARCHAR (MAX) NULL,
[EmailConfirmed] BIT DEFAULT ((0)) NULL,
[Id] NVARCHAR (128) NOT NULL,
[LockoutEnabled] BIT DEFAULT ((0)) NULL,
[LockoutEndDateUtc] DATETIME2 (7) NULL,
[PasswordHash] NVARCHAR (MAX) NULL,
[PhoneNumber] NVARCHAR (MAX) NULL,
[PhoneNumberConfirmed] BIT DEFAULT ((0)) NULL,
[SecurityStamp] NVARCHAR (MAX) NULL,
[TwoFactorEnabled] BIT DEFAULT ((0)) NULL,
[UserName] NVARCHAR (MAX) NULL,
[CreateDate] DATETIME NULL,
[ConfirmationToken] NVARCHAR (128) NULL,
[IsConfirmed] BIT DEFAULT ((0)) NULL,
[LastPasswordFailureDate] DATETIME NULL,
[PasswordFailuresSinceLastSuccess] INT DEFAULT ((0)) NULL,
[PasswordChangedDate] DATETIME NULL,
[PasswordVerificationToken] NVARCHAR (128) NULL,
[PasswordVerificationTokenExpirationDate] DATETIME NULL,
CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
);
GO
CREATE TABLE [dbo].[AspNetRoles] (
[Id] NVARCHAR (128) NOT NULL,
[Name] NVARCHAR (MAX) NOT NULL,
CONSTRAINT [PK_dbo.AspNetRoles] PRIMARY KEY CLUSTERED ([Id] ASC)
);
GO
CREATE TABLE [dbo].[AspNetUserRoles] (
[UserId] NVARCHAR (128) NOT NULL,
[RoleId] NVARCHAR (128) NOT NULL,
CONSTRAINT [PK_dbo.AspNetUserRoles] PRIMARY KEY CLUSTERED ([UserId] ASC, [RoleId] ASC),
CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [dbo].[AspNetRoles] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_RoleId]
ON [dbo].[AspNetUserRoles]([RoleId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_UserId]
ON [dbo].[AspNetUserRoles]([UserId] ASC);
GO
CREATE TABLE [dbo].[AspNetUserLogins] (
[UserId] NVARCHAR (128) NOT NULL,
[LoginProvider] NVARCHAR (128) NOT NULL,
[ProviderKey] NVARCHAR (128) NOT NULL,
CONSTRAINT [PK_dbo.AspNetUserLogins] PRIMARY KEY CLUSTERED ([UserId] ASC, [LoginProvider] ASC, [ProviderKey] ASC),
CONSTRAINT [FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_UserId]
ON [dbo].[AspNetUserLogins]([UserId] ASC);
GO
CREATE TABLE [dbo].[AspNetUserClaims] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[ClaimType] NVARCHAR (MAX) NULL,
[ClaimValue] NVARCHAR (MAX) NULL,
[UserId] NVARCHAR (128) NOT NULL,
CONSTRAINT [PK_dbo.AspNetUserClaims] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_dbo.AspNetUserClaims_dbo.AspNetUsers_User_Id] FOREIGN KEY ([UserId]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_User_Id]
ON [dbo].[AspNetUserClaims]([UserId] ASC);
GO
INSERT INTO AspNetUsers(Id, UserName, PasswordHash, SecurityStamp,
CreateDate, ConfirmationToken, IsConfirmed, LastPasswordFailureDate, PasswordFailuresSinceLastSuccess,
PasswordChangedDate, PasswordVerificationToken, PasswordVerificationTokenExpirationDate,AccessFailedCount,LockoutEndDateUtc)
SELECT Users.UserId, Users.UserName, Memberships.Password,
Memberships.PasswordSalt, CreateDate,
null, 1, '1/1/1977', 0,
'1/1/2017', null, '1/1/2100',0,'1/1/1977'
FROM Users
LEFT OUTER JOIN Memberships ON Users.UserId = Memberships.UserId
GO
INSERT INTO AspNetRoles(Id, Name)
SELECT RoleId, RoleName
FROM Roles
GO
INSERT INTO AspNetUserRoles(UserId, RoleId)
SELECT UserId, RoleId
FROM UsersInRoles
GO
--INSERT INTO AspNetUserLogins(UserId, LoginProvider, ProviderKey)
--SELECT UserId, Provider, ProviderUserId
--FROM Memberships
--GO
Building a website with a back end SQL database. The database holds 571 houses with information about each. I want to add a search feature to the site. I have googled a lot the past couple of days but there are no good step by step guides available for what I am looking for. I want to search for a house by name and as I start typing I want an extender to display with results from the database and when I select one I want it to bring me to a separate aspx page for the specific house and display all the information for it. I have came here as a last resort because google has not been helpful so far.
Search
House Information page
Database
CREATE TABLE [dbo].[Houses] (
[Id] NCHAR (10) NOT NULL,
[Name] NVARCHAR (MAX) NULL,
[Townland] NVARCHAR (MAX) NULL,
[Near] NVARCHAR (MAX) NULL,
[Status] NVARCHAR (MAX) NULL,
[Built] NVARCHAR (MAX) NULL,
[Description] NVARCHAR (MAX) NULL,
[Families] NVARCHAR (MAX) NULL,
[Images] IMAGE NULL,
CONSTRAINT [PK_Houses] PRIMARY KEY CLUSTERED ([Id] ASC)
);
I think what you need is auto-suggetion text box.
Just googled for auto suggestion text box and tadda you will find lot of resources for that.One of auto suggestion example
I have an Excel sheet that is populated by HR employee with thousands of client records it looks like this one:
User Friendly Excel Sheet Example Screenshot
My client's SQL Server table schema looks like this
CREATE TABLE [dbo].[Clients] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (100) NOT NULL,
[Photo] VARCHAR (200) NOT NULL,
[PolicyID] INT NOT NULL,
[BirthDay] DATE NOT NULL,
[Gender] BIT NOT NULL,
[Title] NVARCHAR (100) NULL,
[Nationality] NVARCHAR (100) NOT NULL,
[Relationship] NVARCHAR (50) NOT NULL,
[ClassID] INT NOT NULL,
[SponsorID] INT NULL,
[HRID] INT NOT NULL,
[Active] BIT CONSTRAINT [DF_Clients_Active] DEFAULT ((1)) NOT NULL,
[StartingDate] DATE NOT NULL,
[EndingDate] DATE NOT NULL,
[AddingDate] DATETIME NOT NULL,
[Creator] INT NOT NULL,
[UniqueID] NVARCHAR (50) NULL,
[PassportNo] NVARCHAR (50) NULL,
CONSTRAINT [PK_Clients] PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [FK_Clients_Clients] FOREIGN KEY ([SponsorID]) REFERENCES [dbo].[Clients] ([ID]),
CONSTRAINT [FK_Clients_Employees] FOREIGN KEY ([HRID]) REFERENCES [dbo].[Employees] ([ID]),
CONSTRAINT [FK_Clients_Employees1] FOREIGN KEY ([Creator]) REFERENCES [dbo].[Employees] ([ID]),
CONSTRAINT [FK_Clients_Policy] FOREIGN KEY ([PolicyID]) REFERENCES [dbo].[Policy] ([ID]),
CONSTRAINT [FK_Clients_Classes] FOREIGN KEY ([ClassID]) REFERENCES [dbo].[Classes] ([ID])
);
What is the best approach to achieve such inserts?
I've tried using SqlBulkCopy but it doesn't allow any manipulation on the inserted rows.
I've tried also using SqlAdapter.Update(Datatable) but it failed since I've read the Excel sheet using ExcelDataReader then tried to add some columns like Creator and Adding Date at runtime and when I tried to run Adapter.Update(ModifiedDatatable) it throws an exception
Update requires a valid UpdateCommand when passed DataRow collection with modified rows
When I tried to use SqlBulkCopy to insert this Excel sheet it worked as expected
Excel Sheet with Foreign Keys Screenshot
But it's not right to force the end user to put some foreign keys in the Excel sheet before import.
Notice:
Sorry for uploading screenshots to Tinypic but I couldn't upload them here because of my Rep Points.
Thanks in advance
I would be creating an SSIS package in this scenario. SSIS can read from Excel, and you can get it to query the database for the extra information to build a valid dataset that will not violate the FK constraints.
I'm trying to update a table on Windows Azure but I don't know how.
This is my table. I need to update detallePeriodo from nvarchar(2) to nvarchar(4).
CREATE TABLE [dbo].[CLIENTE_LIDER]
(
[Cedula] NVARCHAR (12) NOT NULL,
[Nombre] NVARCHAR (100) NULL,
[TelCelular] NVARCHAR (9) NULL,
[TelCasa] NVARCHAR (9) NULL,
[Direccion] NVARCHAR (200) NULL,
[Distrito] INT NULL,
[Periosidad] INT NULL,
[DetallePeriodo] NVARCHAR (2) NULL,
[Monto_Total] FLOAT (53) DEFAULT ((0)) NULL,
[Monto_Actual] FLOAT (53) DEFAULT ((0)) NULL,
[Cuota] FLOAT (53) DEFAULT ((0)) NULL,
[Codigo] VARCHAR (12) NULL,
[Ruta] INT NULL,
[FechaIngresoSistema] DATE NULL,
PRIMARY KEY CLUSTERED ([Cedula] ASC),
FOREIGN KEY ([Distrito]) REFERENCES [dbo].[DISTRITOS] ([codigo]),
CONSTRAINT [FK_CLIENTE_LIDER_0] FOREIGN KEY ([Ruta]) REFERENCES [dbo].[RUTA] ([Codigo])
);
From the documentation, which applies to both SQL Server and the Azure SQL Database, it appears you just need a fairly standard ALTER statement.
ALTER TABLE dbo.CLIENTE_LIDER ALTER COLUMN DetallePeriodo NVARCHAR(4);