About Me

My photo
Northglenn, Colorado, United States
I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).

Thursday, August 31, 2006

Set Day of the Month

Ok, had to quickly write this one, to find the nth day of a month.

private System.DateTime SetDayOfMonth(System.Int32 nth, System.DayOfWeek weekday, System.Int32 month, System.Int32 year)
{
System.DateTime dath = new DateTime(year, month, 1); //set to first day of the month
//get to the first weekday looking for
while(dath.DayOfWeek != weekday)
{
dath = dath.AddDays(1);
}

if(nth > 1)
{
dath = dath.AddDays((nth - 1) * 7);
}
else
{
dath = dath.AddMonths(1);
dath = dath.Substract(TimeSpan.FromDays(1); //last day of the month
while(dath.DayOfWeek != weekday)
{
dath = dath.Subtract(TimeSpan.FromDays(1)); //cycle backwards to last
}
dath = dath.Subtract(TimeSpan.FromDays(((nth * -1) - 1) * 7));
}
return dath;
}

Thursday, July 20, 2006

Grid View Problem

Well recently I've been trying to get my grid view to be specific, since their is a column I didn't want showing up. The problem that surfaced was that the first column name would never show up, and the second column name would then be placed over the first. I have looked for solutions, where people might have had problems with BoundFields, but found nothing. So, I decided to do another solution, which works in this case.

I take the data set I was using and make a copy of it, remove the column I didn't want, and then add the selectable column.

Tuesday, July 11, 2006

String.Format

Source: http://idunno.org/displayBlog.aspx/2004071401

This is a cool and very useful feature in C#. If you need to convert something like a date value from a sql database and display it into a textbox, then this is what you were looking for, before, it would always put the time 12:00 AM in after the date.

Example:

DateOnly = new DateTime();
DateOnly = (DateTime)dr["DATE_APPROVED"];
this.TextBoxDate.Text = String.Format("{0:d}", DateOnly);

Here are some of it's formats and conversions

Formating Strings:
exampleoutput
String.Format("--{1,10}--", "test");-- test--
String.Format("--{1,-10}--", "test");--test --

Formating Numbers:
specifiertypeformatoutput
(double 1.2345)
output
(int -12345)
ccurrency{0:c}£1.23-£12,345.00
ddecimal
(whole number)
{0:d}System.FormatException-12345
eexponent / scientific{0:e}1.234500e+000-1.234500e+004
ffixed point{0:f}1.23-12345.00
ggeneral{0:g}1.2345-12345
nnumber{0:n}1.23-12,345.00
rround trippable{0:r}1.23System.FormatException
xhexadecimal{0:x4}System.FormatExceptionffffcfc7

Custom Formatting
specifiertypeformatoutput
(double 1234.56)
0zero placeholder{0:00.000}1234.560
#digit placeholder{0:#.##}1234.56
.decimal point placeholder{0:0.0}1234.6
,thousand separator{0:0,0}1,235
%percentage{0:0%}123456%


Date-Time
specifiertypeformatoutput
(double 1234.56)
0zero placeholder{0:00.000}1234.560
#digit placeholder{0:#.##}1234.56
.decimal point placeholder{0:0.0}1234.6
,thousand separator{0:0,0}1,235
%percentage{0:0%}123456%

specifiertypeoutput
(June 8, 1970 12:30:59)
dShort Date08/06/1970
DLong Date08 June 1970
tShort Time12:30
TLong Time12:30:59
fFull date and time08 June 1970 12:30
FFull date and time (long)08 June 1970 12:30:59
gDefault date and time08/06/1970 12:30
GDefault date and time (long)08/06/1970 12:30:59
MDay / Month8 June
rRFC1123 date stringMon, 08 Jun 1970 12:30:59 GMT
sSortable date/time1970-06-08T12:30:59
uUniversal time, local timezone1970-06-08 12:30:59Z
YMonth / YearJune 1970


specifiertypeoutput
(June 8, 1970 12:30:59)
ddDay08
dddShort Day NameMon
ddddFull Day NameMonday
hh2 digit hour12
HH2 digit hour (24 hour)12
mm2 digit minute30
MMMonth06
MMMShort Month nameJun
MMMMMonth nameJune
ssseconds59
ttAM/PMPM
yy2 digit year70
yyyy4 digit year1970
:seperator, e.g. {0:hh:mm:ss}12:30:59
/seperator, e.g. {0:dd/mm/yyyy}08/06/1970

Monday, July 10, 2006

Convergence theory for nonconvex stochastic programming with an application to mixed logit

http://www.springerlink.com/(gy1xnqenspq0bc55yyftgd55)/app/home/contribution.asp?referrer=parent&backto=issue,1,18;journal,2,146;linkingpublicationresults,1:103081,1

Abstract Monte Carlo methods have extensively been used and studied in the area of stochastic programming. Their convergence properties typically consider global minimizers or first-order critical points of the sample average approximation (SAA) problems and minimizers of the true problem, and show that the former converge to the latter for increasing sample size. However, the assumption of global minimization essentially restricts the scope of these results to convex problems. We review and extend these results in two directions: we allow for local SAA minimizers of possibly nonconvex problems and prove, under suitable conditions, almost sure convergence of local second-order solutions of the SAA problem to second-order critical points of the true problem. We also apply this new theory to the estimation of mixed logit models for discrete choice analysis. New useful convergence properties are derived in this context, both for the constrained and unconstrained cases, and associated estimates of the simulation bias and variance are proposed.

Wednesday, July 05, 2006

On change event in ASP.Net

In ASP.Net the ontextchanged event doesn't get called until a postback, the problem we had was to ask the user before leaving the page, if they would like to save any of their changes that were made, and to enable the submit button if there were changes. We had to go to javascript, again, for this answer.

<script language="javascript" type="text/javascript">
window.onbeforeunload = saveChanges;
function saveChanges()
{
var changes = document.getElementById("<%=this.ButtonSubmit.ClientID%>").disabled;
if(!changes)
{
event.returnValue = 'Changes were not saved. Click Cancel to go back and submit changes, or click OK to continue.';
}
}

function enableSubmit()
{
document.getElementById("<%=this.ButtonSubmit.ClientID%>").disabled = false;
}
</script>


The first function: saveChanges() is called when ever the user tries to change the current page or close the browser. It just asks if they user wants to leave without saving.

The second function is called when ever a textbox, or other asp/html control has changed. This basically just enables the submit button.

So, within the page we might have a ASP.Net control like this:

<asp:textbox id="TextBox1" runat="server" onchange="enableSubmit()" width="49px"><asp:textbox>


Now, you would end up getting a warning in ASP.Net since it doesn't recognize the onchange event, since this is actually a javascript event. The trick here is when the page is render this is how it would look in the html:

<input id="ctl00_ContentPlaceHolder1_TextBox1" style="WIDTH: 49px" onchange="enableSubmit()" name="ctl00$ContentPlaceHolder1$TextBoxNotes">

Monday, June 26, 2006

Riddle: CRIMINAL CUPBEARERS

Source: http://www.ocf.berkeley.edu/~wwu/riddles/hard.shtml

An evil king has 1000 bottles of wine. A neighboring queen plots to kill the bad king, and sends a servant to poison the wine. The king's guards catch the servant after he has only poisoned one bottle. The guards don't know which bottle was poisoned, but they do know that the poison is so potent that even if it was diluted 1,000,000 times, it would still be fatal. Furthermore, the effects of the poison take one month to surface. The king decides he will get some of his prisoners in his vast dungeons to drink the wine. Rather than using 1000 prisoners each assigned to a particular bottle, this king knows that he needs to murder no more than 10 prisoners to figure out what bottle is poisoned, and will still be able to drink the rest of the wine in 5 weeks time. How does he pull this off?


First thing I noticed was that 2^10 is approx 1000 and saw that this dealt with some sort of binary solution. I then broke the question down into a smaller one. Dealing with 8 bottles of wine and 3 prisioners, assuming that the 2^10 has something to do with the solution.

So, I came up with this basic solution:

Bottle 1: Prisoner 1 drinks
Bottle 2: Prisoner 2 drinks
Bottle 3: Prisoner 3 drinks

Bottle 4: Prisoner 1 & 2 drinks
Bottle 5: Prisoner 1 & 3 drinks
Bottle 6: Prisoner 2 & 3 drinks

Bottle 7: Prisoner 1 & 2 & 3 drinks

Bottle 8: No one drinks.

So for example if prisoner 1 & 3 dies, then you know it must be bottle 5 that was poisoned.

I guess this could be looked at as some type of Venn diagram, and/or combinatorics. I don't know if this solution would work for 1000 bottles and 10 prisoners.

Wednesday, June 21, 2006

How to redirect a page, the right way.

Good information to keep in mind for updating websites, using the 301.
http://www.stevenhargrove.com/redirect-web-pages/


Since, I'm developing mostly in ASP.Net, here is his sample code on how to use it.


<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.new-url.com/");
}
</script>

