Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » General » Downloads
 
Add your own
alternative version

Universal Database Admin for ASP.NET and SQL Server (Reloaded)

, 15 Apr 2006
A dynamic web application needs an admin section for CRUD actions on the records/tables in the database. Wouldn't it be nice to have a database admin, which can be plugged to any web application? Just give your SQL connection string and it dynamically manages all table operations in just five pages.
admin.zip
Admin
Admin.csproj.webinfo
bin
Admin.dll
classes
Global.asax
images
Admin.gif
admindb.jpg
admindetails.jpg
CreateTable.jpg
custom.jpg
dataAccess.jpg
Delete.jpg
DeleteTable.JPG
Edit.jpg
Editfield.jpg
InAction.jpg
login.jpg
placeholder-100.jpg
placeholder-200.jpg
placeholder-600.jpg
readtable.jpg
TableDetails.jpg
Tables.jpg
View.jpg
includes
screen1.bmp
SQLI.jpg
admin2005.zip
Admin2005
App_Code
ClassDiagram.cd
classes
Migrated
bin
Admin.dll
classes
Global.asax
images
Admin.gif
admindb.jpg
admindetails.jpg
CreateTable.jpg
custom.jpg
dataAccess.jpg
Delete.jpg
DeleteTable.JPG
Edit.jpg
Editfield.jpg
InAction.jpg
login.jpg
placeholder-100.jpg
placeholder-200.jpg
placeholder-600.jpg
readtable.jpg
TableDetails.jpg
Tables.jpg
View.jpg
includes
screen1.bmp
SQLI.jpg
adminlite.zip
AdminLite
AdminLite.csproj.webinfo
bin
AdminLite.dll
classes
Global.asax
images
Admin.gif
admindb.jpg
admindetails.jpg
clear.gif
CreateTable.jpg
custom.jpg
dataAccess.jpg
Delete.jpg
deleterow.gif
DeleteTable.JPG
edit.gif
Edit.jpg
Editfield.jpg
InAction.jpg
Login.jpg
placeholder-100.jpg
placeholder-200.jpg
placeholder-600.jpg
readtable.jpg
TableDetails.JPG
tables.jpg
View.jpg
includes
admin_src.zip
Admin_Src
Admin.csproj.webinfo
bin
Admin.dll
Admin.pdb
Global.asax
images
Admin.gif
admindb.jpg
admindetails.jpg
dataAccess.jpg
Delete.jpg
Edit.jpg
InAction.jpg
Login.jpg
TableDetails.JPG
tables.jpg
View.jpg
includes
dbadmin_src.zip
dbadmin
images
add.gif
btn_check_update.gif
btn_databases.gif
btn_dbadmin_hp.gif
btn_ftquery.gif
btn_home.gif
btn_procedures.gif
btn_relations.gif
btn_stpworks.gif
btn_tables.gif
btn_views.gif
check.gif
cycle.gif
DBAdmin_logo.gif
delete.gif
edit.gif
excel.gif
folder.gif
ftquery.gif
icon_hide.gif
icon_logoff.gif
icon_settings.gif
icon_show.gif
icon_submit_bug.gif
key.gif
link.gif
linked.gif
logo.jpg
msaccess.gif
query.gif
refresh.gif
rename.gif
run.gif
spacer.gif
structure.gif
table.gif
tables.gif
table_shadow_bottom.gif
table_shadow_corner.gif
table_shadow_right.gif
table_title_center.gif
table_title_left.gif
table_title_right.gif
un_key.gif
view.gif
xml.gif
languages
dbadmin_en.lang
plugins
htmleditor
aleft.gif
aright.gif
blist.gif
bold.gif
center.gif
copy.gif
cut.gif
delete.gif
help.gif
ileft.gif
image.gif
instable.gif
iright.gif
italic.gif
nlist.gif
parea.gif
paste.gif
redo.gif
tbdown.gif
tpaint.gif
under.gif
undo.gif
wlink.gif
scripts
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/********************************************************************
	Please keep the comments if you distribute
        Author :        Quartz (Rajesh Lal - connectrajesh@hotmail.com)
	created:	18:9:2004   5:55
	created:	11/11/2005
	file base:	db.aspx
	purpose:	list all the records in the table
*********************************************************************/

