Click here to Skip to main content
15,892,253 members
Articles / Web Development / IIS

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

Rate me:
Please Sign up or sign in to vote.
4.91/5 (87 votes)
15 Apr 20068 min read 440.9K   9.6K   200  
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.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Configuration; 
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Admin
{
	/// <summary>
	/// Summary description for Image.
	/// </summary>
	public class MyImage : System.Web.UI.Page
	{
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			// Put user code to initialize the page here
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
			SqlCommand myCommand = new SqlCommand("Select * from " + Session["tName"] + " where " + Session["FirstKey"].ToString() + "=" + Request.QueryString["id"], myConnection);
			try
			{
				myConnection.Open();
				SqlDataReader myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
				while (myDataReader.Read())
				{
					//Response.ContentType = (string)myDataReader.GetValue(6);
					Response.BinaryWrite((byte[])myDataReader.GetValue(Convert.ToInt32(Request.QueryString["cnt"])));
				}
            
				myConnection.Close();
				//Response.Write("Person info successfully retrieved!");
			}

			catch (SqlException SQLexc)
			{
				Response.Write("Read Failed : " + SQLexc.ToString());
			}
		}
		public byte[] GetImage(int id, int cnt)
		{
			byte[] imgBinary = null; 
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
			SqlCommand myCommand = new SqlCommand("Select * from " + Session["tName"] + " where " + Session["FirstKey"].ToString() + "=" + id , myConnection);
			
				myConnection.Open();
				SqlDataReader myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
				while (myDataReader.Read())
				{
					//Response.ContentType = (string)myDataReader.GetValue(6);
					 imgBinary = (byte[])myDataReader.GetValue(cnt);
				}
            	myConnection.Close();
		return imgBinary;
		}


		#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.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}

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


Written By
Founder Teamcal AI
United States United States

Comments and Discussions