Wednesday, February 8, 2012

SQL Update column name

exec sp_RENAME 'table.column', 'newName', 'COLUMN';

example:
exec sp_RENAME 'Employee.notes' , 'payNotes', 'COLUMN';

Caution: Changing any part of an object name could break scripts and stored procedures.

Saturday, January 7, 2012

Disable page to display message with Javascript (Lightbox)

Simple way to disable a page to display a message or request a specific user action
This html can be copied and pasted into an html file to view the css, javascript, and html in action.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<style type="text/css">
 .opaqueLayer
 {
  display:none;
  position:absolute;
  top:0px;
  left:0px;
  opacity:0.6;
  filter:alpha(opacity=60);
  background-color: #000000;
  z-Index:1000;
 }

 .questionLayer
 {
  position:absolute;
  top:0px;
  left:0px;
  width:350px;
  height:200px;
  display:none;
  z-Index:1001;
  border:2px solid black;
  background-color:#FFFFFF;
  text-align:center;
  vertical-align:middle;
  padding:10px;
 }
</style>
<script type="text/javascript">
 function getBrowserHeight() {
  var intH = 0;
  var intW = 0;

  if(typeof window.innerWidth == 'number' ) {
    intH = window.innerHeight;
    intW = window.innerWidth;
  }
  else if(document.documentElement &&   (document.documentElement.clientWidth ||   document.documentElement.clientHeight)) {
    intH = document.documentElement.clientHeight;
    intW = document.documentElement.clientWidth;
  }
  else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
    intH = document.body.clientHeight;
    intW = document.body.clientWidth;
  }

  return { width: parseInt(intW), height: parseInt(intH) };
}

function setLayerPosition() {
  var shadow = document.getElementById("shadow");
  var question = document.getElementById("question");

  var bws = getBrowserHeight();
  shadow.style.width = bws.width + "px";
  shadow.style.height = bws.height + "px";

  question.style.left = parseInt((bws.width - 350) / 2);
  question.style.top = parseInt((bws.height - 200) / 2);

  shadow = null;
  question = null;
}

function showLayer() {
  setLayerPosition();

  var shadow = document.getElementById("shadow");
  var question = document.getElementById("question");

  shadow.style.display = "block";
  question.style.display = "block";

  shadow = null;
  question = null;
}

function hideLayer() {
  var shadow = document.getElementById("shadow");
  var question = document.getElementById("question");

  shadow.style.display = "none";
  question.style.display = "none";

  shadow = null;
  question = null;
}

window.onresize = setLayerPosition;
</script>

</head>
<body>
  <div id="shadow" class="opaqueLayer"> </div>
  <div id="question" class="questionLayer">
  <br />
  <br />
  <br />
    Hello!
  <br />
  <br />
  <br />
  <input type="button" onclick="hideLayer();" value="Close" />
</div>

<h3>Modal Layer Test</h3>
<p>Click the image below to display the "modal" layer</p>
<img src="http://www.google.com/intl/en_ALL/images/logo.gif" alt="Test Image" onclick="showLayer();" />
</body>
</html>

Another option would be to add a control to the page that posts back to the server and then responds with the option to hide layer.

Button on .aspx page
<asp:Button runat="server" ID="UpdateBtn" OnClientClick="hideLayer();" Text="Update" CssClass="Btn" />

Button click event that checks condition
void UpdateBtn_Click(object sender, EventArgs e)
{
  if(SomeCondition){
    InsertAlert();
  }
}

Inserts showLayer script on window load
private void InsertAlert()
{
  ClientScript.RegisterClientScriptBlock(typeof(Page), "InsertAlert", "window.onload = showLayer;", true);
}

Tuesday, December 27, 2011

Creating a XmlDocument from a DataTable

A DataSet or DataTable is represented as XML, so it can be transformed into an XmlDocument.
This is a quick way to transform.

1.) Populate a DataTable:
  DataTable dt = GetDetailsFromDatabase();

2.) Use XmlWriter to write the DataTable Xml to a XmlDocument:

  XmlDocument doc = new XmlDocument();
  using (XmlWriter xw = doc.CreateNavigator().AppendChild())
  {
    xw.WriteStartDocument(true);
    dt.WriteXml(xw);
  }

3.) Now the XmlDocument can be used as is or saved:
  doc.Save("test.xml");

Friday, October 21, 2011

The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.

Working on an EventLog process, this error started. Below is a solution.
follow these steps:

1.Start -> Run -> regedit.exe
2.Navigate to My Computer > HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\EventLog
3.Right click this key, select Permissions, and grant the ASPNET account read/write permissions. Note that for the "inaccessible" logs (ie. Security, Virtual Server), you'll also need to grant read access, as permissions have been set to not inherity from the parent key.
Step 4 may be required: Restart IIS (start -> Run -> iisreset)

Friday, September 23, 2011

Save and load multiple bool values with a single decimal value.

The example below will explain how to store and load multiple bool values from a database with a single decimal value.


-----------------------------------------------------------------------
Save bool values as decimal
-----------------------------------------------------------------------

int options = 0;

options=options | Convert.ToInt32(Option1.Checked); //bitwise OR

options=options << 1; //Shift bits

options=options | Convert.ToInt32(Option2.Checked);

options=options << 1;

options=options | Convert.ToInt32(Option3.Checked);

options=options << 1;

options=options | Convert.ToInt32(Option4.Checked);

options=options << 1;

options=options | Convert.ToInt32(Option5.Checked);


DBService.SaveDetail(options);



-----------------------------------------------------------------------
Load decimal value from database
-----------------------------------------------------------------------

DataRow dr = DBService.GetDetail();

int values = int.Parse(dr["decimal"].ToString());


Load controls
Use binary operators to check each bit
Option1.Checked = ((bool)((options & 1) == 1)); //bitwise AND

options=options >> 1; //Shift bits

Option2.Checked = ((bool)((options & 1) == 1));

options=options >> 1;

Option3.Checked = ((bool)((options & 1) == 1));

options=options >> 1;

Option4.Checked = ((bool)((options & 1) == 1));

options=options >> 1;

Option5.Checked = ((bool)((options & 1) == 1));

Monday, September 12, 2011

Using GetEnumerator()

Testing ASCII values of user input

byte[] _asciiAnswer = Encoding.ASCII.GetBytes(AnswerTb.Text.Trim());

IEnumerator _asciiChars = _asciiAnswer.GetEnumerator();
while (_asciiChars.MoveNext())
{
  byte _char = (byte)_asciiChars.Current;
  if (_char >= 65 && _char <= 90)
    IsUpperCase = true;
}

Thursday, August 25, 2011

Syntax, C#

a ? b : c;
x = x ?? 5;

Set x to b if a is true, otherwise set x to c.
x = a ? b : c;

Set x to x if x is not null, otherwise initialize x.
x = x ?? new x();