Algorithms

Here is a good source of Algorithms that are most commonly used in computer science. http://www.cse.ucsd.edu/~dasgupta/mcgrawhill/

I could probably add a couple, but I haven't look through the whole thing. I believe he is making this a book, but I believe their is already an algorithm book out there. Anyway, check it out, looks like a good find.

Friday, June 09, 2006

Way to execute a file through an ASP.Net page.

Very simple and quick:

System.Diagnostics.Process.Start(LocationOfString);

Friday, June 02, 2006

Multiple Control Validator

Ok, so I have to come up with a validation control that would validate two textboxes to see if either one has been populated. The custom validator, unfortuntly, only validates if something has been entered into one of the textboxes. To get around this I set up two private boolean values to represent if text has been entered or not.

private bool TxtBox1 = false;
private bool TxtBox2 = false;

I then have my two custom validator functions, which are similar, except for the name of the second validator and it's TxtBox2:

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (args.Value == "")
{
TxtBox1 = false;
}
else
{
TxtBox1 = true;
}
args.IsValid = getValidation();
}

So, I just check if there was a value entered and set my private variables.
I then call a function to assign the validation.

private bool getValidation()
{
if (TxtBox1 TxtBox2)
{
return true;
}
else
{
return false;
}
}

Now to deal with the textboxes being both empt

protected void ButtonSubmit_Click(object sender, EventArgs e)
{
this.Page.Validate();
if (TxtBox1 == false && TxtBox2 == false)
{
this.CustomValidator1.IsValid = false;
this.CustomValidator2.IsValid = false;
}
}

First validate the other controls, then validate our custom controls to see if both were left blank.
If so, then set both validators to false.

This is what I came up with so far, I might find a way to improve the summary, so that it would only give one line as the error instead of 2.