http://rrlibrary.dyndns.org/photocrawler.aspx
Posted using ShareThis
Sunday, November 8, 2009
Tuesday, September 29, 2009
Creating n-tier application
Create Abstract Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace RRL
{
public abstract class Abstract
{
#region private variables
DataTable genrelist = null;
DataTable websiteslist = null;
DataTable youtubelist = null;
DataTable decadelist = null;
DataTable charitylist = null;
DataTable foundationlist = null;
DataTable akalist = null;
DataTable addresslist = null;
DataTable timelinelist = null;
DataTable editorslist = null;
DataTable venuelist = null;
DataTable creatorlist = null;
DataTable designerlist = null;
DataTable manufacturerlist = null;
DataTable ownerlist = null;
DataTable personbandcomp = null;
DataTable recordedlabellist = null;
DataTable recordingstudioslist = null;
DataTable authorlist = null;
DataTable engineerlist=null;
DataTable producerlist = null;
DataTable publisherlist = null;
DataTable samplesongslist = null;
DataTable bookformats = null;
DataTable mediaformats = null;
DataTable videogameversionlist = null;
DataTable rtvendroslist = null;
DataTable producerslist = null;
DataTable tourmanagerslist = null;
DataTable founderlist = null;
DataTable memberslist = null;
DataTable accesslevelslist = null;
DataTable objectslist = null;
DataTable permissionslist = null;
DataTable statuslist = null;
String blogurl, twitterurl, facebookurl, myspaceurl, archiveurl, hi5url;
#endregion
public DataTable VIDEOGAMEVERSIONLIST
{
get { return videogameversionlist; }
set { videogameversionlist = value; }
}
public DataTable OBJECTSLIST
{
get { return objectslist; }
set { objectslist = value; }
}
public DataTable PERMISSIONSLIST
{
get { return permissionslist; }
set { permissionslist = value; }
}
public DataTable STATUSLIST
{
get { return statuslist; }
set { statuslist = value; }
}
public DataTable ACCESSLEVELSLIST
{
get { return accesslevelslist; }
set { accesslevelslist = value; }
}
public DataTable MEMBERSLIST
{
get { return memberslist; }
set { memberslist = value; }
}
public DataTable PRODUCERSLIST
{
get { return producerslist; }
set { producerslist = value; }
}
public DataTable TOURMANAGERSLIST
{
get { return tourmanagerslist; }
set { tourmanagerslist = value; }
}
public DataTable RTVENDORSLIST
{
get { return rtvendroslist; }
set { rtvendroslist = value; }
}
public DataTable BOOKFORMATS
{
get { return bookformats; }
set { bookformats = value; }
}
public DataTable MEDIAFORMATS
{
get { return mediaformats; }
set { mediaformats = value; }
}
public DataTable PersonBandComp
{
get { return personbandcomp; }
set { personbandcomp = value; }
}
public DataTable ENGINEERLIST
{
get { return engineerlist; }
set { engineerlist = value; }
}
public DataTable PRODUCERLIST
{
get { return producerlist; }
set { producerlist = value; }
}
public DataTable SAMPLESONGSLIST
{
get { return samplesongslist; }
set { samplesongslist = value; }
}
public DataTable RECORDINGSTUDIOSLIST
{
get { return recordingstudioslist; }
set { recordingstudioslist = value; }
}
public DataTable RECORDEDLABELLIST
{
get { return recordedlabellist; }
set { recordedlabellist = value; }
}
public DataTable OWNERLIST
{
get { return ownerlist; }
set { ownerlist = value; }
}
public DataTable VENUELIST
{
get { return venuelist; }
set { venuelist = value; }
}
public DataTable CREATORLIST
{
get { return creatorlist; }
set { creatorlist = value; }
}
public DataTable DESIGNERLIST
{
get { return designerlist; }
set { designerlist = value; }
}
public DataTable GENRELIST
{
get { return genrelist; }
set { genrelist = value; }
}
public DataTable WEBSITESLIST
{
get { return websiteslist; }
set { websiteslist = value; }
}
public DataTable YOUTUBELIST
{
get { return youtubelist; }
set { youtubelist = value; }
}
public DataTable DECADELIST
{
get { return decadelist; }
set { decadelist = value; }
}
public DataTable PUBLISHERLIST
{
get { return publisherlist; }
set { publisherlist = value; }
}
public DataTable CHARITYLIST
{
get { return charitylist; }
set { charitylist = value; }
}
public DataTable FOUNDATIONLIST
{
get { return foundationlist; }
set { foundationlist = value; }
}
public DataTable AKALIST
{
get { return akalist; }
set { akalist = value; }
}
public DataTable ADDRESSLIST
{
get { return addresslist; }
set { addresslist = value; }
}
public DataTable AUTHORLIST
{
get { return authorlist; }
set { authorlist = value; }
}
public DataTable TIMELINELIST
{
get { return timelinelist; }
set { timelinelist = value; }
}
public DataTable FOUNDERLIST
{
get { return founderlist; }
set { founderlist = value; }
}
public String BlogUrl
{
get { return blogurl; }
set { blogurl = value; }
}
public String TwitterUrl
{
get { return twitterurl; }
set { twitterurl = value; }
}
public String FaceBookUrl
{
get { return facebookurl; }
set { facebookurl = value; }
}
public String MySpaceUrl
{
get { return myspaceurl; }
set { myspaceurl = value; }
}
public String Hi5Url
{
get { return hi5url; }
set { hi5url = value; }
}
public String ArchiveUrl
{
get { return archiveurl; }
set { archiveurl = value; }
}
public DataTable AuthorList
{
get { return authorlist; }
set { authorlist = value; }
}
public DataTable EditorList
{
get { return editorslist; }
set { editorslist = value; }
}
}
}
-----------------------
Create Abstract Data Access
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DataAccess
{
public abstract class AbstractDA
{
#region privatevariables
SqlConnection con = new SqlConnection();
ConnectionDA varCon = new ConnectionDA();
Exception varEx = new Exception();
#endregion
public AbstractDA()
{
con.ConnectionString = varCon.GetConnection;
}
public DataTable GetCountryList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from countryentitydetail", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetAuthorList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from entity_authors", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetEngineerList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from entity_engineers", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetProducerList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from entity_Producers", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetSampleSongs()
{
SqlDataAdapter da = new SqlDataAdapter("select * from song_samples", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetArtifactList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from artifacttypes", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable MEDIAFORMATS()
{
SqlDataAdapter da = new SqlDataAdapter("select * from entity_mediaformats", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable RECORDLABEL()
{
SqlDataAdapter da = new SqlDataAdapter("select * from companyentitydetail where companytypeid=1", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable RECORDINGSTUDIOSLIST()
{
SqlDataAdapter da = new SqlDataAdapter("select * from companyentitydetail where companytypeid=4", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetStateList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from stateentitydetail", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetStatesofCountry(string countryid)
{
SqlDataAdapter da = new SqlDataAdapter("select * from stateentitydetail where countryentityid=@countryentityid", con);
DataTable dt = new DataTable();
da.SelectCommand.Parameters.AddWithValue("@countryentityid", countryid);
da.Fill(dt);
return dt;
}
public string GetCountryofState(string stateid)
{
SqlCommand cmd = new SqlCommand("select CountryEntityId from stateentitydetail where entityid=@stateid", con);
cmd.Parameters.AddWithValue("@stateid", stateid);
if(con.State==ConnectionState.Closed)
con.Open();
return cmd.ExecuteScalar().ToString();
if(con.State==ConnectionState.Open)
con.Close();
}
public string GetStateofCity(string cityid)
{
SqlCommand cmd = new SqlCommand("select stateEntityId from cityentitydetail where entityid=@cityid", con);
cmd.Parameters.AddWithValue("@cityid", cityid);
if (con.State == ConnectionState.Closed)
con.Open();
return cmd.ExecuteScalar().ToString();
if (con.State == ConnectionState.Open)
con.Close();
}
public DataTable GetCitiesofState(string stateid)
{
SqlDataAdapter da = new SqlDataAdapter("select * from cityentitydetail where stateentityid=@stateentityid", con);
DataTable dt = new DataTable();
da.SelectCommand.Parameters.AddWithValue("@stateentityid", stateid);
da.Fill(dt);
return dt;
}
public DataTable GetCityList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Cityentitydetail", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetNeighborhoodList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Neighborhoodentitydetail", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetSongList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Songentitydetail", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetDecadesList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Decades", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public string GetASINObject(string ENTITYID)
{
string ASIN = string.Empty;
//SqlDataAdapter da = new SqlDataAdapter("select commonname,friendlyname,id from Entity,entity_authors,entitytype where entity.id=entity_authors.entityid and entity.entitytypeid=entitytype.id and entity_authors.entityid=@eid", con);
SqlCommand cmd = new SqlCommand("select ASIN from Amazon_ASIN where entityid=@id", con);
cmd.Parameters.AddWithValue("@id", ENTITYID);
try
{if(con.State==ConnectionState.Closed)
con.Open();
ASIN = cmd.ExecuteScalar().ToString();
}
catch
{
//No ASIN Available
}
finally
{if(con.State==ConnectionState.Open)
con.Close();
}
return ASIN;
}
public DataTable GetAuthorsListofObject(string ENTITYID)
{
//SqlDataAdapter da = new SqlDataAdapter("select commonname,friendlyname,id from Entity,entity_authors,entitytype where entity.id=entity_authors.entityid and entity.entitytypeid=entitytype.id and entity_authors.entityid=@eid", con);
SqlDataAdapter da = new SqlDataAdapter("select Person_Band_CompId as id,(select commonname from entity where id=a.Person_Band_CompId) as Name from Entity_Authors a where entityid=@id", con);
da.SelectCommand.Parameters.AddWithValue("@id", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetEngineersListofObject(string ENTITYID)
{
//SqlDataAdapter da = new SqlDataAdapter("select commonname,friendlyname,id from Entity,entity_authors,entitytype where entity.id=entity_authors.entityid and entity.entitytypeid=entitytype.id and entity_authors.entityid=@eid", con);
SqlDataAdapter da = new SqlDataAdapter("select PersonEntityId as id,(select commonname from entity where id=a.PersonEntityId) as Name from Entity_Engineers a where entityid=@id", con);
da.SelectCommand.Parameters.AddWithValue("@id", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetProducersListofObject(string ENTITYID)
{
//SqlDataAdapter da = new SqlDataAdapter("select commonname,friendlyname,id from Entity,entity_authors,entitytype where entity.id=entity_authors.entityid and entity.entitytypeid=entitytype.id and entity_authors.entityid=@eid", con);
SqlDataAdapter da = new SqlDataAdapter("select Person_BandEntityId as id,(select commonname from entity where id=a.Person_BandEntityId) as Name from Entity_producers a where entityid=@id", con);
da.SelectCommand.Parameters.AddWithValue("@id", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetSong_SamplesListofObject(string ENTITYID)
{
//SqlDataAdapter da = new SqlDataAdapter("select commonname,friendlyname,id from Entity,entity_authors,entitytype where entity.id=entity_authors.entityid and entity.entitytypeid=entitytype.id and entity_authors.entityid=@eid", con);
SqlDataAdapter da = new SqlDataAdapter("select samplesongid as id,(select commonname from entity where id=a.SampleSongId) as Name from Song_Samples a where songid=@id", con);
da.SelectCommand.Parameters.AddWithValue("@id", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetEditorsListofObject(string ENTITYID)
{
//SqlDataAdapter da = new SqlDataAdapter("select commonname,friendlyname,id from Entity,entity_authors,entitytype where entity.id=entity_authors.entityid and entity.entitytypeid=entitytype.id and entity_authors.entityid=@eid", con);
SqlDataAdapter da = new SqlDataAdapter("select Band_PersonId as id,(select commonname from entity where id=e.Band_PersonId) as Name from Entity_Editor e where entityid=@id", con);
da.SelectCommand.Parameters.AddWithValue("@id", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetBookFormatsList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from BookFormats", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetBookformatsListofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select * from book_bookformats where bookid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetMediaFormatsList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from MediaFormats", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetMediaFormatListofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select * from entity_mediaformats where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetAudioFormatsList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Aud_Rec_Formats", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetRighty_LeftyList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from RightyLefty", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetRatingsList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Ratings", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetCompanyType()
{
SqlDataAdapter da = new SqlDataAdapter("select * from companytype", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetPresident()
{
SqlDataAdapter da = new SqlDataAdapter("select * from company", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
//Access Levels
public DataTable GetAccessLevels()
{
SqlDataAdapter da = new SqlDataAdapter("select * from accesslevels", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
//Objects
public DataTable GetUserObjects()
{
SqlDataAdapter da = new SqlDataAdapter("select * from entitytype", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
//Permissions
public DataTable GetUserPermissions()
{
SqlDataAdapter da = new SqlDataAdapter("select * from permissionlevels", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
//Status
public DataTable GetUserStatus()
{
SqlDataAdapter da = new SqlDataAdapter("select * from entity_state", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetAllLanguages()
{
SqlDataAdapter da = new SqlDataAdapter("select * from mb_language", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetAllSoundTypes()
{
SqlDataAdapter da = new SqlDataAdapter("select * from soundtypes", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetPublisherListofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select Band_or_Person_CompId as id from entity_people_band_comp where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetPublishersList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from companyentitydetail where companytypeid=18", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetGENRELISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select genreid as id from entity_genres where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetWEBSITESLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select url,urlname,urldescription,urltypeid,isofficialurl from entity_urls where entityid=@eid and urltypeid=1 ", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetDecadesListofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select decadeid as id from entity_decades where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetYOUTUBELISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select youtubeid as id,(select youtubesrc from youtube where id=e.youtubeid ) as name from entity_youtubes e where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetTIMELINELISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select timelineid as id,(select timelineurl from timelines where id=e.timelineid ) as name from entity_timelines e where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetCHARITYLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select entityid,CompanyName from companyentitydetail where entityid in(select compid from entity_charity where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetFOUNDATIONLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select entityid,CompanyName from companyentitydetail where entityid in(select compid from entity_foundation where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetVENUELISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select entityid,CompanyName from companyentitydetail where entityid in(select compid from entity_venues where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetCREATORLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select id,CommonName as name from entity where id in(select creatorid from entity_creators where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetTOURMANAGERSLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select id,CommonName as name from entity where id in(select managerid from tour_managers where tourid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetPRODUCERSLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select id,CommonName as name from entity where id in(select person_bandentityid from entity_producers where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetDESIGNERLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select id,CommonName as name from entity where id in(select designerid from entity_designer where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetOWNERLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select id,CommonName as name from entity where id in(select Person_Band_CompId from entity_Owner where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetPeople_Band_CompListofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select id,CommonName as name from entity where id in(select Band_or_Person_CompId from entity_people_band_comp where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetAKALISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select AkaText,IsDefault from Entity_Akas where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetADDRESSLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select AddressTypeId,(select AddresstypeName from AddressType where id=e.AddressTypeId) as TypeName,Address1,Address2,Address3,PlaceId,(select commonname from entity where id=e.PlaceId) as PlaceName,postalCode from Entity_Addresses e where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public string GetBlogUrlofObject(string ENTITYID)
{
string BlogUrl = string.Empty;
try
{
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd_blog = new SqlCommand("select url from entity_urls where entityid=@eid and urltypeid=2 ", con);
cmd_blog.Parameters.AddWithValue("@eid", ENTITYID);
SqlDataReader dr_blog = cmd_blog.ExecuteReader();
if (dr_blog.HasRows)
{
while (dr_blog.Read())
BlogUrl = dr_blog[0].ToString();
}
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return BlogUrl;
}
public string GetTwitterUrlofObject(string ENTITYID)
{
string TwitterUrl = string.Empty;
try
{
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd_twitter = new SqlCommand("select url from entity_urls where entityid=@eid and urltypeid=3 ", con);
cmd_twitter.Parameters.AddWithValue("@eid", ENTITYID);
SqlDataReader dr_twitter = cmd_twitter.ExecuteReader();
if (dr_twitter.HasRows)
{
while (dr_twitter.Read())
TwitterUrl = dr_twitter[0].ToString();
}
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return TwitterUrl;
}
public string GetFacebookUrlofObject(string ENTITYID)
{
string FaceBUrl = string.Empty;
try
{
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd_facebook = new SqlCommand("select url from entity_urls where entityid=@eid and urltypeid=4 ", con);
cmd_facebook.Parameters.AddWithValue("@eid", ENTITYID);
SqlDataReader dr_facebook = cmd_facebook.ExecuteReader();
if (dr_facebook.HasRows)
{
while (dr_facebook.Read())
FaceBUrl = dr_facebook[0].ToString();
}
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return FaceBUrl;
}
public string GetHi5UrlofObject(string ENTITYID)
{
string Hi5Url = string.Empty;
try
{
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd_hi5 = new SqlCommand("select url from entity_urls where entityid=@eid and urltypeid=5 ", con);
cmd_hi5.Parameters.AddWithValue("@eid", ENTITYID);
SqlDataReader dr_Hi5 = cmd_hi5.ExecuteReader();
if (dr_Hi5.HasRows)
{
while (dr_Hi5.Read())
{
Hi5Url= dr_Hi5[0].ToString();
}
}
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return Hi5Url;
}
public string GetArchiveUrlofObject(string ENTITYID)
{
string ArchiveUrl = string.Empty;
try
{
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd_Archive = new SqlCommand("select url from entity_urls where entityid=@eid and urltypeid=7 ", con);
cmd_Archive.Parameters.AddWithValue("@eid", ENTITYID);
SqlDataReader dr_Archive = cmd_Archive.ExecuteReader();
if (dr_Archive.HasRows)
{
while (dr_Archive.Read())
ArchiveUrl = dr_Archive[0].ToString();
}
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return ArchiveUrl;
}
public string GetMySpaceUrlofObject(string ENTITYID)
{
string myspaceurl=string.Empty;
try
{
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd_myspace = new SqlCommand("select url from entity_urls where entityid=@eid and urltypeid=7 ", con);
cmd_myspace.Parameters.AddWithValue("@eid", ENTITYID);
SqlDataReader dr_myspace = cmd_myspace.ExecuteReader();
if (dr_myspace.HasRows)
{
while(dr_myspace.Read())
myspaceurl =dr_myspace[0].ToString();
}
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return myspaceurl;
}
}
}
--------------------------------------
Step 1: First Create Object File means with database fields
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RRL
{
public class City : Abstract
{
string entityid;
string entitytypeid;
string citytext;
string stateentityid;
string abbreviation;
string archive;
string decades;
string tagline;
string vanityurl;
bool editorpick;
string tags;
public string ENTITYID
{
get
{
return entityid;
}
set
{
entityid = value;
}
}
public string STATEENTITYID
{
get
{
return stateentityid;
}
set
{
stateentityid = value;
}
}
public string DECADES
{
get
{
return decades;
}
set
{
decades = value;
}
}
public string ENTITYTYPEID
{
get
{
return entitytypeid;
}
set
{
entitytypeid = value;
}
}
public string CITYTEXT
{
get
{
return citytext;
}
set
{
citytext = value;
}
}
public string ABBREVIATION
{
get
{
return abbreviation;
}
set
{
abbreviation = value;
}
}
public string ARCHIVE
{
get
{
return archive;
}
set
{
archive = value;
}
}
public string TAGLINE
{
get
{
return tagline;
}
set
{
tagline = value;
}
}
public string VANITYURL
{
get
{
return vanityurl;
}
set
{
vanityurl = value;
}
}
public bool EDITORPICK
{
get
{
return editorpick;
}
set
{
editorpick = value;
}
}
public string TAGS
{
get
{
return tags;
}
set
{
tags = value;
}
}
}
}
---------------------------------
Step 2: Create Data Access Layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RRL;
using System.Data.SqlClient;
using System.Data;
namespace DataAccess
{
public class CityDA :AbstractDA
{
City vCity= null;
SqlConnection con = new SqlConnection();
ConnectionDA objCon = new ConnectionDA();
SqlCommand cmd_entity;
#region ICityDA Members
public CityDA(City vnewCity)
{
vCity = vnewCity;
con.ConnectionString = objCon.GetConnection;
}
public CityDA()
{
con.ConnectionString = objCon.GetConnection;
}
public bool CreateNewCity()
{
SqlTransaction Tran = null;
try
{
con.Open();
Guid newguid = Guid.NewGuid();
//First Inserting values in Entity Tables
SqlCommand cmd_entity = new SqlCommand("insert into entity(id,entitytypeid,entitystateid,commonname,createddate,createdby,ownerid,IsEditorsPick,VanityUrl,Tags) values(@id,@entitytypeid,@entitystateid,@commonname,@createddate,@createdby,@ownerid,@IsEditorsPick,@VanityUrl,@Tags)", con);
cmd_entity.Parameters.AddWithValue("@id", newguid);
cmd_entity.Parameters.AddWithValue("@entitytypeid", vCity.ENTITYTYPEID);
cmd_entity.Parameters.AddWithValue("@entitystateid", 1);
cmd_entity.Parameters.AddWithValue("@commonname",vCity.CITYTEXT.Replace("'", "''"));
cmd_entity.Parameters.AddWithValue("@createddate", DateTime.Now.ToString());
cmd_entity.Parameters.AddWithValue("@createdby", 1);
cmd_entity.Parameters.AddWithValue("@ownerid", 1);
cmd_entity.Parameters.AddWithValue("@IsEditorsPick", vCity.EDITORPICK);
cmd_entity.Parameters.AddWithValue("@VanityUrl", vCity.VANITYURL);
cmd_entity.Parameters.AddWithValue("@Tags", vCity.TAGS);
Tran = con.BeginTransaction();
cmd_entity.Transaction = Tran;
cmd_entity.ExecuteNonQuery();
// Tran.Save("CreateEntity");
//Inserting Unique Values
cmd_entity.CommandText = "insert into CityEntityDetail values(@entityid,@StateEntityId,@citytext,@Abbreviation,@archive,@TagLine)";
cmd_entity.Parameters.AddWithValue("@entityid", newguid);
if (vCity.STATEENTITYID != "")
cmd_entity.Parameters.AddWithValue("@StateEntityId", new Guid(vCity.STATEENTITYID));
else
cmd_entity.Parameters.AddWithValue("@StateEntityId", Guid.Empty);
cmd_entity.Parameters.AddWithValue("@CityText", vCity.CITYTEXT.Replace("'", "''"));
cmd_entity.Parameters.AddWithValue("@Abbreviation", vCity.ABBREVIATION.Replace("'", "''"));
cmd_entity.Parameters.AddWithValue("@archive", vCity.ARCHIVE.Replace("'", "''"));
cmd_entity.Parameters.AddWithValue("@TagLine", vCity.TAGLINE.Replace("'", "''"));
cmd_entity.ExecuteNonQuery();
//Tran.Save("CreatePerson");
//cmd_entity.Dispose();
//Inserting Relationships
//Websites
InsertWebsites(vCity.WEBSITESLIST, newguid, cmd_entity);
//AKA List
InsertAKAs(vCity.AKALIST, newguid, cmd_entity);
//Genres
InsertGenres(vCity.GENRELIST, newguid, cmd_entity);
//YOUTUBELIST
InsertYoutubes(vCity.YOUTUBELIST, newguid, cmd_entity);
//TIMELINELIST
InsertTimeLines(vCity.TIMELINELIST, newguid, cmd_entity);
//Decades
InsertDecades(vCity.DECADELIST, newguid, cmd_entity);
Tran.Commit();
return true;
}
catch
{
Tran.Rollback();
return false;
}
finally
{
con.Close();
}
}
public void InsertAKAs(DataTable AKALIST, Guid guid, SqlCommand cmd_entity)
{
foreach (DataRow dr in AKALIST.Rows)
{
cmd_entity.CommandText = "insert into entity_AKAs values('" + guid + "','" + dr["AKAText"].ToString().Replace("'", "''") + "','" + bool.Parse(dr["IsDefault"].ToString()) + "')";
cmd_entity.ExecuteNonQuery();
}
}
public bool UpdateCity()
{
SqlTransaction Tran = null;
try
{
con.Open();
Guid oldguid = new Guid(vCity.ENTITYID);
//First Inserting values in Entity Tables
SqlCommand cmd_entity = new SqlCommand("update entity set modifieddate=@modifieddate,modifiedby=@modifiedby,ownerid=@ownerid,IsEditorsPick=@IsEditorsPick,VanityUrl=@VanityUrl,Tags=@Tags where id=@id", con);
cmd_entity.Parameters.AddWithValue("@id", oldguid);
cmd_entity.Parameters.AddWithValue("@modifieddate", DateTime.Now.ToString());
cmd_entity.Parameters.AddWithValue("@modifiedby", 1);
cmd_entity.Parameters.AddWithValue("@ownerid", 1);
cmd_entity.Parameters.AddWithValue("@IsEditorsPick", vCity.EDITORPICK);
cmd_entity.Parameters.AddWithValue("@VanityUrl", vCity.VANITYURL);
cmd_entity.Parameters.AddWithValue("@Tags", vCity.TAGS);
Tran = con.BeginTransaction();
cmd_entity.Transaction = Tran;
cmd_entity.ExecuteNonQuery();
//Tran.Save("CreateEntity");
//Inserting Unique Values
cmd_entity.CommandText = "update CityEntityDetail set entityid=@entityid,stateentityid=@stateentityid,citytext=@citytext,abbreviation=@abbreviation,archive=@archive,TagLine=@TagLine where entityid=@entityid";
//SqlCommand cmd_person = new SqlCommand("insert into personentitydetail values(@entityid,@stagename,@firstname,@middlename,@lastname,@prefix,@postfix,@birthday,@birthmonth,@birthyear,@placeofbirth,@deathday,@deathmonth,@deathyear,@placeofdeath,@circumstancesofdeath,@rightylefty)", con);
cmd_entity.Parameters.AddWithValue("@entityid", oldguid);
cmd_entity.Parameters.AddWithValue("@stateentityid", vCity.STATEENTITYID);
cmd_entity.Parameters.AddWithValue("@citytext", vCity.CITYTEXT);
cmd_entity.Parameters.AddWithValue("@abbreviation", vCity.ABBREVIATION);
cmd_entity.Parameters.AddWithValue("@archive", vCity.ARCHIVE);
cmd_entity.Parameters.AddWithValue("@TagLine", vCity.TAGLINE);
cmd_entity.ExecuteNonQuery();
//Tran.Save("CreatePerson");
//cmd_entity.Dispose();
//Inserting Relationships
//Websites
cmd_entity.CommandText = "delete from entity_urls where entityid='" + oldguid + "'";
cmd_entity.ExecuteNonQuery();
InsertWebsites(vCity.WEBSITESLIST, oldguid, cmd_entity);
//AKA List
cmd_entity.CommandText = "delete from entity_AKAs where entityid='" + oldguid + "'";
cmd_entity.ExecuteNonQuery();
InsertAKAs(vCity.AKALIST, oldguid, cmd_entity);
//Genres
cmd_entity.CommandText = "delete from entity_Genres where entityid='" + oldguid + "'";
cmd_entity.ExecuteNonQuery();
InsertGenres(vCity.GENRELIST, oldguid, cmd_entity);
//YOUTUBELIST
cmd_entity.CommandText = "delete from entity_Youtubes where entityid='" + oldguid + "'";
cmd_entity.ExecuteNonQuery();
InsertYoutubes(vCity.YOUTUBELIST, oldguid, cmd_entity);
//TIMELINELIST
cmd_entity.CommandText = "delete from entity_TimeLines where entityid='" + oldguid + "'";
cmd_entity.ExecuteNonQuery();
InsertTimeLines(vCity.TIMELINELIST, oldguid, cmd_entity);
//DecadLIST
cmd_entity.CommandText = "delete from entity_decades where entityid='" + oldguid + "'";
cmd_entity.ExecuteNonQuery();
InsertDecades(vCity.DECADELIST, oldguid, cmd_entity);
Tran.Commit();
return true;
}
catch
{
Tran.Rollback();
return false;
}
finally
{
con.Close();
}
}
public bool DeleteCity()
{
return false;
}
public City GetCityDetails()
{
SqlCommand cmd_entity = new SqlCommand("select * from entity where id=@id", con);
cmd_entity.Parameters.AddWithValue("@id", vCity.ENTITYID);
con.Open();
SqlDataReader dr_entity = cmd_entity.ExecuteReader();
if (dr_entity.HasRows)
{
while (dr_entity.Read())
{
if (dr_entity["IsEditorsPick"].ToString() != "")
vCity.EDITORPICK = bool.Parse(dr_entity["IsEditorsPick"].ToString());
else
vCity.EDITORPICK = false;
vCity.VANITYURL = dr_entity["VanityUrl"].ToString();
vCity.TAGS = dr_entity["Tags"].ToString();
}
}
dr_entity.Close();
SqlCommand cmd = new SqlCommand("select * from cityentitydetail where entityid=@eid", con);
cmd.Parameters.AddWithValue("@eid", vCity.ENTITYID);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
vCity.ENTITYID = dr["entityid"].ToString();
vCity.STATEENTITYID = dr["stateentityid"].ToString();
vCity.CITYTEXT = dr["citytext"].ToString();
vCity.ABBREVIATION = dr["Abbreviation"].ToString();
vCity.ARCHIVE = dr["Archive"].ToString();
vCity.TAGLINE = dr["tagline"].ToString();
}
}
dr.Close();
con.Close();
vCity.WEBSITESLIST = GetWEBSITESLISTofObject(vCity.ENTITYID);
vCity.GENRELIST = GetGENRELISTofObject(vCity.ENTITYID);
vCity.YOUTUBELIST = GetYOUTUBELISTofObject(vCity.ENTITYID);
vCity.TIMELINELIST = GetTIMELINELISTofObject(vCity.ENTITYID);
vCity.AKALIST = GetAKALISTofObject(vCity.ENTITYID);
vCity.ArchiveUrl = GetArchiveUrlofObject(vCity.ENTITYID);
vCity.DECADELIST = GetDecadesListofObject(vCity.ENTITYID);
return vCity;
}
public void InsertWebsites(DataTable WEBSITESLIST, Guid guid, SqlCommand cmd_entity)
{
foreach (DataRow dr in WEBSITESLIST.Rows)
{
cmd_entity.CommandText = "insert into entity_urls values('" + guid + "','" + dr["Url"].ToString().Replace("'", "''") + "','" + dr["UrlName"].ToString().Replace("'", "''") + "','" + dr["UrlDescription"].ToString().Replace("'", "''") + "'," + int.Parse(dr["UrlTypeId"].ToString()) + ",'" + bool.Parse(dr["IsOfficialUrl"].ToString()) + "')";
cmd_entity.ExecuteNonQuery();
}
}
public void InsertGenres(DataTable GenresList, Guid guid, SqlCommand cmd_entity)
{
foreach (DataRow dr in GenresList.Rows)
{
cmd_entity.CommandText = "insert into entity_Genres values('" + guid + "','" + dr["Id"].ToString() + "')";
cmd_entity.ExecuteNonQuery();
}
}
public void InsertDecades(DataTable DECADELIST, Guid guid, SqlCommand cmd_entity)
{
foreach (DataRow dr in DECADELIST.Rows)
{
cmd_entity.CommandText = "insert into entity_Decades values('" + guid + "','" + dr["Id"].ToString() + "')";
cmd_entity.ExecuteNonQuery();
}
}
public void InsertYoutubes(DataTable YOUTUBELIST, Guid guid, SqlCommand cmd_entity)
{
foreach (DataRow dr in YOUTUBELIST.Rows)
{
cmd_entity.CommandText = "insert into entity_Youtubes values('" + guid + "','" + dr["Id"].ToString() + "')";
cmd_entity.ExecuteNonQuery();
}
}
public void InsertTimeLines(DataTable TIMELINELIST, Guid guid, SqlCommand cmd_entity)
{
foreach (DataRow dr in TIMELINELIST.Rows)
{
cmd_entity.CommandText = "insert into entity_TimeLines values('" + guid + "','" + dr["Id"].ToString() + "')";
cmd_entity.ExecuteNonQuery();
}
}
#endregion
}
}
-------------------------------
Step 3: Creating Business Layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RRL;
using System.Data;
using DataAccess;
namespace Business
{
public class CityBL
{
CityDA varcityDA= null;
public CityBL(City varNCity)
{
varcityDA =new CityDA(varNCity);
}
public CityBL()
{
varcityDA = new CityDA();
}
#region ICityBL Members
public bool CreatenewCity()
{
return varcityDA.CreateNewCity();
}
public bool UpdateCity()
{
return varcityDA.UpdateCity();
}
public bool DeleteCity()
{
return varcityDA.DeleteCity();
}
public City getCityDetails()
{
return varcityDA.GetCityDetails();
}
public DataTable GetDecades()
{
return varcityDA.GetDecadesList();
}
public DataTable GetAllCountries()
{
return varcityDA.GetCountryList();
}
public DataTable GetStatesofCountry(string countryid)
{
return varcityDA.GetStatesofCountry(countryid);
}
public string GetCountryofState(string stateid)
{
return varcityDA.GetCountryofState(stateid);
}
public string GetStateofCity(string cityid)
{
return varcityDA.GetStateofCity(cityid);
}
#endregion
}
}
------------------
Step 4: Creating Presentation Layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using RRL;
using Business;
using System.Xml;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
public partial class NewTemplate : System.Web.UI.Page
{
CityBL vcity = new CityBL();
City ct = new City();
// PersonBL obj = null;
static string Mode, editentity;
protected void Page_Load(object sender, EventArgs e)
{
Mode = "Edit";
//Charity
//uc_Charity.ENTITYTYPEID = "8";
//Foundation
//uc_Foundation.ENTITYTYPEID = "7";
//YouTube
uc_Youtube.searchtable = "YouTube";
uc_Youtube.conditioncolumn = "YouTubesrc";
//TimeLine
uc_TimeLine.searchtable = "TimeLines";
uc_TimeLine.conditioncolumn = "TimeLineUrl";
if (!IsPostBack)
{
//Country();
//////Edit Mode Specific//////////////////////////////
if (Mode == "Edit")
{
btn_Delete.Visible = true;
//PersonBL newobject = new PersonBL();
string id = "729EF489-0084-4ED5-8912-5ACFC9AB054A";
// Person p = new Person();
editentity = id;
ct.ENTITYID = id;
//get First Requirements
//newobject = new PersonBL(ps);
vcity = new CityBL(ct);
ct = vcity.getCityDetails();
//DataTable dt=getp.GetWebsites();
//Populating
txt_Name.Text = ct.CITYTEXT;
txt_Tagline.Text = ct.TAGLINE;
txt_Archive.Text = ct.ARCHIVE;
txt_Abbrevation.Text = ct.ABBREVIATION;
txt_Tags.Text = ct.TAGS;
txt_Vanityurl.Text = ct.VANITYURL;
Loadcountries();
ddl_Country.SelectedValue = vcity.GetCountryofState(vcity.GetStateofCity(ct.ENTITYID));
LoadStates();
ddl_State.SelectedValue = vcity.GetStateofCity(ct.ENTITYID);
//Load AKAs
DataTable newdt = new DataTable();
DataColumn akatext = new DataColumn("AKAText");
DataColumn isdefault = new DataColumn("IsDefault");
newdt.Columns.Add(akatext);
newdt.Columns.Add(isdefault);
foreach (DataRow dr1 in ct.AKALIST.Rows)
{
DataRow dr2 = newdt.NewRow();
dr2["AKAText"] = dr1["AKAText"];
dr2["IsDefault"] = dr1["IsDefault"];
newdt.Rows.Add(dr2);
}
uc_AKAs.AssignAKAs(newdt);
//Load Decades
DataTable dt = ct.DECADELIST;
LoadDecades();
foreach (DataRow dr in dt.Rows)
{
foreach (ListItem li in cbl_Decades.Items)
{
if (li.Value == dr["id"].ToString())
li.Selected = true;
}
}
//Load Normal Websites
DataTable WebsitesTable = new DataTable();
DataColumn Url = new DataColumn("Url");
DataColumn UrlName = new DataColumn("UrlName");
DataColumn UrlTypeId = new DataColumn("UrlTypeId");
DataColumn UrlDesc = new DataColumn("UrlDescription");
DataColumn IsOfficial = new DataColumn("IsOfficialUrl");
WebsitesTable.Columns.Add(Url);
WebsitesTable.Columns.Add(UrlName);
WebsitesTable.Columns.Add(UrlDesc);
WebsitesTable.Columns.Add(IsOfficial);
WebsitesTable.Columns.Add(UrlTypeId);
foreach (DataRow dr1 in ct.WEBSITESLIST.Rows)
{
DataRow dr2 = WebsitesTable.NewRow();
dr2["Url"] = dr1["Url"];
dr2["UrlName"] = dr1["UrlName"];
dr2["UrlDescription"] = dr1["UrlDescription"];
dr2["IsOfficialUrl"] = dr1["IsOfficialUrl"];
dr2["UrlTypeId"] = dr1["UrlTypeId"];
WebsitesTable.Rows.Add(dr2);
}
uc_Websites.AssignWebsites(WebsitesTable);
DataTable dt_genres = ct.GENRELIST;
LoadGenres();
foreach (DataRow dr in dt_genres.Rows)
{
foreach (ListItem li in cbl_Genres0.Items)
{
if (li.Value == dr["id"].ToString())
li.Selected = true;
}
}
//Load Youtube
uc_Youtube.LoadTable(ct.YOUTUBELIST);
//Load TimeLine
uc_TimeLine.LoadTable(ct.TIMELINELIST);
chb_EPick.Checked = ct.EDITORPICK;
txt_Vanityurl.Text = ct.VANITYURL;
btn_CreateCity.Text = "Update";
}
else
{
uc_Websites.ClearWebsites();
uc_AKAs.ClearAKAs();
LoadGenres();//Genres
LoadDecades();//Decades
btn_Delete.Visible = true;
Loadcountries();
// ddl_Country.SelectedValue = vcity.GetCountryofState(vcity.GetStateofCity(ct.ENTITYID));
}
}
}
public void LoadDecades()
{
CityBL vcity = new CityBL();
DataTable Decades = vcity.GetDecades();
cbl_Decades.DataSource = Decades;
cbl_Decades.DataTextField = "Decade";
cbl_Decades.DataValueField = "Id";
cbl_Decades.DataBind();
}
public void LoadGenres()
{
GenreBL vGENRELIST = new GenreBL();
DataTable Genres = vGENRELIST.GetGENRELIST();
cbl_Genres0.DataSource = Genres;
cbl_Genres0.DataTextField = "GenreName";
cbl_Genres0.DataValueField = "Id";
cbl_Genres0.DataBind();
}
protected void btn_CreateCity_Click(object sender, EventArgs e)
{
City vNewCity = new City();
vNewCity.ENTITYTYPEID = "6";
vNewCity.CITYTEXT = txt_Name.Text;
vNewCity.STATEENTITYID = ddl_State.SelectedValue;
vNewCity.TAGLINE = txt_Tagline.Text;
vNewCity.TAGS = txt_Tags.Text;
vNewCity.VANITYURL = txt_Vanityurl.Text;
vNewCity.ABBREVIATION = txt_Abbrevation.Text;
vNewCity.ARCHIVE = txt_Archive.Text;
vNewCity.EDITORPICK = chb_EPick.Checked;
//DataTable Youtubes = uc_Youtube.RESULTSTABLE();
//vNewState.YOUTUBELIST = Youtubes;
DataTable AKAs = uc_AKAs.GetAKAs();
vNewCity.AKALIST = AKAs;
DataTable Websites = uc_Websites.GetWebsites();
vNewCity.WEBSITESLIST = Websites;
//Genres
DataTable Genres = new DataTable();
DataColumn dc1 = new DataColumn("Id");
Genres.Columns.Add(dc1);
foreach (ListItem li in cbl_Genres0.Items)
{
if (li.Selected == true)
{
DataRow dr = Genres.NewRow();
dr["Id"] = li.Value;
Genres.Rows.Add(dr);
}
}
//Genres Ends.........
vNewCity.GENRELIST = Genres;
//You Tubes
DataTable Youtubes = uc_Youtube.RESULTSTABLE();
vNewCity.YOUTUBELIST = Youtubes;
//Time Lines
DataTable TimeLines = uc_TimeLine.RESULTSTABLE();
vNewCity.TIMELINELIST = TimeLines;
//Decades
DataTable Decades = new DataTable();
DataColumn dc_decade = new DataColumn("Id");
Decades.Columns.Add(dc_decade);
foreach (ListItem li in cbl_Decades.Items)
{
if (li.Selected == true)
{
DataRow dr = Decades.NewRow();
dr["Id"] = li.Value;
Decades.Rows.Add(dr);
}
}
//Decades Ends Here
vNewCity.DECADELIST = Decades;
//vNewCountry.COUNTRYENTITYID = ddl_Country.SelectedValue;
CityBL citybl = new CityBL(vNewCity);
if (btn_CreateCity.Text.ToLower() != "update")
{
if (citybl.CreatenewCity())
//ClientScript.RegisterClientScriptBlock(this.GetType(), "msg", "");
Response.Write("Success!");
else
Response.Write("insertion Failed");
}
else
{
vNewCity.ENTITYID = "729EF489-0084-4ED5-8912-5ACFC9AB054A";
if (citybl.UpdateCity())
//ClientScript.RegisterClientScriptBlock(this.GetType(), "msg", "");
Response.Write("Success!");
else
Response.Write("insertion Failed");
}
}
protected void btn_Delete_Click(object sender, EventArgs e)
{
City vNewcity = new City();
string id = "729EF489-0084-4ED5-8912-5ACFC9AB054A";
ct.ENTITYID = id;
CityBL vca = new CityBL(vNewcity);
if (vca.DeleteCity())
//ClientScript.RegisterClientScriptBlock(this.GetType(), "msg", "");
Response.Write("Delete Success!");
else
Response.Write("Deletion Failed");
}
void Loadcountries()
{
CityBL nbl = new CityBL();
ddl_Country.DataSource = nbl.GetAllCountries();
ddl_Country.DataTextField = "countrytext";
ddl_Country.DataValueField = "EntityId";
ddl_Country.DataBind();
ddl_Country.Items.Insert(0, "Select");
}
public void LoadStates()
{
ddl_State.Items.Clear();
if (ddl_Country.SelectedIndex > 0)
{
CityBL nbl = new CityBL();
ddl_State.DataSource = nbl.GetStatesofCountry(ddl_Country.SelectedValue);
ddl_State.DataTextField = "statetext";
ddl_State.DataValueField = "entityid";
ddl_State.DataBind();
ddl_State.Items.Insert(0, "Select");
}
}
protected void ddl_Country_SelectedIndexChanged(object sender, EventArgs e)
{
LoadStates();
}
}
-------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace RRL
{
public abstract class Abstract
{
#region private variables
DataTable genrelist = null;
DataTable websiteslist = null;
DataTable youtubelist = null;
DataTable decadelist = null;
DataTable charitylist = null;
DataTable foundationlist = null;
DataTable akalist = null;
DataTable addresslist = null;
DataTable timelinelist = null;
DataTable editorslist = null;
DataTable venuelist = null;
DataTable creatorlist = null;
DataTable designerlist = null;
DataTable manufacturerlist = null;
DataTable ownerlist = null;
DataTable personbandcomp = null;
DataTable recordedlabellist = null;
DataTable recordingstudioslist = null;
DataTable authorlist = null;
DataTable engineerlist=null;
DataTable producerlist = null;
DataTable publisherlist = null;
DataTable samplesongslist = null;
DataTable bookformats = null;
DataTable mediaformats = null;
DataTable videogameversionlist = null;
DataTable rtvendroslist = null;
DataTable producerslist = null;
DataTable tourmanagerslist = null;
DataTable founderlist = null;
DataTable memberslist = null;
DataTable accesslevelslist = null;
DataTable objectslist = null;
DataTable permissionslist = null;
DataTable statuslist = null;
String blogurl, twitterurl, facebookurl, myspaceurl, archiveurl, hi5url;
#endregion
public DataTable VIDEOGAMEVERSIONLIST
{
get { return videogameversionlist; }
set { videogameversionlist = value; }
}
public DataTable OBJECTSLIST
{
get { return objectslist; }
set { objectslist = value; }
}
public DataTable PERMISSIONSLIST
{
get { return permissionslist; }
set { permissionslist = value; }
}
public DataTable STATUSLIST
{
get { return statuslist; }
set { statuslist = value; }
}
public DataTable ACCESSLEVELSLIST
{
get { return accesslevelslist; }
set { accesslevelslist = value; }
}
public DataTable MEMBERSLIST
{
get { return memberslist; }
set { memberslist = value; }
}
public DataTable PRODUCERSLIST
{
get { return producerslist; }
set { producerslist = value; }
}
public DataTable TOURMANAGERSLIST
{
get { return tourmanagerslist; }
set { tourmanagerslist = value; }
}
public DataTable RTVENDORSLIST
{
get { return rtvendroslist; }
set { rtvendroslist = value; }
}
public DataTable BOOKFORMATS
{
get { return bookformats; }
set { bookformats = value; }
}
public DataTable MEDIAFORMATS
{
get { return mediaformats; }
set { mediaformats = value; }
}
public DataTable PersonBandComp
{
get { return personbandcomp; }
set { personbandcomp = value; }
}
public DataTable ENGINEERLIST
{
get { return engineerlist; }
set { engineerlist = value; }
}
public DataTable PRODUCERLIST
{
get { return producerlist; }
set { producerlist = value; }
}
public DataTable SAMPLESONGSLIST
{
get { return samplesongslist; }
set { samplesongslist = value; }
}
public DataTable RECORDINGSTUDIOSLIST
{
get { return recordingstudioslist; }
set { recordingstudioslist = value; }
}
public DataTable RECORDEDLABELLIST
{
get { return recordedlabellist; }
set { recordedlabellist = value; }
}
public DataTable OWNERLIST
{
get { return ownerlist; }
set { ownerlist = value; }
}
public DataTable VENUELIST
{
get { return venuelist; }
set { venuelist = value; }
}
public DataTable CREATORLIST
{
get { return creatorlist; }
set { creatorlist = value; }
}
public DataTable DESIGNERLIST
{
get { return designerlist; }
set { designerlist = value; }
}
public DataTable GENRELIST
{
get { return genrelist; }
set { genrelist = value; }
}
public DataTable WEBSITESLIST
{
get { return websiteslist; }
set { websiteslist = value; }
}
public DataTable YOUTUBELIST
{
get { return youtubelist; }
set { youtubelist = value; }
}
public DataTable DECADELIST
{
get { return decadelist; }
set { decadelist = value; }
}
public DataTable PUBLISHERLIST
{
get { return publisherlist; }
set { publisherlist = value; }
}
public DataTable CHARITYLIST
{
get { return charitylist; }
set { charitylist = value; }
}
public DataTable FOUNDATIONLIST
{
get { return foundationlist; }
set { foundationlist = value; }
}
public DataTable AKALIST
{
get { return akalist; }
set { akalist = value; }
}
public DataTable ADDRESSLIST
{
get { return addresslist; }
set { addresslist = value; }
}
public DataTable AUTHORLIST
{
get { return authorlist; }
set { authorlist = value; }
}
public DataTable TIMELINELIST
{
get { return timelinelist; }
set { timelinelist = value; }
}
public DataTable FOUNDERLIST
{
get { return founderlist; }
set { founderlist = value; }
}
public String BlogUrl
{
get { return blogurl; }
set { blogurl = value; }
}
public String TwitterUrl
{
get { return twitterurl; }
set { twitterurl = value; }
}
public String FaceBookUrl
{
get { return facebookurl; }
set { facebookurl = value; }
}
public String MySpaceUrl
{
get { return myspaceurl; }
set { myspaceurl = value; }
}
public String Hi5Url
{
get { return hi5url; }
set { hi5url = value; }
}
public String ArchiveUrl
{
get { return archiveurl; }
set { archiveurl = value; }
}
public DataTable AuthorList
{
get { return authorlist; }
set { authorlist = value; }
}
public DataTable EditorList
{
get { return editorslist; }
set { editorslist = value; }
}
}
}
-----------------------
Create Abstract Data Access
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DataAccess
{
public abstract class AbstractDA
{
#region privatevariables
SqlConnection con = new SqlConnection();
ConnectionDA varCon = new ConnectionDA();
Exception varEx = new Exception();
#endregion
public AbstractDA()
{
con.ConnectionString = varCon.GetConnection;
}
public DataTable GetCountryList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from countryentitydetail", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetAuthorList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from entity_authors", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetEngineerList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from entity_engineers", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetProducerList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from entity_Producers", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetSampleSongs()
{
SqlDataAdapter da = new SqlDataAdapter("select * from song_samples", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetArtifactList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from artifacttypes", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable MEDIAFORMATS()
{
SqlDataAdapter da = new SqlDataAdapter("select * from entity_mediaformats", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable RECORDLABEL()
{
SqlDataAdapter da = new SqlDataAdapter("select * from companyentitydetail where companytypeid=1", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable RECORDINGSTUDIOSLIST()
{
SqlDataAdapter da = new SqlDataAdapter("select * from companyentitydetail where companytypeid=4", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetStateList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from stateentitydetail", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetStatesofCountry(string countryid)
{
SqlDataAdapter da = new SqlDataAdapter("select * from stateentitydetail where countryentityid=@countryentityid", con);
DataTable dt = new DataTable();
da.SelectCommand.Parameters.AddWithValue("@countryentityid", countryid);
da.Fill(dt);
return dt;
}
public string GetCountryofState(string stateid)
{
SqlCommand cmd = new SqlCommand("select CountryEntityId from stateentitydetail where entityid=@stateid", con);
cmd.Parameters.AddWithValue("@stateid", stateid);
if(con.State==ConnectionState.Closed)
con.Open();
return cmd.ExecuteScalar().ToString();
if(con.State==ConnectionState.Open)
con.Close();
}
public string GetStateofCity(string cityid)
{
SqlCommand cmd = new SqlCommand("select stateEntityId from cityentitydetail where entityid=@cityid", con);
cmd.Parameters.AddWithValue("@cityid", cityid);
if (con.State == ConnectionState.Closed)
con.Open();
return cmd.ExecuteScalar().ToString();
if (con.State == ConnectionState.Open)
con.Close();
}
public DataTable GetCitiesofState(string stateid)
{
SqlDataAdapter da = new SqlDataAdapter("select * from cityentitydetail where stateentityid=@stateentityid", con);
DataTable dt = new DataTable();
da.SelectCommand.Parameters.AddWithValue("@stateentityid", stateid);
da.Fill(dt);
return dt;
}
public DataTable GetCityList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Cityentitydetail", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetNeighborhoodList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Neighborhoodentitydetail", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetSongList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Songentitydetail", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetDecadesList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Decades", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public string GetASINObject(string ENTITYID)
{
string ASIN = string.Empty;
//SqlDataAdapter da = new SqlDataAdapter("select commonname,friendlyname,id from Entity,entity_authors,entitytype where entity.id=entity_authors.entityid and entity.entitytypeid=entitytype.id and entity_authors.entityid=@eid", con);
SqlCommand cmd = new SqlCommand("select ASIN from Amazon_ASIN where entityid=@id", con);
cmd.Parameters.AddWithValue("@id", ENTITYID);
try
{if(con.State==ConnectionState.Closed)
con.Open();
ASIN = cmd.ExecuteScalar().ToString();
}
catch
{
//No ASIN Available
}
finally
{if(con.State==ConnectionState.Open)
con.Close();
}
return ASIN;
}
public DataTable GetAuthorsListofObject(string ENTITYID)
{
//SqlDataAdapter da = new SqlDataAdapter("select commonname,friendlyname,id from Entity,entity_authors,entitytype where entity.id=entity_authors.entityid and entity.entitytypeid=entitytype.id and entity_authors.entityid=@eid", con);
SqlDataAdapter da = new SqlDataAdapter("select Person_Band_CompId as id,(select commonname from entity where id=a.Person_Band_CompId) as Name from Entity_Authors a where entityid=@id", con);
da.SelectCommand.Parameters.AddWithValue("@id", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetEngineersListofObject(string ENTITYID)
{
//SqlDataAdapter da = new SqlDataAdapter("select commonname,friendlyname,id from Entity,entity_authors,entitytype where entity.id=entity_authors.entityid and entity.entitytypeid=entitytype.id and entity_authors.entityid=@eid", con);
SqlDataAdapter da = new SqlDataAdapter("select PersonEntityId as id,(select commonname from entity where id=a.PersonEntityId) as Name from Entity_Engineers a where entityid=@id", con);
da.SelectCommand.Parameters.AddWithValue("@id", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetProducersListofObject(string ENTITYID)
{
//SqlDataAdapter da = new SqlDataAdapter("select commonname,friendlyname,id from Entity,entity_authors,entitytype where entity.id=entity_authors.entityid and entity.entitytypeid=entitytype.id and entity_authors.entityid=@eid", con);
SqlDataAdapter da = new SqlDataAdapter("select Person_BandEntityId as id,(select commonname from entity where id=a.Person_BandEntityId) as Name from Entity_producers a where entityid=@id", con);
da.SelectCommand.Parameters.AddWithValue("@id", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetSong_SamplesListofObject(string ENTITYID)
{
//SqlDataAdapter da = new SqlDataAdapter("select commonname,friendlyname,id from Entity,entity_authors,entitytype where entity.id=entity_authors.entityid and entity.entitytypeid=entitytype.id and entity_authors.entityid=@eid", con);
SqlDataAdapter da = new SqlDataAdapter("select samplesongid as id,(select commonname from entity where id=a.SampleSongId) as Name from Song_Samples a where songid=@id", con);
da.SelectCommand.Parameters.AddWithValue("@id", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetEditorsListofObject(string ENTITYID)
{
//SqlDataAdapter da = new SqlDataAdapter("select commonname,friendlyname,id from Entity,entity_authors,entitytype where entity.id=entity_authors.entityid and entity.entitytypeid=entitytype.id and entity_authors.entityid=@eid", con);
SqlDataAdapter da = new SqlDataAdapter("select Band_PersonId as id,(select commonname from entity where id=e.Band_PersonId) as Name from Entity_Editor e where entityid=@id", con);
da.SelectCommand.Parameters.AddWithValue("@id", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetBookFormatsList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from BookFormats", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetBookformatsListofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select * from book_bookformats where bookid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetMediaFormatsList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from MediaFormats", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetMediaFormatListofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select * from entity_mediaformats where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetAudioFormatsList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Aud_Rec_Formats", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetRighty_LeftyList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from RightyLefty", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetRatingsList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Ratings", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetCompanyType()
{
SqlDataAdapter da = new SqlDataAdapter("select * from companytype", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetPresident()
{
SqlDataAdapter da = new SqlDataAdapter("select * from company", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
//Access Levels
public DataTable GetAccessLevels()
{
SqlDataAdapter da = new SqlDataAdapter("select * from accesslevels", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
//Objects
public DataTable GetUserObjects()
{
SqlDataAdapter da = new SqlDataAdapter("select * from entitytype", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
//Permissions
public DataTable GetUserPermissions()
{
SqlDataAdapter da = new SqlDataAdapter("select * from permissionlevels", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
//Status
public DataTable GetUserStatus()
{
SqlDataAdapter da = new SqlDataAdapter("select * from entity_state", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetAllLanguages()
{
SqlDataAdapter da = new SqlDataAdapter("select * from mb_language", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetAllSoundTypes()
{
SqlDataAdapter da = new SqlDataAdapter("select * from soundtypes", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetPublisherListofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select Band_or_Person_CompId as id from entity_people_band_comp where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetPublishersList()
{
SqlDataAdapter da = new SqlDataAdapter("select * from companyentitydetail where companytypeid=18", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable GetGENRELISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select genreid as id from entity_genres where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetWEBSITESLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select url,urlname,urldescription,urltypeid,isofficialurl from entity_urls where entityid=@eid and urltypeid=1 ", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetDecadesListofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select decadeid as id from entity_decades where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetYOUTUBELISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select youtubeid as id,(select youtubesrc from youtube where id=e.youtubeid ) as name from entity_youtubes e where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetTIMELINELISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select timelineid as id,(select timelineurl from timelines where id=e.timelineid ) as name from entity_timelines e where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetCHARITYLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select entityid,CompanyName from companyentitydetail where entityid in(select compid from entity_charity where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetFOUNDATIONLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select entityid,CompanyName from companyentitydetail where entityid in(select compid from entity_foundation where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetVENUELISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select entityid,CompanyName from companyentitydetail where entityid in(select compid from entity_venues where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetCREATORLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select id,CommonName as name from entity where id in(select creatorid from entity_creators where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetTOURMANAGERSLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select id,CommonName as name from entity where id in(select managerid from tour_managers where tourid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetPRODUCERSLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select id,CommonName as name from entity where id in(select person_bandentityid from entity_producers where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetDESIGNERLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select id,CommonName as name from entity where id in(select designerid from entity_designer where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetOWNERLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select id,CommonName as name from entity where id in(select Person_Band_CompId from entity_Owner where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetPeople_Band_CompListofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select id,CommonName as name from entity where id in(select Band_or_Person_CompId from entity_people_band_comp where entityid=@eid)", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetAKALISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select AkaText,IsDefault from Entity_Akas where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public DataTable GetADDRESSLISTofObject(string ENTITYID)
{
SqlDataAdapter da = new SqlDataAdapter("select AddressTypeId,(select AddresstypeName from AddressType where id=e.AddressTypeId) as TypeName,Address1,Address2,Address3,PlaceId,(select commonname from entity where id=e.PlaceId) as PlaceName,postalCode from Entity_Addresses e where entityid=@eid", con);
da.SelectCommand.Parameters.AddWithValue("@eid", ENTITYID);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public string GetBlogUrlofObject(string ENTITYID)
{
string BlogUrl = string.Empty;
try
{
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd_blog = new SqlCommand("select url from entity_urls where entityid=@eid and urltypeid=2 ", con);
cmd_blog.Parameters.AddWithValue("@eid", ENTITYID);
SqlDataReader dr_blog = cmd_blog.ExecuteReader();
if (dr_blog.HasRows)
{
while (dr_blog.Read())
BlogUrl = dr_blog[0].ToString();
}
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return BlogUrl;
}
public string GetTwitterUrlofObject(string ENTITYID)
{
string TwitterUrl = string.Empty;
try
{
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd_twitter = new SqlCommand("select url from entity_urls where entityid=@eid and urltypeid=3 ", con);
cmd_twitter.Parameters.AddWithValue("@eid", ENTITYID);
SqlDataReader dr_twitter = cmd_twitter.ExecuteReader();
if (dr_twitter.HasRows)
{
while (dr_twitter.Read())
TwitterUrl = dr_twitter[0].ToString();
}
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return TwitterUrl;
}
public string GetFacebookUrlofObject(string ENTITYID)
{
string FaceBUrl = string.Empty;
try
{
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd_facebook = new SqlCommand("select url from entity_urls where entityid=@eid and urltypeid=4 ", con);
cmd_facebook.Parameters.AddWithValue("@eid", ENTITYID);
SqlDataReader dr_facebook = cmd_facebook.ExecuteReader();
if (dr_facebook.HasRows)
{
while (dr_facebook.Read())
FaceBUrl = dr_facebook[0].ToString();
}
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return FaceBUrl;
}
public string GetHi5UrlofObject(string ENTITYID)
{
string Hi5Url = string.Empty;
try
{
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd_hi5 = new SqlCommand("select url from entity_urls where entityid=@eid and urltypeid=5 ", con);
cmd_hi5.Parameters.AddWithValue("@eid", ENTITYID);
SqlDataReader dr_Hi5 = cmd_hi5.ExecuteReader();
if (dr_Hi5.HasRows)
{
while (dr_Hi5.Read())
{
Hi5Url= dr_Hi5[0].ToString();
}
}
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return Hi5Url;
}
public string GetArchiveUrlofObject(string ENTITYID)
{
string ArchiveUrl = string.Empty;
try
{
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd_Archive = new SqlCommand("select url from entity_urls where entityid=@eid and urltypeid=7 ", con);
cmd_Archive.Parameters.AddWithValue("@eid", ENTITYID);
SqlDataReader dr_Archive = cmd_Archive.ExecuteReader();
if (dr_Archive.HasRows)
{
while (dr_Archive.Read())
ArchiveUrl = dr_Archive[0].ToString();
}
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return ArchiveUrl;
}
public string GetMySpaceUrlofObject(string ENTITYID)
{
string myspaceurl=string.Empty;
try
{
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd_myspace = new SqlCommand("select url from entity_urls where entityid=@eid and urltypeid=7 ", con);
cmd_myspace.Parameters.AddWithValue("@eid", ENTITYID);
SqlDataReader dr_myspace = cmd_myspace.ExecuteReader();
if (dr_myspace.HasRows)
{
while(dr_myspace.Read())
myspaceurl =dr_myspace[0].ToString();
}
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return myspaceurl;
}
}
}
--------------------------------------
Step 1: First Create Object File means with database fields
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RRL
{
public class City : Abstract
{
string entityid;
string entitytypeid;
string citytext;
string stateentityid;
string abbreviation;
string archive;
string decades;
string tagline;
string vanityurl;
bool editorpick;
string tags;
public string ENTITYID
{
get
{
return entityid;
}
set
{
entityid = value;
}
}
public string STATEENTITYID
{
get
{
return stateentityid;
}
set
{
stateentityid = value;
}
}
public string DECADES
{
get
{
return decades;
}
set
{
decades = value;
}
}
public string ENTITYTYPEID
{
get
{
return entitytypeid;
}
set
{
entitytypeid = value;
}
}
public string CITYTEXT
{
get
{
return citytext;
}
set
{
citytext = value;
}
}
public string ABBREVIATION
{
get
{
return abbreviation;
}
set
{
abbreviation = value;
}
}
public string ARCHIVE
{
get
{
return archive;
}
set
{
archive = value;
}
}
public string TAGLINE
{
get
{
return tagline;
}
set
{
tagline = value;
}
}
public string VANITYURL
{
get
{
return vanityurl;
}
set
{
vanityurl = value;
}
}
public bool EDITORPICK
{
get
{
return editorpick;
}
set
{
editorpick = value;
}
}
public string TAGS
{
get
{
return tags;
}
set
{
tags = value;
}
}
}
}
---------------------------------
Step 2: Create Data Access Layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RRL;
using System.Data.SqlClient;
using System.Data;
namespace DataAccess
{
public class CityDA :AbstractDA
{
City vCity= null;
SqlConnection con = new SqlConnection();
ConnectionDA objCon = new ConnectionDA();
SqlCommand cmd_entity;
#region ICityDA Members
public CityDA(City vnewCity)
{
vCity = vnewCity;
con.ConnectionString = objCon.GetConnection;
}
public CityDA()
{
con.ConnectionString = objCon.GetConnection;
}
public bool CreateNewCity()
{
SqlTransaction Tran = null;
try
{
con.Open();
Guid newguid = Guid.NewGuid();
//First Inserting values in Entity Tables
SqlCommand cmd_entity = new SqlCommand("insert into entity(id,entitytypeid,entitystateid,commonname,createddate,createdby,ownerid,IsEditorsPick,VanityUrl,Tags) values(@id,@entitytypeid,@entitystateid,@commonname,@createddate,@createdby,@ownerid,@IsEditorsPick,@VanityUrl,@Tags)", con);
cmd_entity.Parameters.AddWithValue("@id", newguid);
cmd_entity.Parameters.AddWithValue("@entitytypeid", vCity.ENTITYTYPEID);
cmd_entity.Parameters.AddWithValue("@entitystateid", 1);
cmd_entity.Parameters.AddWithValue("@commonname",vCity.CITYTEXT.Replace("'", "''"));
cmd_entity.Parameters.AddWithValue("@createddate", DateTime.Now.ToString());
cmd_entity.Parameters.AddWithValue("@createdby", 1);
cmd_entity.Parameters.AddWithValue("@ownerid", 1);
cmd_entity.Parameters.AddWithValue("@IsEditorsPick", vCity.EDITORPICK);
cmd_entity.Parameters.AddWithValue("@VanityUrl", vCity.VANITYURL);
cmd_entity.Parameters.AddWithValue("@Tags", vCity.TAGS);
Tran = con.BeginTransaction();
cmd_entity.Transaction = Tran;
cmd_entity.ExecuteNonQuery();
// Tran.Save("CreateEntity");
//Inserting Unique Values
cmd_entity.CommandText = "insert into CityEntityDetail values(@entityid,@StateEntityId,@citytext,@Abbreviation,@archive,@TagLine)";
cmd_entity.Parameters.AddWithValue("@entityid", newguid);
if (vCity.STATEENTITYID != "")
cmd_entity.Parameters.AddWithValue("@StateEntityId", new Guid(vCity.STATEENTITYID));
else
cmd_entity.Parameters.AddWithValue("@StateEntityId", Guid.Empty);
cmd_entity.Parameters.AddWithValue("@CityText", vCity.CITYTEXT.Replace("'", "''"));
cmd_entity.Parameters.AddWithValue("@Abbreviation", vCity.ABBREVIATION.Replace("'", "''"));
cmd_entity.Parameters.AddWithValue("@archive", vCity.ARCHIVE.Replace("'", "''"));
cmd_entity.Parameters.AddWithValue("@TagLine", vCity.TAGLINE.Replace("'", "''"));
cmd_entity.ExecuteNonQuery();
//Tran.Save("CreatePerson");
//cmd_entity.Dispose();
//Inserting Relationships
//Websites
InsertWebsites(vCity.WEBSITESLIST, newguid, cmd_entity);
//AKA List
InsertAKAs(vCity.AKALIST, newguid, cmd_entity);
//Genres
InsertGenres(vCity.GENRELIST, newguid, cmd_entity);
//YOUTUBELIST
InsertYoutubes(vCity.YOUTUBELIST, newguid, cmd_entity);
//TIMELINELIST
InsertTimeLines(vCity.TIMELINELIST, newguid, cmd_entity);
//Decades
InsertDecades(vCity.DECADELIST, newguid, cmd_entity);
Tran.Commit();
return true;
}
catch
{
Tran.Rollback();
return false;
}
finally
{
con.Close();
}
}
public void InsertAKAs(DataTable AKALIST, Guid guid, SqlCommand cmd_entity)
{
foreach (DataRow dr in AKALIST.Rows)
{
cmd_entity.CommandText = "insert into entity_AKAs values('" + guid + "','" + dr["AKAText"].ToString().Replace("'", "''") + "','" + bool.Parse(dr["IsDefault"].ToString()) + "')";
cmd_entity.ExecuteNonQuery();
}
}
public bool UpdateCity()
{
SqlTransaction Tran = null;
try
{
con.Open();
Guid oldguid = new Guid(vCity.ENTITYID);
//First Inserting values in Entity Tables
SqlCommand cmd_entity = new SqlCommand("update entity set modifieddate=@modifieddate,modifiedby=@modifiedby,ownerid=@ownerid,IsEditorsPick=@IsEditorsPick,VanityUrl=@VanityUrl,Tags=@Tags where id=@id", con);
cmd_entity.Parameters.AddWithValue("@id", oldguid);
cmd_entity.Parameters.AddWithValue("@modifieddate", DateTime.Now.ToString());
cmd_entity.Parameters.AddWithValue("@modifiedby", 1);
cmd_entity.Parameters.AddWithValue("@ownerid", 1);
cmd_entity.Parameters.AddWithValue("@IsEditorsPick", vCity.EDITORPICK);
cmd_entity.Parameters.AddWithValue("@VanityUrl", vCity.VANITYURL);
cmd_entity.Parameters.AddWithValue("@Tags", vCity.TAGS);
Tran = con.BeginTransaction();
cmd_entity.Transaction = Tran;
cmd_entity.ExecuteNonQuery();
//Tran.Save("CreateEntity");
//Inserting Unique Values
cmd_entity.CommandText = "update CityEntityDetail set entityid=@entityid,stateentityid=@stateentityid,citytext=@citytext,abbreviation=@abbreviation,archive=@archive,TagLine=@TagLine where entityid=@entityid";
//SqlCommand cmd_person = new SqlCommand("insert into personentitydetail values(@entityid,@stagename,@firstname,@middlename,@lastname,@prefix,@postfix,@birthday,@birthmonth,@birthyear,@placeofbirth,@deathday,@deathmonth,@deathyear,@placeofdeath,@circumstancesofdeath,@rightylefty)", con);
cmd_entity.Parameters.AddWithValue("@entityid", oldguid);
cmd_entity.Parameters.AddWithValue("@stateentityid", vCity.STATEENTITYID);
cmd_entity.Parameters.AddWithValue("@citytext", vCity.CITYTEXT);
cmd_entity.Parameters.AddWithValue("@abbreviation", vCity.ABBREVIATION);
cmd_entity.Parameters.AddWithValue("@archive", vCity.ARCHIVE);
cmd_entity.Parameters.AddWithValue("@TagLine", vCity.TAGLINE);
cmd_entity.ExecuteNonQuery();
//Tran.Save("CreatePerson");
//cmd_entity.Dispose();
//Inserting Relationships
//Websites
cmd_entity.CommandText = "delete from entity_urls where entityid='" + oldguid + "'";
cmd_entity.ExecuteNonQuery();
InsertWebsites(vCity.WEBSITESLIST, oldguid, cmd_entity);
//AKA List
cmd_entity.CommandText = "delete from entity_AKAs where entityid='" + oldguid + "'";
cmd_entity.ExecuteNonQuery();
InsertAKAs(vCity.AKALIST, oldguid, cmd_entity);
//Genres
cmd_entity.CommandText = "delete from entity_Genres where entityid='" + oldguid + "'";
cmd_entity.ExecuteNonQuery();
InsertGenres(vCity.GENRELIST, oldguid, cmd_entity);
//YOUTUBELIST
cmd_entity.CommandText = "delete from entity_Youtubes where entityid='" + oldguid + "'";
cmd_entity.ExecuteNonQuery();
InsertYoutubes(vCity.YOUTUBELIST, oldguid, cmd_entity);
//TIMELINELIST
cmd_entity.CommandText = "delete from entity_TimeLines where entityid='" + oldguid + "'";
cmd_entity.ExecuteNonQuery();
InsertTimeLines(vCity.TIMELINELIST, oldguid, cmd_entity);
//DecadLIST
cmd_entity.CommandText = "delete from entity_decades where entityid='" + oldguid + "'";
cmd_entity.ExecuteNonQuery();
InsertDecades(vCity.DECADELIST, oldguid, cmd_entity);
Tran.Commit();
return true;
}
catch
{
Tran.Rollback();
return false;
}
finally
{
con.Close();
}
}
public bool DeleteCity()
{
return false;
}
public City GetCityDetails()
{
SqlCommand cmd_entity = new SqlCommand("select * from entity where id=@id", con);
cmd_entity.Parameters.AddWithValue("@id", vCity.ENTITYID);
con.Open();
SqlDataReader dr_entity = cmd_entity.ExecuteReader();
if (dr_entity.HasRows)
{
while (dr_entity.Read())
{
if (dr_entity["IsEditorsPick"].ToString() != "")
vCity.EDITORPICK = bool.Parse(dr_entity["IsEditorsPick"].ToString());
else
vCity.EDITORPICK = false;
vCity.VANITYURL = dr_entity["VanityUrl"].ToString();
vCity.TAGS = dr_entity["Tags"].ToString();
}
}
dr_entity.Close();
SqlCommand cmd = new SqlCommand("select * from cityentitydetail where entityid=@eid", con);
cmd.Parameters.AddWithValue("@eid", vCity.ENTITYID);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
vCity.ENTITYID = dr["entityid"].ToString();
vCity.STATEENTITYID = dr["stateentityid"].ToString();
vCity.CITYTEXT = dr["citytext"].ToString();
vCity.ABBREVIATION = dr["Abbreviation"].ToString();
vCity.ARCHIVE = dr["Archive"].ToString();
vCity.TAGLINE = dr["tagline"].ToString();
}
}
dr.Close();
con.Close();
vCity.WEBSITESLIST = GetWEBSITESLISTofObject(vCity.ENTITYID);
vCity.GENRELIST = GetGENRELISTofObject(vCity.ENTITYID);
vCity.YOUTUBELIST = GetYOUTUBELISTofObject(vCity.ENTITYID);
vCity.TIMELINELIST = GetTIMELINELISTofObject(vCity.ENTITYID);
vCity.AKALIST = GetAKALISTofObject(vCity.ENTITYID);
vCity.ArchiveUrl = GetArchiveUrlofObject(vCity.ENTITYID);
vCity.DECADELIST = GetDecadesListofObject(vCity.ENTITYID);
return vCity;
}
public void InsertWebsites(DataTable WEBSITESLIST, Guid guid, SqlCommand cmd_entity)
{
foreach (DataRow dr in WEBSITESLIST.Rows)
{
cmd_entity.CommandText = "insert into entity_urls values('" + guid + "','" + dr["Url"].ToString().Replace("'", "''") + "','" + dr["UrlName"].ToString().Replace("'", "''") + "','" + dr["UrlDescription"].ToString().Replace("'", "''") + "'," + int.Parse(dr["UrlTypeId"].ToString()) + ",'" + bool.Parse(dr["IsOfficialUrl"].ToString()) + "')";
cmd_entity.ExecuteNonQuery();
}
}
public void InsertGenres(DataTable GenresList, Guid guid, SqlCommand cmd_entity)
{
foreach (DataRow dr in GenresList.Rows)
{
cmd_entity.CommandText = "insert into entity_Genres values('" + guid + "','" + dr["Id"].ToString() + "')";
cmd_entity.ExecuteNonQuery();
}
}
public void InsertDecades(DataTable DECADELIST, Guid guid, SqlCommand cmd_entity)
{
foreach (DataRow dr in DECADELIST.Rows)
{
cmd_entity.CommandText = "insert into entity_Decades values('" + guid + "','" + dr["Id"].ToString() + "')";
cmd_entity.ExecuteNonQuery();
}
}
public void InsertYoutubes(DataTable YOUTUBELIST, Guid guid, SqlCommand cmd_entity)
{
foreach (DataRow dr in YOUTUBELIST.Rows)
{
cmd_entity.CommandText = "insert into entity_Youtubes values('" + guid + "','" + dr["Id"].ToString() + "')";
cmd_entity.ExecuteNonQuery();
}
}
public void InsertTimeLines(DataTable TIMELINELIST, Guid guid, SqlCommand cmd_entity)
{
foreach (DataRow dr in TIMELINELIST.Rows)
{
cmd_entity.CommandText = "insert into entity_TimeLines values('" + guid + "','" + dr["Id"].ToString() + "')";
cmd_entity.ExecuteNonQuery();
}
}
#endregion
}
}
-------------------------------
Step 3: Creating Business Layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RRL;
using System.Data;
using DataAccess;
namespace Business
{
public class CityBL
{
CityDA varcityDA= null;
public CityBL(City varNCity)
{
varcityDA =new CityDA(varNCity);
}
public CityBL()
{
varcityDA = new CityDA();
}
#region ICityBL Members
public bool CreatenewCity()
{
return varcityDA.CreateNewCity();
}
public bool UpdateCity()
{
return varcityDA.UpdateCity();
}
public bool DeleteCity()
{
return varcityDA.DeleteCity();
}
public City getCityDetails()
{
return varcityDA.GetCityDetails();
}
public DataTable GetDecades()
{
return varcityDA.GetDecadesList();
}
public DataTable GetAllCountries()
{
return varcityDA.GetCountryList();
}
public DataTable GetStatesofCountry(string countryid)
{
return varcityDA.GetStatesofCountry(countryid);
}
public string GetCountryofState(string stateid)
{
return varcityDA.GetCountryofState(stateid);
}
public string GetStateofCity(string cityid)
{
return varcityDA.GetStateofCity(cityid);
}
#endregion
}
}
------------------
Step 4: Creating Presentation Layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using RRL;
using Business;
using System.Xml;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
public partial class NewTemplate : System.Web.UI.Page
{
CityBL vcity = new CityBL();
City ct = new City();
// PersonBL obj = null;
static string Mode, editentity;
protected void Page_Load(object sender, EventArgs e)
{
Mode = "Edit";
//Charity
//uc_Charity.ENTITYTYPEID = "8";
//Foundation
//uc_Foundation.ENTITYTYPEID = "7";
//YouTube
uc_Youtube.searchtable = "YouTube";
uc_Youtube.conditioncolumn = "YouTubesrc";
//TimeLine
uc_TimeLine.searchtable = "TimeLines";
uc_TimeLine.conditioncolumn = "TimeLineUrl";
if (!IsPostBack)
{
//Country();
//////Edit Mode Specific//////////////////////////////
if (Mode == "Edit")
{
btn_Delete.Visible = true;
//PersonBL newobject = new PersonBL();
string id = "729EF489-0084-4ED5-8912-5ACFC9AB054A";
// Person p = new Person();
editentity = id;
ct.ENTITYID = id;
//get First Requirements
//newobject = new PersonBL(ps);
vcity = new CityBL(ct);
ct = vcity.getCityDetails();
//DataTable dt=getp.GetWebsites();
//Populating
txt_Name.Text = ct.CITYTEXT;
txt_Tagline.Text = ct.TAGLINE;
txt_Archive.Text = ct.ARCHIVE;
txt_Abbrevation.Text = ct.ABBREVIATION;
txt_Tags.Text = ct.TAGS;
txt_Vanityurl.Text = ct.VANITYURL;
Loadcountries();
ddl_Country.SelectedValue = vcity.GetCountryofState(vcity.GetStateofCity(ct.ENTITYID));
LoadStates();
ddl_State.SelectedValue = vcity.GetStateofCity(ct.ENTITYID);
//Load AKAs
DataTable newdt = new DataTable();
DataColumn akatext = new DataColumn("AKAText");
DataColumn isdefault = new DataColumn("IsDefault");
newdt.Columns.Add(akatext);
newdt.Columns.Add(isdefault);
foreach (DataRow dr1 in ct.AKALIST.Rows)
{
DataRow dr2 = newdt.NewRow();
dr2["AKAText"] = dr1["AKAText"];
dr2["IsDefault"] = dr1["IsDefault"];
newdt.Rows.Add(dr2);
}
uc_AKAs.AssignAKAs(newdt);
//Load Decades
DataTable dt = ct.DECADELIST;
LoadDecades();
foreach (DataRow dr in dt.Rows)
{
foreach (ListItem li in cbl_Decades.Items)
{
if (li.Value == dr["id"].ToString())
li.Selected = true;
}
}
//Load Normal Websites
DataTable WebsitesTable = new DataTable();
DataColumn Url = new DataColumn("Url");
DataColumn UrlName = new DataColumn("UrlName");
DataColumn UrlTypeId = new DataColumn("UrlTypeId");
DataColumn UrlDesc = new DataColumn("UrlDescription");
DataColumn IsOfficial = new DataColumn("IsOfficialUrl");
WebsitesTable.Columns.Add(Url);
WebsitesTable.Columns.Add(UrlName);
WebsitesTable.Columns.Add(UrlDesc);
WebsitesTable.Columns.Add(IsOfficial);
WebsitesTable.Columns.Add(UrlTypeId);
foreach (DataRow dr1 in ct.WEBSITESLIST.Rows)
{
DataRow dr2 = WebsitesTable.NewRow();
dr2["Url"] = dr1["Url"];
dr2["UrlName"] = dr1["UrlName"];
dr2["UrlDescription"] = dr1["UrlDescription"];
dr2["IsOfficialUrl"] = dr1["IsOfficialUrl"];
dr2["UrlTypeId"] = dr1["UrlTypeId"];
WebsitesTable.Rows.Add(dr2);
}
uc_Websites.AssignWebsites(WebsitesTable);
DataTable dt_genres = ct.GENRELIST;
LoadGenres();
foreach (DataRow dr in dt_genres.Rows)
{
foreach (ListItem li in cbl_Genres0.Items)
{
if (li.Value == dr["id"].ToString())
li.Selected = true;
}
}
//Load Youtube
uc_Youtube.LoadTable(ct.YOUTUBELIST);
//Load TimeLine
uc_TimeLine.LoadTable(ct.TIMELINELIST);
chb_EPick.Checked = ct.EDITORPICK;
txt_Vanityurl.Text = ct.VANITYURL;
btn_CreateCity.Text = "Update";
}
else
{
uc_Websites.ClearWebsites();
uc_AKAs.ClearAKAs();
LoadGenres();//Genres
LoadDecades();//Decades
btn_Delete.Visible = true;
Loadcountries();
// ddl_Country.SelectedValue = vcity.GetCountryofState(vcity.GetStateofCity(ct.ENTITYID));
}
}
}
public void LoadDecades()
{
CityBL vcity = new CityBL();
DataTable Decades = vcity.GetDecades();
cbl_Decades.DataSource = Decades;
cbl_Decades.DataTextField = "Decade";
cbl_Decades.DataValueField = "Id";
cbl_Decades.DataBind();
}
public void LoadGenres()
{
GenreBL vGENRELIST = new GenreBL();
DataTable Genres = vGENRELIST.GetGENRELIST();
cbl_Genres0.DataSource = Genres;
cbl_Genres0.DataTextField = "GenreName";
cbl_Genres0.DataValueField = "Id";
cbl_Genres0.DataBind();
}
protected void btn_CreateCity_Click(object sender, EventArgs e)
{
City vNewCity = new City();
vNewCity.ENTITYTYPEID = "6";
vNewCity.CITYTEXT = txt_Name.Text;
vNewCity.STATEENTITYID = ddl_State.SelectedValue;
vNewCity.TAGLINE = txt_Tagline.Text;
vNewCity.TAGS = txt_Tags.Text;
vNewCity.VANITYURL = txt_Vanityurl.Text;
vNewCity.ABBREVIATION = txt_Abbrevation.Text;
vNewCity.ARCHIVE = txt_Archive.Text;
vNewCity.EDITORPICK = chb_EPick.Checked;
//DataTable Youtubes = uc_Youtube.RESULTSTABLE();
//vNewState.YOUTUBELIST = Youtubes;
DataTable AKAs = uc_AKAs.GetAKAs();
vNewCity.AKALIST = AKAs;
DataTable Websites = uc_Websites.GetWebsites();
vNewCity.WEBSITESLIST = Websites;
//Genres
DataTable Genres = new DataTable();
DataColumn dc1 = new DataColumn("Id");
Genres.Columns.Add(dc1);
foreach (ListItem li in cbl_Genres0.Items)
{
if (li.Selected == true)
{
DataRow dr = Genres.NewRow();
dr["Id"] = li.Value;
Genres.Rows.Add(dr);
}
}
//Genres Ends.........
vNewCity.GENRELIST = Genres;
//You Tubes
DataTable Youtubes = uc_Youtube.RESULTSTABLE();
vNewCity.YOUTUBELIST = Youtubes;
//Time Lines
DataTable TimeLines = uc_TimeLine.RESULTSTABLE();
vNewCity.TIMELINELIST = TimeLines;
//Decades
DataTable Decades = new DataTable();
DataColumn dc_decade = new DataColumn("Id");
Decades.Columns.Add(dc_decade);
foreach (ListItem li in cbl_Decades.Items)
{
if (li.Selected == true)
{
DataRow dr = Decades.NewRow();
dr["Id"] = li.Value;
Decades.Rows.Add(dr);
}
}
//Decades Ends Here
vNewCity.DECADELIST = Decades;
//vNewCountry.COUNTRYENTITYID = ddl_Country.SelectedValue;
CityBL citybl = new CityBL(vNewCity);
if (btn_CreateCity.Text.ToLower() != "update")
{
if (citybl.CreatenewCity())
//ClientScript.RegisterClientScriptBlock(this.GetType(), "msg", "");
Response.Write("Success!");
else
Response.Write("insertion Failed");
}
else
{
vNewCity.ENTITYID = "729EF489-0084-4ED5-8912-5ACFC9AB054A";
if (citybl.UpdateCity())
//ClientScript.RegisterClientScriptBlock(this.GetType(), "msg", "");
Response.Write("Success!");
else
Response.Write("insertion Failed");
}
}
protected void btn_Delete_Click(object sender, EventArgs e)
{
City vNewcity = new City();
string id = "729EF489-0084-4ED5-8912-5ACFC9AB054A";
ct.ENTITYID = id;
CityBL vca = new CityBL(vNewcity);
if (vca.DeleteCity())
//ClientScript.RegisterClientScriptBlock(this.GetType(), "msg", "");
Response.Write("Delete Success!");
else
Response.Write("Deletion Failed");
}
void Loadcountries()
{
CityBL nbl = new CityBL();
ddl_Country.DataSource = nbl.GetAllCountries();
ddl_Country.DataTextField = "countrytext";
ddl_Country.DataValueField = "EntityId";
ddl_Country.DataBind();
ddl_Country.Items.Insert(0, "Select");
}
public void LoadStates()
{
ddl_State.Items.Clear();
if (ddl_Country.SelectedIndex > 0)
{
CityBL nbl = new CityBL();
ddl_State.DataSource = nbl.GetStatesofCountry(ddl_Country.SelectedValue);
ddl_State.DataTextField = "statetext";
ddl_State.DataValueField = "entityid";
ddl_State.DataBind();
ddl_State.Items.Insert(0, "Select");
}
}
protected void ddl_Country_SelectedIndexChanged(object sender, EventArgs e)
{
LoadStates();
}
}
-------------------------------
Tuesday, June 16, 2009
Developing and Using a Custom Server Control
•Create an ASP.NET server control.
•Add metadata to the control and its members to control security and design-time behavior.
•Use the App_Code directory in an ASP.NET Web site to test your control without manual compilation steps.
•Specify a tag prefix in a configuration file and in the control's assembly.
•Compile the control into an assembly and add it to the Bin directory.
•Embed a bitmap into the control's assembly as the toolbox icon for a visual designer.
•Use the compiled control in a page.
To create the code for the custom server control
1. In Visual Studio or Visual Web Developer Express Edition, create a Web site and name it ServerControlsTest.
2. Create an App_Code directory directly under the root directory of your Web site.
ASP.NET dynamically compiles code included in the App_Code directory under an ASP.NET Web site's root. Classes in source files in App_Code can therefore be accessed from pages without being manually compiled into assemblies.
3. In the App_Code directory, create a class named WelcomeLabel.cs or WelcomeLabel.vb.
4. Add the following code to the source file for the control:
Visual Basic
Copy Code
' WelcomeLabel.vb
Option Strict On
Imports System
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace Samples.AspNet.VB.Controls
< _
AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal), _
AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal), _
DefaultProperty("Text"), _
ToolboxData( _
"<{0}:WelcomeLabel runat=""server""> {0}:WelcomeLabel>") _
> _
Public Class WelcomeLabel
Inherits Label
< _
Bindable(False), _
Category("Appearance"), _
Description("The text to use if the user is not authenticated."), _
DefaultValue(""), _
Localizable(True) _
> _
Public Property NameForAnonymousUser() As String
Get
Dim s As String = CStr(ViewState("NameForAnonymousUser"))
If s Is Nothing Then s = String.Empty
Return s
End Get
Set(ByVal value As String)
ViewState("NameForAnonymousUser") = value
End Set
End Property
Protected Overrides Sub RenderContents( _
ByVal writer As HtmlTextWriter)
writer.WriteEncodedText(Text)
If Context IsNot Nothing Then
Dim s As String = Context.User.Identity.Name
If (s IsNot Nothing) AndAlso (s <> String.Empty) Then
Dim split() As String = s.Split("\".ToCharArray)
Dim n As Integer = split.Length - 1
If (split(n) <> String.Empty) Then
writer.Write(", ")
writer.Write(split(n))
End If
Else
If (NameForAnonymousUser <> String.Empty) Then
writer.Write(", ")
writer.Write(NameForAnonymousUser)
Else
writer.Write("!")
End If
End If
End If
End Sub
End Class
End Namespace
C#
Copy Code
// WelcomeLabel.cs
using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Samples.AspNet.CS.Controls
{
[
AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal),
DefaultProperty("Text"),
ToolboxData("<{0}:WelcomeLabel runat=\"server\"> {0}:WelcomeLabel>")
]
public class WelcomeLabel : Label
{
[
Bindable(false),
Category("Appearance"),
Description("The text to use if the user is not authenticated."),
DefaultValue(""),
Localizable(true)
]
public string NameForAnonymousUser
{
get
{
string s = (string)ViewState["NameForAnonymousUser"];
return (s == null) ? String.Empty : s;
}
set
{
ViewState["NameForAnonymousUser"] = value;
}
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.WriteEncodedText(Text);
if (Context != null)
{
string s = Context.User.Identity.Name;
if (s != null && s != String.Empty)
{
string[] split = s.Split('\\');
int n = split.Length - 1;
if (split[n] != String.Empty)
{
writer.Write(", ");
writer.Write(split[n]);
}
}
else
{
if (NameForAnonymousUser != String.Empty)
{
writer.Write(", ");
writer.Write(NameForAnonymousUser);
}
else
{
writer.Write("!");
}
}
}
}
}
}
Code Discussion
If your control renders a user interface (UI) element or any other visible element on the client, you should derive your control from System.Web.UI.WebControls..::.WebControl (or a derived class). If your control renders an element that is not visible in the browser, such as a hidden element or a meta element, derive your control from System.Web.UI..::.Control. The WebControl class derives from Control and adds style-related properties such as Font, ForeColor, and BackColor. In addition, a control that derives from WebControl participates in the themes features of ASP.NET without any extra work on your part.
If your control extends the functionality of an existing control, such as the Button, Label, or Image controls, you can derive from that control.
From Label, WelcomeLabel inherits the Text property. WelcomeLabel overrides the RenderContents method to provide the logic for creating the text to display. The parameter passed into the RenderContents method is an object of type HtmlTextWriter, which is a utility class that has methods for rendering tags and other HTML (and HTML-variant) markup.
The WelcomeLabel uses view state to store the value for the NameForAnonymousUser property. Using view state persists the value of NameForAnonymousUser across postbacks. On each postback, the page is recreated and the value is restored from view state. If the value was not stored in view state, the value would be set to its default, Empty, on each postback. The ViewState property inherited from WebControl is a dictionary that saves data values. Values are entered and retrieved by using a String key. In this case, "NameForAnonymousUser" is used as the key. Items in the dictionary are typed as Object, which you must then cast to the property type. For more information, see ASP.NET State Management Overview.
Notice that WelcomeLabel makes successive calls to the Write method of the HtmlTextWriter object, instead of performing string concatenation and then invoking the Write method. This improves performance because the HtmlTextWriter object writes directly to the output stream. String concatenation requires time and memory to create the string, and then writes to the stream. When you implement rendering in your controls, you should follow the pattern illustrated in this walkthrough.
The attributes applied to WelcomeLabel contain metadata that is used by the common language runtime and by design-time tools. At the class level, WelcomeLabel is marked with the following attributes:
•AspNetHostingPermissionAttribute is a code-access security attribute. It causes the just-in-time (JIT) compiler to check that code that links to WelcomeLabel is granted the AspNetHostingPermission permission. All public ASP.NET classes are marked with this attribute. You should apply AspNetHostingPermissionAttribute to your controls as a security check against partially trusted callers. For more information, see Code Access Security. For more information about the JIT compiler, see Compiling MSIL to Native Code.
•DefaultPropertyAttribute is a design-time attribute that specifies the default property of a control. In visual designers, the property browser typically highlights the default property when a page developer clicks the control on the design surface.
•ToolboxDataAttribute specifies the format string for the element. The string becomes the control's markup when the control is double-clicked in the toolbox or dragged from the toolbox onto the design surface. For WelcomeLabel, the attribute creates this element:
The WelcomeLabel control also inherits two attributes from the WebControl base class, ParseChildrenAttribute and PersistChildrenAttribute. They are applied as ParseChildren(true) and PersistChildren(false). These two attributes work together so that child elements are interpreted as properties and properties are persisted as attributes.
The following attributes applied to the NameForAnonymousUser property of WelcomeLabel are standard design-time attributes that you will generally apply to public properties of your controls:
•BindableAttribute, specified as true or false, specifies for visual designers whether it is meaningful to bind the property to data. For example, in Visual Studio, if a property is marked with Bindable(true), the property is displayed in the DataBindings dialog box. If a property is not marked with this attribute, the property browser infers the value to be Bindable(false).
•CategoryAttribute specifies how to categorize the property in the visual designer's property browser. For example, Category("Appearance") tells the property browser to display the property in the Appearance category when the page developer uses the category view of the property browser. You can specify a string argument corresponding to an existing category in the property browser or create your own category.
•DescriptionAttribute specifies a brief description of the property. In Visual Studio, the property browser displays the description of the selected property at the bottom of the Properties window.
•DefaultValueAttribute specifies a default value for the property. This value should be the same as the default value that you return from the property accessor (getter). In Visual Studio, the DefaultValueAttribute allows a page developer to reset a property value to its default by displaying the shortcut menu in the Properties window and clicking the Reset button.
•LocalizableAttribute, specified as true or false, specifies for visual designers whether it is meaningful to localize the property. When a property is marked Localizable(true), the visual designer includes it when localized resources are being serialized. The designer will persist the property value to the culture-neutral resource file or other localization source when the control is polled for localizable properties.
To add a tag prefix mapping in a Web page
•In the Default.aspx page, add the following code after the @ Page directive:
[C#]
Copy Code
<%@ Register TagPrefix="aspSample"
Namespace="Samples.AspNet.CS.Controls"%>
[Visual Basic]
Copy Code
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls"%>
Creating a Page to Use the Control
You can now use the custom control in an ASP.NET Web page.
To create a page that uses the custom control
1.Copy the following markup into the Default.aspx file and save the file.
Visual Basic
Copy Code
WelcomeLabel Test
C#
Copy Code
WelcomeLabel Test
2.View Default.aspx in your browser.
In addition to the WelcomeLabel control's Text property that was explicitly defined in the markup that you copied, you can see from the control instance in the page that it has BackColor and ForeColor properties that you did not define. The WelcomeLabel control gets these and other style-related properties by inheritance from the WebControl base class. In addition, WelcomeLabel can be assigned a skin and be part of a theme without any work on your part.
Compiling the Control into an Assembly
The App_Code directory enables you to test your control without compiling it. However, if you want to distribute your control as object code to other developers, you must compile it. In addition, a control cannot be added to the toolbox of a visual designer unless it is compiled into an assembly.
You can provide a default tag prefix that a visual designer should use for your control by including the assembly-level System.Web.UI..::.TagPrefixAttribute attribute. The tag prefix is registered with the page the first time the control is double-clicked in the toolbox or dragged from the toolbox onto the page.
If you decide to use the TagPrefixAttribute attribute, you can specify it in a separate file that is compiled with your controls. By convention, the file is named AssemblyInfo.languageExtension, such as AssemblyInfo.cs or AssembyInfo.vb. The following procedure describes how to specify the TagPrefixAttribute metadata.
To prepare the control for compiling and provide a default tag prefix
1. Create a new directory named CustomControls that is not in the Web site.
2. Create a file that is named AssemblyInfo.cs or AssemblyInfo.vb in the directory and add the following code to the file.
C#
Copy Code
using System;
using System.Web.UI;
[assembly: TagPrefix("Samples.AspNet.CS.Controls", "aspSample")]
Visual Basic
Copy Code
Imports System
Imports System.Web.UI
The TagPrefix attribute creates a mapping between the namespace Samples.AspNet.CS.Controls or Samples.AspNet.VB.Controls and the prefix aspSample.
3. Copy the WelcomeLabel.cs or WelcomeLabel.vb file from App_Code to the CustomControls directory.
4. Delete the WelcomeLabel.cs or WelcomeLabel.vb file in the App_Code folder of the Web site.
If you do not delete the source files from the App_Code folder, your control's type will occur in both the compiled assembly and in the dynamically generated assembly created by ASP.NET. This will create an ambiguous reference when your control is loaded, and any page in which the control is used will generate a compiler error.
A visual designer such as Visual Studio typically uses a default icon (such as an image of a gear) to display a control in the toolbox. As an option for your control, you can customize the appearance of your control in the toolbox by embedding a 16-by-16-pixel bitmap in your control's assembly. By convention, visual designers use the lowermost left pixel of the bitmap as the transparent color.
To create a toolbox icon
1. Create or obtain a 16-by-16-pixel bitmap as the toolbox icon for your control.
2. Name the bitmap WelcomeLabel.bmp.
3. Add the bitmap file to the CustomControls directory where you have the source files for the WelcomeLabel control.
When compiling the code, you embed the bitmap as a resource in the assembly.
To compile the control into an assembly and embed the icon
1. Open the Visual Studio Command Prompt window. For more information, see Command-line Building With csc.exe.
2. At the command line, switch to the directory that contains the custom control class files.
3. Run the following command.
C#
Copy Code
csc /res:WelcomeLabel.bmp,Samples.AspNet.CS.Controls.WelcomeLabel.bmp /t:library /out:Samples.AspNet.CS.Controls.dll /r:System.dll /r:System.Web.dll *.cs
Visual Basic
Copy Code
vbc /res:WelcomeLabel.bmp,Samples.AspNet.VB.Controls.WelcomeLabel.bmp /t:library /out:Samples.AspNet.VB.Controls.dll /r:System.dll /r:System.Web.dll *.vb
The /res compiler option embeds a resource in the assembly. In this example, the bitmap is embedded as a resource. You specify a name for the resource in the second parameter of the /res option. You must name the embedded bitmap resource the same as the namespace-qualified name of the control with which it is associated. For example, if the name of the control is Samples.AspNet.CS.Controls.WelcomeLabel, the name of the embedded bitmap must be Samples.AspNet.CS.Controls.WelcomeLabel.bmp.
This naming convention causes a visual designer to automatically use the bitmap as your control's toolbox icon. If you do not use the naming convention, you must apply the ToolboxBitmapAttribute to the control to specify the name of the embedded bitmap resource.
The /t:library compiler option tells the compiler to create a library instead of an executable assembly. The /out option provides a name for the assembly and the /r option lists the assemblies that are linked to your assembly.
To keep the example self-contained, this walkthrough asks you to create an assembly with a single control. In general, the .NET Framework design guidelines recommend that you do not create assemblies that contain only a few classes. For ease of deployment, you should create as few assemblies as possible.
4. When you have finished compiling the class files, close the Command line window.
Using the Compiled Custom Control in an ASP.NET Page
To test the compiled version of your custom control, you must make your control's assembly available to pages in the Web site.
To make your control's assembly available to the Web site and add it to the toolbox
1. Return to Visual Studio.
2. Create a Bin directory under the root of the Web site.
3. Right-click the Bin directory and select Add Existing Item.
4. Navigate to the directory that contains the control assembly (Samples.AspNet.CS.Controls.dll or Samples.AspNet.VB.Controls.dll) and select the assembly.
5. Right-click anywhere in the Toolbox and then click Add Tab.
6. Name the tab "CustomControl".
7. Right-click the CustomControl tab and then click Choose Items.
8. Click Browse and select the assembly that you copied to the Bin folder.
8. The WelcomeLabel control now appears in the Toolbox with the icon you created.
The assembly that you created in this walkthrough is referred to as a private assembly because it must be included in an ASP.NET Web site's Bin directory to enable pages in the Web site to use your control. The assembly cannot be accessed from other applications unless a copy is also installed with those applications. If you are creating controls for shared Web hosting applications, you will typically package your controls in a private assembly. However, if you create controls for use in a dedicated hosting environment or you create a suite of controls that an ISP makes available to all its customers, you might have to package your controls in a shared (strongly named) assembly that is installed in the global assembly cache. For more information, see Working with Assemblies and the Global Assembly Cache.
The custom control is now available from the Toolbox.
To use the custom control from the Toolbox
1. Delete Default.aspx to remove any code related to the custom control from the previous procedures.
2. Create a new Web page called Default2.aspx.
3. In Design view, drag the WelcomeLabel control from the Toolbox onto Default2.aspx.
4. Set the Text property to "Hello".
5. In Source view, notice that the @ Register directive was automatically added. The assembly attribute is set because the control no longer is in the App_Code folder.
6. View Default.aspx in a Web browser.
•Add metadata to the control and its members to control security and design-time behavior.
•Use the App_Code directory in an ASP.NET Web site to test your control without manual compilation steps.
•Specify a tag prefix in a configuration file and in the control's assembly.
•Compile the control into an assembly and add it to the Bin directory.
•Embed a bitmap into the control's assembly as the toolbox icon for a visual designer.
•Use the compiled control in a page.
To create the code for the custom server control
1. In Visual Studio or Visual Web Developer Express Edition, create a Web site and name it ServerControlsTest.
2. Create an App_Code directory directly under the root directory of your Web site.
ASP.NET dynamically compiles code included in the App_Code directory under an ASP.NET Web site's root. Classes in source files in App_Code can therefore be accessed from pages without being manually compiled into assemblies.
3. In the App_Code directory, create a class named WelcomeLabel.cs or WelcomeLabel.vb.
4. Add the following code to the source file for the control:
Visual Basic
Copy Code
' WelcomeLabel.vb
Option Strict On
Imports System
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace Samples.AspNet.VB.Controls
< _
AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal), _
AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal), _
DefaultProperty("Text"), _
ToolboxData( _
"<{0}:WelcomeLabel runat=""server""> {0}:WelcomeLabel>") _
> _
Public Class WelcomeLabel
Inherits Label
< _
Bindable(False), _
Category("Appearance"), _
Description("The text to use if the user is not authenticated."), _
DefaultValue(""), _
Localizable(True) _
> _
Public Property NameForAnonymousUser() As String
Get
Dim s As String = CStr(ViewState("NameForAnonymousUser"))
If s Is Nothing Then s = String.Empty
Return s
End Get
Set(ByVal value As String)
ViewState("NameForAnonymousUser") = value
End Set
End Property
Protected Overrides Sub RenderContents( _
ByVal writer As HtmlTextWriter)
writer.WriteEncodedText(Text)
If Context IsNot Nothing Then
Dim s As String = Context.User.Identity.Name
If (s IsNot Nothing) AndAlso (s <> String.Empty) Then
Dim split() As String = s.Split("\".ToCharArray)
Dim n As Integer = split.Length - 1
If (split(n) <> String.Empty) Then
writer.Write(", ")
writer.Write(split(n))
End If
Else
If (NameForAnonymousUser <> String.Empty) Then
writer.Write(", ")
writer.Write(NameForAnonymousUser)
Else
writer.Write("!")
End If
End If
End If
End Sub
End Class
End Namespace
C#
Copy Code
// WelcomeLabel.cs
using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Samples.AspNet.CS.Controls
{
[
AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal),
DefaultProperty("Text"),
ToolboxData("<{0}:WelcomeLabel runat=\"server\"> {0}:WelcomeLabel>")
]
public class WelcomeLabel : Label
{
[
Bindable(false),
Category("Appearance"),
Description("The text to use if the user is not authenticated."),
DefaultValue(""),
Localizable(true)
]
public string NameForAnonymousUser
{
get
{
string s = (string)ViewState["NameForAnonymousUser"];
return (s == null) ? String.Empty : s;
}
set
{
ViewState["NameForAnonymousUser"] = value;
}
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.WriteEncodedText(Text);
if (Context != null)
{
string s = Context.User.Identity.Name;
if (s != null && s != String.Empty)
{
string[] split = s.Split('\\');
int n = split.Length - 1;
if (split[n] != String.Empty)
{
writer.Write(", ");
writer.Write(split[n]);
}
}
else
{
if (NameForAnonymousUser != String.Empty)
{
writer.Write(", ");
writer.Write(NameForAnonymousUser);
}
else
{
writer.Write("!");
}
}
}
}
}
}
Code Discussion
If your control renders a user interface (UI) element or any other visible element on the client, you should derive your control from System.Web.UI.WebControls..::.WebControl (or a derived class). If your control renders an element that is not visible in the browser, such as a hidden element or a meta element, derive your control from System.Web.UI..::.Control. The WebControl class derives from Control and adds style-related properties such as Font, ForeColor, and BackColor. In addition, a control that derives from WebControl participates in the themes features of ASP.NET without any extra work on your part.
If your control extends the functionality of an existing control, such as the Button, Label, or Image controls, you can derive from that control.
From Label, WelcomeLabel inherits the Text property. WelcomeLabel overrides the RenderContents method to provide the logic for creating the text to display. The parameter passed into the RenderContents method is an object of type HtmlTextWriter, which is a utility class that has methods for rendering tags and other HTML (and HTML-variant) markup.
The WelcomeLabel uses view state to store the value for the NameForAnonymousUser property. Using view state persists the value of NameForAnonymousUser across postbacks. On each postback, the page is recreated and the value is restored from view state. If the value was not stored in view state, the value would be set to its default, Empty, on each postback. The ViewState property inherited from WebControl is a dictionary that saves data values. Values are entered and retrieved by using a String key. In this case, "NameForAnonymousUser" is used as the key. Items in the dictionary are typed as Object, which you must then cast to the property type. For more information, see ASP.NET State Management Overview.
Notice that WelcomeLabel makes successive calls to the Write method of the HtmlTextWriter object, instead of performing string concatenation and then invoking the Write method. This improves performance because the HtmlTextWriter object writes directly to the output stream. String concatenation requires time and memory to create the string, and then writes to the stream. When you implement rendering in your controls, you should follow the pattern illustrated in this walkthrough.
The attributes applied to WelcomeLabel contain metadata that is used by the common language runtime and by design-time tools. At the class level, WelcomeLabel is marked with the following attributes:
•AspNetHostingPermissionAttribute is a code-access security attribute. It causes the just-in-time (JIT) compiler to check that code that links to WelcomeLabel is granted the AspNetHostingPermission permission. All public ASP.NET classes are marked with this attribute. You should apply AspNetHostingPermissionAttribute to your controls as a security check against partially trusted callers. For more information, see Code Access Security. For more information about the JIT compiler, see Compiling MSIL to Native Code.
•DefaultPropertyAttribute is a design-time attribute that specifies the default property of a control. In visual designers, the property browser typically highlights the default property when a page developer clicks the control on the design surface.
•ToolboxDataAttribute specifies the format string for the element. The string becomes the control's markup when the control is double-clicked in the toolbox or dragged from the toolbox onto the design surface. For WelcomeLabel, the attribute creates this element:
The WelcomeLabel control also inherits two attributes from the WebControl base class, ParseChildrenAttribute and PersistChildrenAttribute. They are applied as ParseChildren(true) and PersistChildren(false). These two attributes work together so that child elements are interpreted as properties and properties are persisted as attributes.
The following attributes applied to the NameForAnonymousUser property of WelcomeLabel are standard design-time attributes that you will generally apply to public properties of your controls:
•BindableAttribute, specified as true or false, specifies for visual designers whether it is meaningful to bind the property to data. For example, in Visual Studio, if a property is marked with Bindable(true), the property is displayed in the DataBindings dialog box. If a property is not marked with this attribute, the property browser infers the value to be Bindable(false).
•CategoryAttribute specifies how to categorize the property in the visual designer's property browser. For example, Category("Appearance") tells the property browser to display the property in the Appearance category when the page developer uses the category view of the property browser. You can specify a string argument corresponding to an existing category in the property browser or create your own category.
•DescriptionAttribute specifies a brief description of the property. In Visual Studio, the property browser displays the description of the selected property at the bottom of the Properties window.
•DefaultValueAttribute specifies a default value for the property. This value should be the same as the default value that you return from the property accessor (getter). In Visual Studio, the DefaultValueAttribute allows a page developer to reset a property value to its default by displaying the shortcut menu in the Properties window and clicking the Reset button.
•LocalizableAttribute, specified as true or false, specifies for visual designers whether it is meaningful to localize the property. When a property is marked Localizable(true), the visual designer includes it when localized resources are being serialized. The designer will persist the property value to the culture-neutral resource file or other localization source when the control is polled for localizable properties.
To add a tag prefix mapping in a Web page
•In the Default.aspx page, add the following code after the @ Page directive:
[C#]
Copy Code
<%@ Register TagPrefix="aspSample"
Namespace="Samples.AspNet.CS.Controls"%>
[Visual Basic]
Copy Code
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls"%>
Creating a Page to Use the Control
You can now use the custom control in an ASP.NET Web page.
To create a page that uses the custom control
1.Copy the following markup into the Default.aspx file and save the file.
Visual Basic
Copy Code
C#
Copy Code
2.View Default.aspx in your browser.
In addition to the WelcomeLabel control's Text property that was explicitly defined in the markup that you copied, you can see from the control instance in the page that it has BackColor and ForeColor properties that you did not define. The WelcomeLabel control gets these and other style-related properties by inheritance from the WebControl base class. In addition, WelcomeLabel can be assigned a skin and be part of a theme without any work on your part.
Compiling the Control into an Assembly
The App_Code directory enables you to test your control without compiling it. However, if you want to distribute your control as object code to other developers, you must compile it. In addition, a control cannot be added to the toolbox of a visual designer unless it is compiled into an assembly.
You can provide a default tag prefix that a visual designer should use for your control by including the assembly-level System.Web.UI..::.TagPrefixAttribute attribute. The tag prefix is registered with the page the first time the control is double-clicked in the toolbox or dragged from the toolbox onto the page.
If you decide to use the TagPrefixAttribute attribute, you can specify it in a separate file that is compiled with your controls. By convention, the file is named AssemblyInfo.languageExtension, such as AssemblyInfo.cs or AssembyInfo.vb. The following procedure describes how to specify the TagPrefixAttribute metadata.
To prepare the control for compiling and provide a default tag prefix
1. Create a new directory named CustomControls that is not in the Web site.
2. Create a file that is named AssemblyInfo.cs or AssemblyInfo.vb in the directory and add the following code to the file.
C#
Copy Code
using System;
using System.Web.UI;
[assembly: TagPrefix("Samples.AspNet.CS.Controls", "aspSample")]
Visual Basic
Copy Code
Imports System
Imports System.Web.UI
The TagPrefix attribute creates a mapping between the namespace Samples.AspNet.CS.Controls or Samples.AspNet.VB.Controls and the prefix aspSample.
3. Copy the WelcomeLabel.cs or WelcomeLabel.vb file from App_Code to the CustomControls directory.
4. Delete the WelcomeLabel.cs or WelcomeLabel.vb file in the App_Code folder of the Web site.
If you do not delete the source files from the App_Code folder, your control's type will occur in both the compiled assembly and in the dynamically generated assembly created by ASP.NET. This will create an ambiguous reference when your control is loaded, and any page in which the control is used will generate a compiler error.
A visual designer such as Visual Studio typically uses a default icon (such as an image of a gear) to display a control in the toolbox. As an option for your control, you can customize the appearance of your control in the toolbox by embedding a 16-by-16-pixel bitmap in your control's assembly. By convention, visual designers use the lowermost left pixel of the bitmap as the transparent color.
To create a toolbox icon
1. Create or obtain a 16-by-16-pixel bitmap as the toolbox icon for your control.
2. Name the bitmap WelcomeLabel.bmp.
3. Add the bitmap file to the CustomControls directory where you have the source files for the WelcomeLabel control.
When compiling the code, you embed the bitmap as a resource in the assembly.
To compile the control into an assembly and embed the icon
1. Open the Visual Studio Command Prompt window. For more information, see Command-line Building With csc.exe.
2. At the command line, switch to the directory that contains the custom control class files.
3. Run the following command.
C#
Copy Code
csc /res:WelcomeLabel.bmp,Samples.AspNet.CS.Controls.WelcomeLabel.bmp /t:library /out:Samples.AspNet.CS.Controls.dll /r:System.dll /r:System.Web.dll *.cs
Visual Basic
Copy Code
vbc /res:WelcomeLabel.bmp,Samples.AspNet.VB.Controls.WelcomeLabel.bmp /t:library /out:Samples.AspNet.VB.Controls.dll /r:System.dll /r:System.Web.dll *.vb
The /res compiler option embeds a resource in the assembly. In this example, the bitmap is embedded as a resource. You specify a name for the resource in the second parameter of the /res option. You must name the embedded bitmap resource the same as the namespace-qualified name of the control with which it is associated. For example, if the name of the control is Samples.AspNet.CS.Controls.WelcomeLabel, the name of the embedded bitmap must be Samples.AspNet.CS.Controls.WelcomeLabel.bmp.
This naming convention causes a visual designer to automatically use the bitmap as your control's toolbox icon. If you do not use the naming convention, you must apply the ToolboxBitmapAttribute to the control to specify the name of the embedded bitmap resource.
The /t:library compiler option tells the compiler to create a library instead of an executable assembly. The /out option provides a name for the assembly and the /r option lists the assemblies that are linked to your assembly.
To keep the example self-contained, this walkthrough asks you to create an assembly with a single control. In general, the .NET Framework design guidelines recommend that you do not create assemblies that contain only a few classes. For ease of deployment, you should create as few assemblies as possible.
4. When you have finished compiling the class files, close the Command line window.
Using the Compiled Custom Control in an ASP.NET Page
To test the compiled version of your custom control, you must make your control's assembly available to pages in the Web site.
To make your control's assembly available to the Web site and add it to the toolbox
1. Return to Visual Studio.
2. Create a Bin directory under the root of the Web site.
3. Right-click the Bin directory and select Add Existing Item.
4. Navigate to the directory that contains the control assembly (Samples.AspNet.CS.Controls.dll or Samples.AspNet.VB.Controls.dll) and select the assembly.
5. Right-click anywhere in the Toolbox and then click Add Tab.
6. Name the tab "CustomControl".
7. Right-click the CustomControl tab and then click Choose Items.
8. Click Browse and select the assembly that you copied to the Bin folder.
8. The WelcomeLabel control now appears in the Toolbox with the icon you created.
The assembly that you created in this walkthrough is referred to as a private assembly because it must be included in an ASP.NET Web site's Bin directory to enable pages in the Web site to use your control. The assembly cannot be accessed from other applications unless a copy is also installed with those applications. If you are creating controls for shared Web hosting applications, you will typically package your controls in a private assembly. However, if you create controls for use in a dedicated hosting environment or you create a suite of controls that an ISP makes available to all its customers, you might have to package your controls in a shared (strongly named) assembly that is installed in the global assembly cache. For more information, see Working with Assemblies and the Global Assembly Cache.
The custom control is now available from the Toolbox.
To use the custom control from the Toolbox
1. Delete Default.aspx to remove any code related to the custom control from the previous procedures.
2. Create a new Web page called Default2.aspx.
3. In Design view, drag the WelcomeLabel control from the Toolbox onto Default2.aspx.
4. Set the Text property to "Hello".
5. In Source view, notice that the @ Register directive was automatically added. The assembly attribute is set because the control no longer is in the App_Code folder.
6. View Default.aspx in a Web browser.
Monday, May 11, 2009
Subscribe to:
Comments (Atom)





