MIS 431 – ADO.NET
A couple bits of ADO.NET
I was about to start compiling a list of ASP.NET tags for reference, but the work has already been done!
http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx
Thanks, naspinski!
|
1 2 3 4 5 |
<% if (User.IsInRole("admin")) { %>
You can see this
<% } else { %>
You are no admin fool!
<%} %> |
|
1 2 |
The Date is now <%= DateTime.Now.ToShortDateString() %>
The value of string1 is <%= string1 %> |
|
1 2 3 4 5 6 |
<asp:Repeater ID="rptMeetings" DataSourceID="meetings"
runat="server">
<ItemTemplate>
<%# Eval("MeetingName")%>
</ItemTemplate>
</asp:Repeater> |
|
1 2 3 4 |
<asp:SqlDataSource ID="party" runat="server"
ConnectionString="<%$ ConnectionStrings:letsParty %>"
SelectCommand="SELECT * FROM [table]"
/> |
|
1 2 3 4 5 |
<asp:Label ID="lblAwesome" runat="server"/>
<%-- sometimes end users make me angry --%>
<asp:LinkButton ID="lbEdit" Text="Edit"
OnClick="Edit_Click" runat="server" /> |
|
1 |
<%: Html.TextBox("FirstName") %> |
This project includes all the features I demoed in class along with a custom SiteMapProvider and Server component tag (<isu:IsuRepeater …)
It also uses the “ADO.NET Entity Data Model” for managing persistence. Please see: http://msdn.microsoft.com/en-us/library/e80y5yhx.aspx
I’ll discuss ADO.NET for managing the data tier next Tuesday in class if time allows.
I wrote this as part of a small project and quickly realized it had other entertaining uses.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php
$str = "I like driving/flying/eating cars/aeroplanes/lunch";
$lines = array($str);
$results = array();
while ($line = array_shift($lines)) {
if (preg_match("/(\w+)(?:\/\w+)+/ui", $line, $match, PREG_OFFSET_CAPTURE)) {
list($string, $offset) = $match[1];
array_push($lines, sprintf("%s%s",
substr($line, 0, $offset + strlen($string)),
substr($line, $offset + strlen($match[0][0]))
));
array_push($lines, sprintf("%s%s",
substr($line, 0, $offset),
substr($line, $offset + strlen($string) + 1)
));
} else {
$results[] = $line;
}
}
print_r($results); |
This VB.NET method returns the total number of weekend days between two dates.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Public Function CountWeekendDays(ByVal FromDate As Date, ByVal ToDate As Date)
Dim TotalDaysInt As Integer = ToDate.Date.Subtract(FromDate.Date).Days
Dim WeeksInt As Integer = Math.Floor(TotalDaysInt / 7)
Dim WeekendDaysInt As Integer = WeeksInt * 2
If TotalDaysInt Mod 7 <> 0 Then
If FromDate.DayOfWeek = DayOfWeek.Sunday Then
WeekendDaysInt += 1
Else
Dim SaturdayOffsetInt = Math.Max((TotalDaysInt Mod 7) - (DayOfWeek.Saturday - FromDate.DayOfWeek), 0)
WeekendDaysInt += Math.Min(SaturdayOffsetInt, 2)
End If
End If
Return WeekendDaysInt
End Function |
It works by multiplying the number of full weeks by 2, then it takes the leftover days and calculates how many of those days overlap the weekend.
For example:
Sunday = 0, Monday = 1, Tuesday = 2 …(and so on)
Let’s say FromDate is Wednesday, and the total number of days to ToDate is 18:
FromDate = Wednesday = 3
TotalDays = 18
TotalWeeks = Math.Floor(18 / 7) = 2
LeftoverDays = (TotalDays Mod 7) = (TodalDays – (TotalWeeks * 7)) = 4
DaysUntilSaturday = (Saturday – CheckInDate) = (6 – 3) = 3
DaysThatOverlapSaturday = (LeftoverDays – DaysUntilSaturday) = (4 – 3) = 1
Saturday is overlapped by 1 day, so we add 1 to the total number of weekend days.
WeekendDays = ((TotalWeeks * 2) + DaysThatOverlapSaturday) = (4 + 1) = 5
Of course if DaysThatOverlapSaturday < 1, don’t add anything.
If TotalDays = 19, then there would be 2 days that overlap Saturday. Since we know Sunday is the next day, we add both days.
If TotalDays = 20, then there would be 3 days that overlap Saturday, but that third day is Monday, so we only add 2.
If TotalDays = 21, then that’s 3 full weeks, and we know that 3 full weeks will always contain 6 weekend nights, so there’s nothing more to do.
The only other case is if the total number of days aren’t divisible by 7 and the Check-In date is Sunday. Since we know the remaining number of days are less then 7, we only have to add 1 for that Sunday.
Here’s my first attempt at a Go board with javascript and the canvas tag.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
<!DOCTYPE html>
<html>
<head>
<title>Simple Go board with canvas tag</title>
<script language="JavaScript" type="text/javascript">
<!-- //
var Go = {
nextMove : 'black'
};
Go.board = {
size : 19, // breaks if > 25
margin : 30,
bgColor : '#FFA54F'
}
Go.apply = function(o, c){
if(o && c && typeof c == 'object'){
for(var prop in c){
o[prop] = c[prop];
}
}
return o;
};
Go.apply(Go.board, {
xLabel : 'abcdefghjklmnopqrstuvwxyz',
map : new Array(),
stoneWidth : 0,
stoneHeight : 0,
getStarPoints : function(){
// I'd like an algorithm for this
var result = new Array();
switch (this.size){
case 19:
result.push([4,4],[10,4],[16,4],[4,10],[10,10],[16,10],[4,16],[10,16],[16,16]);
break;
case 13:
result.push([4,4],[10,4],[7,7],[4,10],[10,10]);
break;
case 9:
result.push([3,3],[7,3],[5,5],[3,7],[7,7]);
break;
}
return result;
},
getCoords : function(x,y){
return this.map[x][y];
},
buildMap : function(){
var m = (this.margin||0);
var hLineGap = (Go.canvas.height-(m*2))/(this.size-1);
var vLineGap = (Go.canvas.width-(m*2))/(this.size-1);
var x, y, lat, lon;
// a convenient moment to determine stone size.
this.stoneWidth = vLineGap/2;
this.stoneHeight = hLineGap/2;
for (lon=0; lon<this.size; lon++){
x = (lon*vLineGap)+m;
this.map[lon+1] = [];
for (lat=0; lat<this.size; lat++){
y = (lat*hLineGap)+m;
this.map[lon+1][lat+1] = [x,y];
}
}
},
drawBackground : function(){
var c = Go.cxt;
c.fillStyle = this.bgColor;
c.fillRect(0,0,Go.canvas.width, Go.canvas.height);
},
drawStone : function(x,y,color){
var coords = this.getCoords(x,y);
if (undefined==coords){return false;}
var c = Go.cxt;
if (!color){
color = Go.nextMove;
Go.nextMove = (color=='black')?'white':'black';
}
c.beginPath();
c.arc(coords[0],coords[1],this.stoneWidth,0,Math.PI*2,true);
c.fillStyle = color;
c.fill();
c.stroke();
},
drawStar : function(x,y){
var coords = this.getCoords(x,y);
if (undefined==coords){return false;}
var c = Go.cxt;
c.beginPath();
c.arc(coords[0],coords[1],4,0,Math.PI*2,true);
c.fillStyle = 'black';
c.fill();
c.stroke();
},
drawStars : function(){
var stars = this.getStarPoints();
for (var i=0; i<stars.length; i++){
star = stars[i];
this.drawStar(star[0], star[1]);
}
},
drawLines : function(){
var c = Go.cxt, fc, tc;
for (var i=1; i<=this.size; i++){
c.beginPath();
fc = this.getCoords(1, i);
tc = this.getCoords(this.size, i);
c.moveTo(fc[0], fc[1]);
c.lineTo(tc[0], tc[1]);
c.stroke();
}
for (var i=1; i<=this.size; i++){
c.beginPath();
fc = this.getCoords(i, 1);
tc = this.getCoords(i, this.size);
c.moveTo(fc[0], fc[1]);
c.lineTo(tc[0], tc[1]);
c.stroke();
}
},
drawLabels : function(){
var c = Go.cxt, f1, f2;
c.textAlign = 'center';
c.textBaseline = 'middle';
for (var i=1; i<=this.size; i++){
f1 = this.getCoords(i, 1);
f2 = this.getCoords(i, this.size);
c.strokeText(this.xLabel.charAt(i-1).toUpperCase(), f1[0], f1[1]-(this.margin/2));
c.strokeText(this.xLabel.charAt(i-1).toUpperCase(), f2[0], f2[1]+(this.margin/2));
}
for (var i=1; i<=this.size; i++){
f1 = this.getCoords(1, i);
f2 = this.getCoords(this.size, i);
c.strokeText(i, f1[0]-(this.margin/2), f1[1]);
c.strokeText(i, f2[0]+(this.margin/2), f2[1]);
}
},
monitorMouseMove : function(e){
var x, y, mx, my,
sw = Go.board.stoneWidth,
sh = Go.board.stoneHeight;
if(e.offsetX) {
mx = e.offsetX;
my = e.offsetY;
}
else if(e.layerX) {
mx = e.layerX;
my = e.layerY;
}
x = Math.round((mx+(sw/2))/(2*sw));
y = Math.round((my+(sw/2))/(2*sh));
},
monitorMouseClick : function(e){
// this isn't really perfected..
var x, y, mx, my,
sw = Go.board.stoneWidth,
sh = Go.board.stoneHeight,
m = Go.board.margin;
if(e.offsetX) {
mx = e.offsetX;
my = e.offsetY;
}
else if(e.layerX) {
mx = e.layerX;
my = e.layerY;
}
x = Math.round((mx+(m/2)+(sw/2))/(2*sw));
y = Math.round((my+(m/2)+(sw/2))/(2*sh));
Go.board.drawStone(x,y);
}
});
var start = function(){
Go.canvas = document.getElementById('goboard');
Go.canvas.onmousemove = Go.board.monitorMouseMove;
Go.canvas.onclick = Go.board.monitorMouseClick;
Go.cxt = Go.canvas.getContext('2d');
Go.board.buildMap();
Go.board.drawBackground();
Go.board.drawLines();
Go.board.drawStars();
Go.board.drawLabels();
}
// -->
</script>
</head>
<body onLoad="start();">
<canvas id="goboard" name="goboard" width="900" height="900">
</canvas>
</body>
</html> |
There are a lot of little hidden support features built in to a Mac that you’ll only ever find out if you dig deep or call Apple tech support directly. One quite useful procedure I recently learned of is how to reset all hardware in your mac. This may be useful if you are experiencing odd behavior that doesn’t seem software related.
Step 1: Shut down the Mac normally.
Step 2: Remove all power and peripherals. (Pull every last cord out of the back)
Step 3: Hold the power button in for 10 seconds. (I believe this is to ensure that all power is thoroughly drained from the system as there are certain electronic components that can hold on to a charge for quite some time.)
Step 4: Plug everything back in.
Step 5: Turn on the power and hold command+alt+p+r
Keep these keys held until you hear the boot up chime ring twice. There may be a slight pause between chimes.
Once this is complete, resume using the Mac normally and see if your problems have been solved. (If not, don’t despair. This is after all only one of a number of possible troubleshooting steps)
Perhaps I’ll write more about this later, but for now I’ll just say that I’ve been using a lot of the Zend Framework and Ext Javascript library. I enjoy writing code with these fantastic tools.
Zend Framework
Ext Javascript Library
Javascript isn’t always the easiest stuff to write. Browser issues, debugging (at least before the days of firebug), etc. Anybody who’s tried anything more than a simple alert() knows this. So I’m always happy when I come across a free pre-written library for accomplishing tasks that would have otherwise taken me more than a few minutes to do myself. Here’s one that just happened to gracefully meet my every need for a countdown timer: