Navigation

Search

Categories

On this page

Update records from two tables
Conditional GridView Cell Formatting
Displaying all files in a directory and in a datatable
Using XMLHttpRequest
Clearing a Form In ASP.NET
Google AJAX Search API
Javascript/AJAX Library
Using XP_EXECRESULTSET To Obtain Database Size Information
SQL Web Data Administrator
AJAX-Style Loading Icons
Checking for null values in Request.Querysting
The Format() Function
Formatting Dates and Times
Formatting Percentages
Formatting Numbers

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 112
This Year: 50
This Month: 4
This Week: 0
Comments: 0

Sign In

 Wednesday, July 11, 2007
Wednesday, July 11, 2007 3:17:55 PM (Eastern Standard Time, UTC-05:00) ( )

UPDATE table1
SET col1 = t2.col1
FROM table1 AS t1
JOIN table2 AS t2
ON t1.col2 = t2.col2

 Monday, July 09, 2007
Monday, July 09, 2007 8:31:48 AM (Eastern Standard Time, UTC-05:00) ( )

This code sample shows how to either show or make invisible, a checkbox in each row of the Gridview, along with making text conditional, based on certain criteria. In this case, if the Postal code starts with a non-numeric character, we change it to "Alt Text", and we set the Visible property of the checkbox in that row to "False"

<%@ Import Namespace="System.Drawing" %>
<script language="VB" Runat="server">
Sub DataCheck(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim chkExp as CheckBox
If e.Row.RowType = DataControlRowType.DataRow Then
Dim sCode as String=e.Row.Cells(7).text
If Not isNumeric(sCode.Substring(1,1)) Then
e.Row.Cells(7).text="<i style='color:red'>(Alt Text)</i>"
        chkExp= CType(e.row.FindControl("ck1"), Checkbox)
        chkExp.Visible="False"
End If
End If
End Sub    
</script>
<html>
<head runat="server">
    <title>Conditional GridView Cell Formatting</title>
</head>
<body>
<form id="form1" Runat="server">
<asp:GridView OnRowDataBound="DataCheck" AutoGenerateColumns="False"
    DataSourceID="SqlDataSource1" ID="GridView2" runat="Server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ck1" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="First" />
<asp:BoundField DataField="LastName" HeaderText="Last" />
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Region" HeaderText="Region" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
<asp:BoundField DataField="country" HeaderText="country" />
</Columns>
<HeaderStyle BackColor="Blue" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="Select FirstName, LastName, Title, Address, City, Region, PostalCode, country from Employees">
</asp:SqlDataSource>
        </form>
    </body>
</html>

Monday, July 09, 2007 8:27:20 AM (Eastern Standard Time, UTC-05:00) ( )

This sample shows how to create a datatable manually, adding files from a directory (using System.IO), and then binding the datatable to a Gridview

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<script language="VB" Runat="server">
Dim dt As DataTable
Dim dr As DataRow
    Sub Page_Load(Source as Object, E as EventArgs)
dt = GetFiles()
gvFiles.DataSource = dt
gvFiles.DataBind()
    End Sub
Public Function GetFiles() As DataTable
Dim strFilePath = Server.MapPath("\test\")
Dim DirInfo As New DirectoryInfo(strFilePath)
Dim Files As FileInfo() = DirInfo.GetFiles()

Dim myTable As New DataTable
myTable.Columns.Add("File Name", Type.GetType("System.String"))
myTable.Columns.Add("Last Write Time", Type.GetType("System.String"))
Dim i As Integer
For i = 0 To Files.Length - 1
Dim Filename As String = Files(i).Name
Dim sWrite As String = Files(i).LastWriteTime
Dim myrow As DataRow
' create new row
myrow = myTable.NewRow
' add files/write times into cells
myrow("File Name") = Filename
myrow("Last Write Time") = sWrite
myTable.Rows.Add(myrow)
Next
Return myTable
End Function    
</script>
<html>
    <head runat="server">        
        <title>Displaying all files in a directory and in a datatable</title>
    </head>
    <body>
<asp:GridView ID="gvFiles" runat="server" Width="432px">
</asp:GridView>
        </form>
    </body>
</html>

 Tuesday, July 03, 2007
Tuesday, July 03, 2007 12:48:50 PM (Eastern Standard Time, UTC-05:00) ( )

A coworker passed along this link from IBM which provides a nice series of tutorials on AJAX. The example below is a classic example of how to use the XMLHTTPRequest object while checking for different browser types and then using the server's response for something.

<script language="javascript" type="text/javascript">
var request = false;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}

if (!request)
alert("Error initializing XMLHttpRequest!");

function getCustomerInfo() {
var phone = document.getElementById("phone").value;
var url = "/cgi-local/lookupCustomer.php?phone=" + escape(phone);
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}

function updatePage() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseText.split("|");
document.getElementById("order").value = response[0];
document.getElementById("address").innerHTML = response[1].replace(/\n/g, "<br />");
} else if (request.status == 404) {
alert ("Requested URL is not found.");
} else if (request.status == 403) {
alert("Access denied.");
} else
alert("status is " + request.status);
}
}

</script>

<body>
<p><img src="breakneck-logo_4c.gif" alt="Break Neck Pizza" /></p>
<form action="POST">
<p>Enter your phone number:
<input type="text" size="14" name="phone" id="phone"
onChange="getCustomerInfo();" />
</p>
<p>Your order will be delivered to:</p>
<div id="address"></div>
<p>Type your order in here:</p>
<p><textarea name="order" rows="6" cols="50" id="order"></textarea></p>
<p><input type="submit" value="Order Pizza" id="submit" /></p>
</form>
</body>

Tuesday, July 03, 2007 8:07:15 AM (Eastern Standard Time, UTC-05:00) ( )

Clearing a form of all entries or values is a common task in HTML but seemed to be a bit more challenging in ASP.NET.  It seemed the only way to do this was by posting the page back to itself using server.transfer("page.aspx")

I just stumbled across this method new to ASP.NET 2.0 using OnClientClick and it works great.

<asp:Button ID="btnReset" CssClass="smalltext" OnClientClick="Form1.reset();return false;" runat="server" Text="Clear Form" />

HTML version:

<input type="reset" value="Clear Form" name="Reset" onclick="ResetControls(this.form)">

 Monday, July 02, 2007
Monday, July 02, 2007 11:48:54 AM (Eastern Standard Time, UTC-05:00) ( )

Google makes a cool API that utilizes AJAX to which allows developers to add a Web or local search to their sites relatively easily. 

I created an example of it here: http://www.stonecoastwebdesign.com/search/example.htm

Monday, July 02, 2007 9:37:01 AM (Eastern Standard Time, UTC-05:00) ( )

A co-worker passed this along to me which looks pretty cool

http://extjs.com/

 Tuesday, June 26, 2007
Tuesday, June 26, 2007 10:01:39 PM (Eastern Standard Time, UTC-05:00) (  |  )

Came across this article and code which provides a script for obtaining the size of every database on your SQL Server 2000 box. Very nice...

http://www.sqlservercentral.com/columnists/jtshyman/usingxp_execresultsettoobtaindatabasesizeinformati.asp

Tuesday, June 26, 2007 9:56:40 PM (Eastern Standard Time, UTC-05:00) ( )

I came across this nifty web-based administrator for SQL Server from Codeplex.   I haven't tried it yet but it looks promising and it's open source.

 

 

 Monday, June 25, 2007
Monday, June 25, 2007 11:52:42 AM (Eastern Standard Time, UTC-05:00) ( )

Some useful icons for displaying those "loading" messages used in AJAX-enabled applications.

 

 Friday, June 22, 2007
Friday, June 22, 2007 9:36:56 AM (Eastern Standard Time, UTC-05:00) ( )

Something I do all of the time and I think this the best method to do it

If Not Request.QueryString("DashboardID") Is Nothing andalso Request.QueryString("DashboardID").trim.length > 0 then

 Thursday, June 21, 2007
Thursday, June 21, 2007 8:21:22 PM (Eastern Standard Time, UTC-05:00) ( )

The Format() Function

The Format() function is a general-purpose formatting function that returns a string value formatted according to a format string. The format strings duplicate numeric and date/time formats produced by the specialized formats described above. The general format for applying the Format() function is shown below.

Format(value, "format string")

Formatting Numbers

A format string for numeric values can use one of the predefined string values shown in the following table.

String Description
General Number|G|g Displays number with no thousand separator.
Currency|C|c Displays number with thousand separator, if appropriate; display two digits to the right of the decimal separator.
Fixed|F|f Displays at least one digit to the left and two digits to the right of the decimal separator.
Standard|N|n Displays number with thousand separator, at least one digit to the left and two digits to the right of the decimal separator.
Percent Displays number multiplied by 100 with a percent sign (%) appended immediately to the right; always displays two digits to the right of the decimal separator.
P|p Displays number with thousandths separator multiplied by 100 with a percent sign (%) appended to the right and separated by a single space; always displays two digits to the right of the decimal separator.

Examples of applying a format string to numeric values are shown in the following table.

Format Output
Format(12345.6789,"g") 12345.6789
Format(12345.6789,"c") $12,345.68
Format(12345.6789,"f") 12345.68
Format(12345.6789,"n") 12,345.68
Format(-12345.6789,"g") -12345.6789
Format(-12345.6789,"c") ($12,345.68)
Format(-12345.6789,"f") -12345.68
Format(-12345.6789,"n") -12,345.68
Format(.6789,"Percent") 67.89%
Format(.6789,"p") 67.89 %
Format(-.6789,"Percent") -67.89%
Format(-.6789,"p") -67.89 %

Formatting Dates and Times

A format string for date/time values can use one of the predefined string values shown in the following table.

String Description
Long Date|D Displays a date in long date format.
Short Date|d Displays a date in short date format.
Long Time|T Displays a date in long date format.
Short Time|t Displays a date in short date format.
F Displays the long date and long time.
f Displays the long date and short time.
g Displays the short date and short time.
M|m Displays the month and the day of a date.
Y|y Formats the date as the year and month.

Examples of applying a format string to date/time values are shown in the following table.

Format Output
Format(Now,"D") Thursday, June 21, 2007
Format(Now,"d") 6/21/2007
Format(Now,"T") 9:10:57 PM
Format(Now,"t") 9:10 PM
Format(Now,"F") Thursday, June 21, 2007 9:10:57 PM
Format(Now,"f") Thursday, June 21, 2007 9:10 PM
Format(Now,"g") 6/21/2007 9:10 PM
Format(Now,"m") June 21
Format(Now,"y") June, 2007

User-defined Numeric Formats

Formats can be defined for displaying numeric values by composing a string to describe the format. This user-defined string is applied through the Format() function. The characters shown in the following table are used to compose the format string.

Character Description
0 Digit placeholder. Displays a digit or a zero. If the value has a digit in the position, then it displays; otherwise, a zero is displayed.
# Digit placeholder. Displays a digit or a space. If the value has a digit in the position, then it displays; otherwise, a space is displayed.
. Decimal placeholder; determines how many digits are displayed to the left and right of the decimal separator.
, Thousand separator; separates thousands from hundreds within a number that has four or more places to the left of the decimal separator. Only a single "," is required in the format, between the first set of digit placeholders.
% Percent placeholder. Multiplies the expression by 100. The percent character (%) is inserted in the position where it appears in the format string.
- + $ ( ) Literal characters; displayed exactly as typed in the format string.

Examples of applying a user-defined format string to numeric values are shown in the following table.

Format Output
Format(012345.6789,"0.00") 12345.68
Format(012345.6789,"0,0.000") 12,345.679
Format(012345.6789,"00000,0.000000") 012,345.678900
Format(012345.6789,"#.##") 12345.68
Format(012345.6789,"#,#.##") 12,345.68
Format(012345.6789,"$ #,#.##") $ 12,345.68
Format(-012345.6789,"#,#.####") -12,345.6789
Format(-012345.6789,"$#,#.##") -$12,345.68
Format(.6789,"#,#.##") .68
Format(.6789,"0,0.000") 00.679
Format(-.6789," 0.0000") - 0.6789
Format(.6789,"0.00%") 67.89%

User-defined Date/Time Formats

Formats can be defined for displaying date and time values by composing a string to describe the format. This user-defined string is applied through the Format() function. The characters shown in the following table are used to compose the date/time format string.

Character Description
: Time separator.
/ - Date separators.
% Precedes a single-character format string.
d Displays the day as a number without a leading zero.
dd Displays the day as a number with a leading zero.
ddd Displays the day name as an abbreviation.
dddd Displays the day as a full name.
M Displays the month as a number without a leading zero.
MM Displays the month as a number with a leading zero.
MMM Displays the month name as an abbreviation.
MMMM Displays the month as a full name.
yy Displays the year in two-digit format.
yyyy Displays the year in four-digit format.
h Displays the hour as a number without leading zeros using the 12-hour clock.
hh Displays the hour as a number with leading zeros using the 12-hour clock.
H Displays the hour as a number without leading zeros using the 24-hour clock.
HH Displays the hour as a number with leading zeros using the 24-hour clock.
m Displays the minute as a number without leading zeros.
mm Displays the minute as a number with leading zeros.
s Displays the seconds as a number without leading zeros.
ss Displays the seconds as a number with leading zeros.
f... Displays fractions of seconds using up to 7 characters to display fractional digits.
tt Uses the 12-hour clock and displays an uppercase AM with any hour before noon; displays an uppercase PM with any hour between noon and 11:59 P.M.
  Additional characters and punctuation marks can be used within the format string. Characters that match any of the formatting characters must be preceded by "\".

Examples of applying a user-defined format string to date/time values are shown in the following table.

Format Output
Format(Now,"M/d/yy") 6/21/07
Format(Now,"M-d-yyyy") 6-21-2007
Format(Now,"d-MMMM-yy") 21-June-07
Format(Now,"d MMMM, yyyy") 21 June, 2007
Format(Now,"MMMM d, yyyy") June 21, 2007
Format(Now,"MMMM, yyyy") June, 2007
Format(Now,"%d") 21
Format(Now,"h:m tt") 9:10 PM
Format(Now,"h:m:ss tt") 9:10:57 PM
Format(Now,"H:m") 21:10
Format(Now,"M/d/yy - h:mtt") 6/21/07 - 9:10PM
Format(Now,"H:m:ss.fffffff") 21:10:57.5829544
Format(Now,"To\da\y i\s MMMM d, yyyy.") Today is June 21, 2007.

Thursday, June 21, 2007 8:19:00 PM (Eastern Standard Time, UTC-05:00) ( )

FormatDateTime(Now) 6/21/2007 9:10:57 PM
FormatDateTime(Today) 6/21/2007
FormatDateTime(TimeOfDay) 9:10:57 PM
FormatDateTime(Now,DateFormat.LongDate) Thursday, June 21, 2007
FormatDateTime(Today,DateFormat.LongDate) Thursday, June 21, 2007
FormatDateTime(Now,DateFormat.ShortDate) 6/21/2007
FormatDateTime(Today,DateFormat.ShortDate) 6/21/2007
FormatDateTime(Now,DateFormat.LongTime) 9:10:57 PM
FormatDateTime(TimeOfDay,DateFormat.LongTime) 9:10:57 PM
FormatDateTime(Now,DateFormat.ShortTime) 21:10
FormatDateTime(TimeOfDay,DateFormat.ShortTime) 21:10

Thursday, June 21, 2007 8:18:24 PM (Eastern Standard Time, UTC-05:00) ( )

Format Output
FormatPercent(.6789) 67.89%
FormatPercent(.6789,4) 67.8900%
FormatPercent(-.6789) -67.89%
FormatPercent(-.6789,,,True) (67.89%)

Thursday, June 21, 2007 8:17:43 PM (Eastern Standard Time, UTC-05:00) ( )

Format Output
FormatNumber(12345.6789) 12,345.68
FormatNumber(12345.6789,5) 12,345.67890
FormatNumber(12345.6789,,,,False) 12345.68
FormatNumber(-12345.6789) -12,345.68
FormatNumber(-12345.6789,,,True) (12,345.68)
FormatNumber(.6789) 0.68
FormatNumber(.6789,,False) .68
FormatNumber(-.6789,4) -0.6789