'Search a User By it's CNIC Number in .NET 6 MVC Application
I'm trying to build a .NET 6 Core MVC App using stored procedures in which when I enter any User's "CNIC Number" then all the records opposite to that user would be displayed.
On the front-end view, the user enters his/her CNIC Number, and all the rows that are related to that CNIC Number are displayed to the user.
A user can have more than 1 debit card registered against his/her CNIC Number in the database. So all the rows that are present opposite to his/her CNIC number would be displayed to the user.
Database Table:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserInfo](
[ID] [int] IDENTITY(1,1) NOT NULL,
[CNIC] [nvarchar](50) NULL,
[UserName] [text] NULL,
[CardNumber] [nvarchar](50) NULL,
[CardStatus] [text] NULL,
CONSTRAINT [PK_UserInfo] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Procedure:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: CyberSoft
-- Description: Get User Info By CNIC
-- =============================================
ALTER PROCEDURE [dbo].[ABL_GetUserByCNIC]
(
@CNIC nvarchar(50) = ''
)
AS
BEGIN
SELECT * FROM UserInfo
WHERE CNIC= @CNIC
END
UsersContoller:
using ABL_USER_DebitCard_Info.Context;
using ABL_USER_DebitCard_Info.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace ABL_USER_DebitCard_Info.Controllers
{
public class UsersController : Controller
{
DebitCard_DAL dbcontext = new DebitCard_DAL();
public ActionResult Details(string CNIC)
{
if(CNIC == null)
{
return NotFound();
}
else
{
Users user = dbcontext.GetUserByCNIC(CNIC);
if(user == null)
{
return NotFound();
}
else
{
return View(user);
}
}
}
}
}
Users Model:
namespace ABL_USER_DebitCard_Info.Models
{
public class Users
{
public int Id { get; set; }
public string CNIC { get; set; }
public string UserName { get; set; }
public string CardNumber { get; set; }
public string CardStatus { get; set; }
}
}
DBContext
using ABL_USER_DebitCard_Info.Models;
using System.Data;
using System.Data.SqlClient;
namespace ABL_USER_DebitCard_Info.Context
{
public class DebitCard_DAL
{
string connectionString = "Data Source = (localdb)\\MSSQLLocalDB; Initial Catalog = ABL_DebitCard_User_Info_DB";
public Users GetUserByCNIC(string CNIC)
{
var user = new Users();
using (SqlConnection conn = new SqlConnection())
{
SqlCommand cmd = new SqlCommand("ABL_GetUserByCNIC", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CNIC", CNIC);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
user.Id = Convert.ToInt32(reader["ID"].ToString);
user.CNIC = reader["CNIC"].ToString();
user.UserName = reader["UserName"].ToString();
user.CardNumber = reader["CardNumber"].ToString();
user.CardStatus = reader["CardStatus"].ToString();
}
conn.Close();
}
return user;
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
