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();

Wednesday, July 13, 2011

Get XML from RSS

To get the XML from an RSS feed, view the source of the page. Programmatically, the following can be used:
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);

httpReq.Method = @"GET";
httpReq.ContentType = @"text/xml";
httpReq.Timeout = 600000;
httpReq.KeepAlive = false;

HttpWebResponse resp = (HttpWebResponse)httpReq.GetResponse();
StreamReader srdr = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
string xmlstr = srdr.ReadToEnd();
resp.Close();

At this point, the XML is in the string xmlstr, this can then be loaded into an XML document or utilized as is.

Wednesday, May 11, 2011

RSS date sort with pubdate

Problems with RSS date sorting could be caused by the formatting of the date.
Use the format below:
<pubdate>startdate.ToString("o")</pubdate>

This will produce the format within the feed which will sort appropriately.
<pubdate>2011-04-28T00:00:00.0000000</pubdate>

Friday, April 8, 2011

Using hidden field to confirm status before postback

Declare in page
<input type="hidden" id="confirmed" name="confirmed" value="">

Set up javascript validation
function SelectBtn_Click(src, e)
{
  var f = document.MyForm;
  var v = f.NumberOfTxt.value;

    if (IsStringOfDigits(v) && confirm("Are you sure you want to   select " + v + "?\n If \"Yes\" click OK, otherwise click Cancel."))
  {
    document.MyForm.confirmed.value = "Y";
    event.returnValue = true;
    return true;
  }
  else
  {
    document.MyForm.confirmed.value = "";
    event.returnValue = false;
    return false;
  }
}

Set attribute in code behind
this.SelectBtn.Attributes["onclick"] = "return SelectBtn_Click(this, event);";

In postback, check the hidden value
"Y".Equals(Request.Form["confirmed"])