Source Code:
Database Table :
USE [dbLearning]
GO
/****** Object: Table [dbo].[dbFriends] Script Date: 10-05-2021 23:52:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[dbFriends](
[FriendID] [int] IDENTITY(1,1) NOT NULL,
[FriendName] [varchar](100) NULL,
[Mobile] [varchar](50) NULL,
[CityID] [int] NULL,
CONSTRAINT [PK_dbFriends] PRIMARY KEY CLUSTERED
(
[FriendID] 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]
GO
USE [dbLearning]
GO
/****** Object: Table [dbo].[tblCities] Script Date: 11-05-2021 00:04:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblCities](
[CityID] [int] IDENTITY(1,1) NOT NULL,
[CityName] [varchar](50) NULL,
CONSTRAINT [PK_tblCities] PRIMARY KEY CLUSTERED
(
[CityID] 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]
GO
Code of List-of-Friends.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="list-of-friends.aspx.cs" Inherits="list_of_friends" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvFriend" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="FriendName" HeaderText="Friend Name" />
<asp:BoundField DataField="Mobile" HeaderText ="Mobile Number" />
<asp:TemplateField HeaderText ="Friend City">
<ItemTemplate>
<%# GetCityNameByCityID(Convert.ToString(Eval("CityID"))) %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Code of List-of-Friends.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class list_of_friends : System.Web.UI.Page
{
FriendDataClassesDataContext db = new FriendDataClassesDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var friendlist = (from a in db.dbFriends select a).ToList();
gvFriend.DataSource = friendlist;
gvFriend.DataBind();
}
}
public string GetCityNameByCityID(string _CityID)
{
string CityName = string.Empty;
var cityDetail = (from a in db.tblCities
where a.CityID == Convert.ToInt32(_CityID)
select a).FirstOrDefault();
if(cityDetail != null)
{
CityName = cityDetail.CityName;
}
return CityName;
}
}
No comments:
Post a Comment