namespace Admin
{
	/// <summary>
	/// Summary description for Template.
	/// </summary>
	public class DB : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid DGtable;
		DataTable TempTable;
		DataView TempTableView;
		DataSet mydr;
		protected System.Web.UI.WebControls.Button Button1;
		//protected System.Web.UI.WebControls.Label lblstatus;
		protected System.Web.UI.WebControls.Button Button2;
		protected System.Web.UI.WebControls.TextBox txtSQL;
		public string SortField;
		public string sQuery = "";
		protected System.Web.UI.WebControls.Label lblSQLError;
		public bool userQuery = false;
		protected int ImageColumnNo =0;
		protected string ImageColumnName ="Image";
		protected ArrayList ImageColumnNoAndNameList = new ArrayList();



	
		private void Page_Load(object sender, System.EventArgs e)
		{
			Session["tName"] = Request.QueryString["table"].ToString().Trim();
			Session["qry"] = "Select * from " + Session["tName"];
			getImageColumnNumberAndName();
				
			if (!IsPostBack) 
			{
				lblSQLError.Text = "<font face = 'courier new' size = 2>Enter your SQL query above (<a href='http://www.w3schools.com/sql/sql_where.asp' target = '_blank' >Learn SQL query ^</a>)</font>";
				BindDataGrid();
			} 
			else
			{
				try
				{
					Session["qry"] = txtSQL.Text  ;
					BindDataGrid();
					lblSQLError.Text = "<font face = 'courier new' size = 2>Enter your SQL query above (<a href='http://www.w3schools.com/sql/sql_where.asp' target = '_blank' >Learn SQL query ^</a>)</font>";
				}
				catch(Exception e1)
				{
					Session["qry"] = "Select * from " + Session["tName"];
					txtSQL.Text = "Select * from " + Session["tName"];
					BindDataGrid();
					lblSQLError.Text = "<font face = 'courier new' size = 2><a href='http://www.w3schools.com/sql/sql_where.asp' target = '_blank' >Error:" + e1.Message + " </a></font>";
				}
			}
		
			//lblstatus.Text = "Results of the query: [<b>" + txtSQL.Text + "</b>]";
			
		}
		private void getImageColumnNumberAndName()
		{
			ImageColumnNoAndNameList.Clear(); 
			//int retValue=0;
			string s="";
			//	System.Web.UI.WebControls.Image myImage ;
			
			SqlDataReader myReader;
			int j=0;
				
			//i = Convert.ToInt32(Request.QueryString["id"]); 
			s = "SELECT * FROM " + Session["tName"].ToString() ; 

			clsDataAccess myclass = new clsDataAccess();
			myclass.openConnection();
			myReader = myclass.getData(s);
			
			while (myReader.Read()&&(j<1) )
			{
				for(j=1; j<myReader.FieldCount;j++) 
				{
					if (String.Compare(myReader.GetDataTypeName(j).ToString().ToLower() ,"image")==0)
					{
						Photo p = new Photo(j,j,myReader.GetName(j).ToString());
						ImageColumnNoAndNameList.Add(p);  
					}
				}
				j++;
			}
		}
		

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Button2.Click += new System.EventHandler(this.Button2_Click);
			this.DGtable.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DGtable_PageIndexChanged);
			this.DGtable.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DGtable_SortCommand);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
		private string GetFirstKeys()
		{
			string returnvalue = "Node";
			DataTable dt = new DataTable();
			
			string query;
			query = "Select * from " + Request.QueryString["table"].ToString().Trim()   ;
				
			clsDataAccess myclass = new clsDataAccess();
			myclass.openConnection();
			mydr = myclass.getDatabyPaging(query);
			returnvalue = mydr.Tables[0].Columns[0].Caption;  
			mydr.Clear();
			return returnvalue;
		}
		private void DGtable_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			DGtable.CurrentPageIndex=e.NewPageIndex;
			getImageColumnNumberAndName();
			BindDataGrid();
		}
		private void BindDataGrid()
		{
			DGtable.Columns.Clear();  
			Session["FirstKey"] = GetFirstKeys();
			HyperLinkColumn urlView = new HyperLinkColumn();
			urlView.Text = "View";
			urlView.DataNavigateUrlField = Session["FirstKey"].ToString() ;
			urlView.HeaderText = "View";
			urlView.DataNavigateUrlFormatString = "Viewdata.aspx?id={0}"; 

			HyperLinkColumn urlEdit = new HyperLinkColumn();
			urlEdit.Text = "Edit";
			urlEdit.DataNavigateUrlField = Session["FirstKey"].ToString() ;
			urlEdit.HeaderText = "Edit";
			urlEdit.DataNavigateUrlFormatString = "Editdata.aspx?id={0}";

			HyperLinkColumn urlDelete= new HyperLinkColumn();
			urlDelete.Text = "Delete";
			urlDelete.DataNavigateUrlField = Session["FirstKey"].ToString() ;
			urlDelete.HeaderText = "Delete";
			urlDelete.DataNavigateUrlFormatString = "Deletedata.aspx?id={0}";

			//Response.End();
			HyperLinkColumn[] urlImages = new HyperLinkColumn[ImageColumnNoAndNameList.Count];
				
			for (int i=0;i<ImageColumnNoAndNameList.Count;i++)
			{
				Photo p = (Photo)ImageColumnNoAndNameList[i];

				urlImages[i] = new HyperLinkColumn(); 
				urlImages[i].HeaderText= p.Caption;
				urlImages[i].DataNavigateUrlField = Session["FirstKey"].ToString() ;
				urlImages[i].DataNavigateUrlFormatString = "image.aspx?id={0}&cnt=" + p.PhotoID;
				urlImages[i].Target = "_blank";
				urlImages[i].DataTextField = Session["FirstKey"].ToString() ;
				urlImages[i].DataTextFormatString ="<img border = '0' src='image.aspx?id={0}&cnt=" + p.PhotoID + "' Width='75' >";
			}

//				
//			
//			// Add three columns to collection.
			DGtable.Columns.Add(urlView);
			DGtable.Columns.Add(urlEdit);
			DGtable.Columns.Add(urlDelete);
//			
			for (int i=0;i<ImageColumnNoAndNameList.Count;i++)
			{
				DGtable.Columns.Add(urlImages[i]);
			}
//			
			DGtable.DataSource = CreateDataSource(); 
			DGtable.DataBind();
		}

		private void DGtable_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			SortField = (string)e.SortExpression;
			Session["qry"] = "Select * from " + Session["tName"].ToString();
			txtSQL.Text =  Session["qry"].ToString() + " Order by " + SortField ;
			//lblstatus.Text = "Results of the query: [<b>" + txtSQL.Text + "</b>]";
			DGtable.CurrentPageIndex=0;
			getImageColumnNumberAndName();
			BindDataGrid();
			//txtSQL.Text =   txtSQL.Text + " SortField2:" + SortField;	

		}
		ICollection CreateDataSource() 
		{
			
			DataTable dt = new DataTable();
			Session["tName"] = Request.QueryString["table"].ToString().Trim();
			string query;

			query = Session["qry"].ToString() ;
			
			if (SortField != null)
				{
					if (SortField.Length > 0) 
					{
						txtSQL.Text =   query + " order by " + SortField;	
					}

				}
				else
				{
					txtSQL.Text =   Session["qry"].ToString()  ;
				}
			
			sQuery = txtSQL.Text;
		
			clsDataAccess myclass = new clsDataAccess();
			myclass.openConnection();
			mydr = myclass.getDatabyPaging(query);
			//'mydr.Tables[0].PrimaryKey  
			TempTable = new DataTable();
			TempTable = mydr.Tables[0];
			TempTableView = new DataView(TempTable);
			TempTableView.Sort = SortField;
			// Get the number of elements in the array.
			return TempTableView;
		}

		

		private void Button1_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("AddData.aspx"); 
		}

		private void Button2_Click(object sender, System.EventArgs e)
		{
				Session["qry"] = txtSQL.Text  ;
		}

		private void lblSQLError_Click(object sender, System.EventArgs e)
		{
		
		}
		
	}
	public class DataGridTemplate : ITemplate
	{
		ListItemType templateType;
		string columnName;
   
		public DataGridTemplate(ListItemType type, string colname)
		{
			templateType = type;
			columnName = colname;
		}

		public void InstantiateIn(System.Web.UI.Control container)
		{
			Literal lc = new Literal();
			System.Web.UI.WebControls.Image myImage = new System.Web.UI.WebControls.Image();

			switch(templateType)
			{
				case ListItemType.Header:
					lc.Text = "<B>" + columnName + "</B>";
					container.Controls.Add(lc);
					break;
				case ListItemType.Item:
					lc.Text ="<Img src='" + columnName + "{0}" + "&cnt=5'  Width='100' Height='75'>";
					container.Controls.Add(lc);
					break;
				case ListItemType.EditItem:
					TextBox tb = new TextBox();
					tb.Text = "";
					container.Controls.Add(tb);
					break;
				case ListItemType.Footer:
					lc.Text = "<I>" + columnName + "</I>";
					container.Controls.Add(lc);
					break;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author


| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 15 Apr 2006
Article Copyright 2005 by Raj Lal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid