Entries Tagged 'ASP.NET' ↓
March 9th, 2007 — ASP.NET
We recently dropped one of our commercial libraries, the Rebex.Net.Mail Tool. Instead we are using the build in System.NET.Mail Class from ASP.NET 2.0. In ASP.NET 1.1 you were able to use the System.Web.Mail Class and you were able to send mail via GMail by adding those filters:
ASP.NET v1.1:
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
// - smtp.gmail.com use smtp authentication
mailMsg.Filters.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mailMsg.Filters.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "xx");
mailMsg.Filters.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "xx");
// - smtp.gmail.com use port 465 or 587
mailMsg.FiltersAdd("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
// - smtp.gmail.com use STARTTLS (some call this SSL)
mailMsg.Filters.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
// try to send Mail
As I switched to the new ASP.NET 2.0 System.Net.Mail class the filters function was gone. Important: mailMsg.Headers.Add in the new class is not the equivalent class to that Filters.Add method, that’s what I thought, too, in the beginning.
In fact it got much easier now, but totally different:
ASP.NET v2.0:
// Smtp configuration
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential("xx", "xx");
smtp.EnableSsl = true;
I’d love to have more specific documentations on those changes, but thanks to Microsoft for making our code smaller once again ;).
February 10th, 2007 — ASP.NET
In case you are using the ASP.NET Membership Provider you probably noticed that LastActivityDate doesn’t update correctly whenever someone checked Remember me on his Login. I got a site running with a Cookie timeout of 10080 seconds, a pretty long time. I need to have an accurate LastActivityDate because I want to display whenever a user has actually been active.
So what now? I want to stick to my cookie timeout value and need to update the LastActivityDate manually. Here’s my approach (I love stored procedures).
First create a stored Procedure:
31 set ANSI_NULLS ON
32 set QUOTED_IDENTIFIER OFF
33 GO
34 – =============================================
35 – Author: Andreas Kraus (http:/www.andreas-kraus.net/blog)
36 – Create date: 2007-02-10
37 – Description: Updates LastActivityDate
38 – =============================================
39 ALTER PROCEDURE [dbo].[aspnet_Membership_UpdateLastActivityDate]
40 @UserName nvarchar(256),
41 @LastActivityDate datetime
42
43 AS
44 BEGIN
45 IF (@UserName IS NULL)
46 RETURN(1)
47
48 IF (@LastActivityDate IS NULL)
49 RETURN(1)
50
51 UPDATE dbo.aspnet_Users WITH (ROWLOCK)
52 SET
53 LastActivityDate = @LastActivityDate
54 WHERE
55 @UserName = UserName
56 END
Then use this code in your global.asax by hitting the Application_AuthenticateRequest Event:
31 void Application_AuthenticateRequest(object sender, EventArgs e)
32 {
33 if (User.Identity.IsAuthenticated)
34 {
35 System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
36 System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
37 cmd.Connection = cn;
38 cmd.CommandText = “aspnet_Membership_UpdateLastActivityDate”;
39 cmd.Parameters.AddWithValue(“@UserName “, User.Identity.Name);
40 cmd.Parameters.AddWithValue(“@LastActivityDate”, DateTime.Now);
41 cmd.CommandType = System.Data.CommandType.StoredProcedure;
42 cn.Open();
43 cmd.ExecuteNonQuery();
44 cn.Close();
45 }
46 }
That’s it, now you have a most accurate LastActivityDate value to work with.
December 18th, 2006 — ASP.NET
Version 1.9 adds a little requested feature to my ASP.NET Deployment Tool. Whenever you hit Deploy Local a file called compilerlog.txt will be created now in the Application Folder which holds the compiler output.
So if you got lots of warnings and errors popping up it’s easier to investigate them by looking into compilerlog.txt. Of course using Visual Studio is still best for investigation but it appeared that many people do small fixes without firing up VS once again.
“Bin” is checked by default now: It only copies the content of the bin directory when you hit Copy to FTP.
Short overview for people who don’t know it yet:
An avanced and robust Deployment Tool for precompiling your ASP.NET Websites! Pre-Compilation gives your site a performance boost and secures it.
Precompile and deploy your Website: Specific Error Panel, FTP Support, Deploy to Network, Merge Assemblies, easy to use.
Download it here: ASP.NET Deployment Tool v1.9 (I didn’t update the version number on that page yet, don’t mind it, it’s v1.9)
Edit: I set the background color of the Toolstrip to a static color as many people use black for their desktops 
November 13th, 2006 — ASP.NET
I’ve dealt with custom ASP.NET Paging lately and wanted to share the fastest possible way to do that with you if you don’t use ADO.NET or any ASP.NET Controls.
Create a stored Procedure like this:
CREATE PROCEDURE [dbo].[usp_PageResults_NAI]
(
@startRowIndex int,
@maximumRows int
)
AS
DECLARE @first_id int, @startRow int
– A check can be added to make sure @startRowIndex isn’t > count(1)
– from employees before doing any actual work unless it is guaranteed
– the caller won’t do that
– Get the first employeeID for our page of records
SET ROWCOUNT @startRowIndex
SELECT @first_id = employeeID FROM employees ORDER BY employeeid
– Now, set the row count to MaximumRows and get
– all records >= @first_id
SET ROWCOUNT @maximumRows
SELECT e.*, d.name as DepartmentName
FROM employees e
INNER JOIN Departments D ON
e.DepartmentID = d.DepartmentID
WHERE employeeid >= @first_id
ORDER BY e.EmployeeID
SET ROWCOUNT 0
GO
And in ASP.NET you execute a DataReader against this piece of code:
42 string SQL = “pics_Paging”;
43
44 cmd.Connection = cn;
45 cmd.CommandText = SQL;
46 cmd.Parameters.AddWithValue(“@startRowIndex”, 6);
47 cmd.Parameters.AddWithValue(“@maximumRows”, 4);
48 cmd.CommandType = System.Data.CommandType.StoredProcedure;
That’s all and it’s pretty speedy, thanks to 4GuysFromRolla.com for the input! All you have to do now is to create the logic for startRowIndex and maximumRows which is a piece of cake 
November 9th, 2006 — ASP.NET
Hosting in a basic configuration can be a developer’s pain when it comes to file operations. Usually the webspace doesn’t allow files to be created which has a good reason in that configuration, though.
A very smart way to deal with that is to use Isolated Storages. That way all the files are saved in a virtual storage which also means that you can control the access level which is pretending conflicts with other applications. That’s how it could look like:
176 // Filestore init
177 IsolatedStorageFile isoStore = IsolatedStoargeFile.GetStore(IsolatedStorageScope.Assembly | IsolatedStorageScope.User, null, null);
178
179 // Create dir
180 isoStore.CreateDirectory(“myAppData”);
181 isoStore.CreateDirectory(“myAppData/settings”);
182
183 // Write File into Store
184 IsolatedStoargeFileStream isoStream1 = new IsolatedStorageFileStream(“myAppdData/hello.txt”, System.IO.FileMode.Create, isoStore);
185 byte[] content = Encoding.UTF8.GetBytes(“Hello!”);
186 isoStream1.Write(content, 0, content.Length);
187
188 // Read file
189 IsolatedStorageFileStream isoStream2 = new IsolatedStorageFileStream(“myAppData/hello.txt”, System.IO.FileMode.Open, isoStore);
190 byte[] content2 = new byte[isoStream2.Length];
191 isoStream2.Read(content2, 0, content2.Length);
192 isoStream2.Close();
193
194 Response.Write(Encoding.UTF8.GetString(content2));
195
196 // delete store
197 isoStore.Remove();
That’s all, a pretty clean approach for dealing with files in ASP.NET due to hosting restrictions. Nevertheless it’s also interesting to use in Desktop Applications due to the customized privileges for that storage.
October 25th, 2006 — ASP.NET
If you moved from ATLAS to the new ASP.NET AJAX Beta1 you might encounter that kind of problem, all other controls are affected as well like Element ‘UpdatePanel’ is not a known element.
To get around this issue you have to change the tagPrefix in your web.config:
Old:
<controls>
<add tagPrefix="asp" namespace="Microsoft.Web.UI"
assembly="Microsoft.Web.Extensions, Version=1.0.61025.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add tagPrefix="asp" namespace="Microsoft.Web.UI.Controls"
assembly="Microsoft.Web.Extensions, Version=1.0.61025.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</controls>
New:
<controls>
<add tagPrefix="ajax" namespace="Microsoft.Web.UI"
assembly="Microsoft.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"/>
<add tagPrefix="ajax" namespace="Microsoft.Web.UI.Controls"
assembly="Microsoft.Web.Extensions, Version=1.0.61025.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</controls>
This works as a temporary fix, this issue will be addressed in the next release of ASP.NET AJAX Beta.
hth
October 24th, 2006 — ASP.NET
Microsoft said, ASP.NET applications contain 40 to 70 percent less code than ASP `applications`. My little test shows that it’s true, ASP.NET is really faster. However, an exact statement on speed is difficult because it’s depending on many factors.
The point of success isn’t the compiling but the early binding. That can be shown in an easy loop. I’ve used a Dual AMD Opteron, 2GHZ, 4GB RAM on Windows 2003 Server / IIS 6.0 for that test, here’s the code:
In ASP:
<%
Dim b, a, start, end, count
count = 50000000
Response.Write("Loops: " & count & "<hr>")
start = now
for a = 1 to count
b = b + a
next
end = now
Response.Write("Start: " & start & "<br>")
Response.Write("End: " & end & "<br>")
Response.Write("Execution length: " & DateDiff("s",start,end))
%>
In ASP.NET 2.0 with late binding:
private void Page_Load(object sender, System.EventArgs e)
{
object b = null;
object a = null;
object start = null;
object end = null;
object count = null;
count = 50000000;
Response.Write("Loops: " + count.ToString() + "<hr");
start = DateTime.Now;
object tempFor1 = ;
for (a = 1; a <= count; a++)
{
b = b + a;
}
end = DateTime.Now;
Response.Write("Start: " + start.ToString() + "<br>");
Response.Write("End: " + end.ToString() + "<br>");
Response.Write("Execution length:
" + Microsoft.VisualBasic.DateAndTime.DateDiff("s", start,
end, Microsoft.VisualBasic.FirstDayOfWeek.Sunday,
Microsoft.VisualBasic.FirstWeekOfYear.Jan1));
}
And now in ASP.NET 2.0 with early binding:
Same like above but:
Int64 b, Int64 a, DateTime start;
DateTime end;
Int64 count;
Here are the results:
- The ASP Code: 26 seconds
- The ASP.NET 2.0 Code with late binding: 16 seconds
- The ASP.NET 2.0 Code with early binding: < 1 second!
The result speaks for itself.

October 22nd, 2006 — ASP.NET
Too bad. Microsoft said goodbye to Atlas and comes up with a *new* framework called ASP.NET AJAX. There are lots of new features and bugfixes within that beta release which comes in 3 pars:
- The ASP.NET AJAX v1.0 “Core†download. This redist contains the features that will be fully supported by Microsoft Product Support, and which will have a standard 10 year Microsoft support license (24 hours a day, 7 days a week, 365 days a year). The download includes support for the core AJAX type-system, networking stack, component model, extender base classes, and the server-side functionality to integrate within ASP.NET (including the super-popular ScriptManager, UpdatePanel, and Timer controls).
- The ASP.NET AJAX “Value-Add†CTP download. This redist contains the additional higher-level features that were in previous CTPs of “Atlas,†but which won’t be in the fully-supported 1.0 “core†redist. These features will continue to be community supported as we refine them further and incorporate more feedback. Over time we’ll continue to move features into the “core†download as we finalize features in this value-add package more.
- The ASP.NET AJAX Control Toolkit. This project contains 28 free, really cool, AJAX-enabled controls that are built on top of the ASP.NET AJAX 1.0 “Core†download. The project is collaborative shared source and built by a combination of Microsoft and non-Microsoft developers, and you can join the community or just download it on CodePlex today.
Sounds nice but the thing which makes me a little bit angry is.. the old Atlas code has to be migrated. It took me about 5 hours to migrate four of my existing ASP.NET Atlas applications. Here’s the migration guide: Atlas Migration guide.
Let’s hope we’re working with somewhat more stable bits now, ASP.NET Ajax.
October 10th, 2006 — ASP.NET, Development, SQL
Red-Gate released a fantastic SQL Refactor Tool which helps you refactoring that horrible SQL Code you have to deal with on a daily basis.
SQL Refactor is an Add-In to Microsoft Management Studio. Therefore you must have Management Studio installed. SQL Refactor’s features are available from the Management Studio menus, which can access both SQL Server 2000 and SQL Server 2005. In this release of SQL Refactor you can use the following features:
- SQL Lay Out reformats your T-SQL scripts. You can select this feature from the top level SQL Refactor menu. There are over 30 options to control this feature, these you can access from the top level SQL Refactor menu.
- Smart Rename renames functions, views, stored procedures and tables, and updates all the references to these renamed objects. You can select this feature from the context menu in Management Studio’s Object Explorer.
- Smart Rename parameters and columns renames parameters of stored procedures and functions, and columns of tables and views. You can select this feature from the context menu in Management Studio’s Object Explorer.
- Table Split splits a table into two tables, and automatically rewrites the referencing stored procedures, views, and so on. You can also use this refactoring to introduce referential integrity tables. You can select this feature from the context menu in Management Studio’s Object Explorer.
- Uppercase keywords turns keywords in your script or selection to uppercase.
- Summarize Script provides you with an overview of your script. By highlighting items in this overview you can see the corresponding statements highlighted in your script.
- Encapsulate as stored procedure turns your selection into a new stored procedure, and if requested, introduces a reference to it in your script.
- Expand wildcards expands SELECT * statements to include a full list of columns in the select part.
- Find unused variables and parameters shows you the variables and parameters in you script that are not used, or that are only assigned to.
- Qualify Object Names modifies the script so that all object names are qualified. You can select this feature from the top level SQL Refactor menu.
Download it here: ftp://ftp.red-gate.com/sqlrefactorbeta/sqlrefactorsetup.exe
Great Job!
September 26th, 2006 — ASP.NET, SQL
Here’s how to update one table by selecting the values from another:
UPDATE C
SET C.Phone = O.[Tel],
C.Fax = O.[Fax]
FROM output$ O
JOIN Contacts C
ON C.MemberId = O.MemberId
Maybe this saves someone time..
September 23rd, 2006 — ASP.NET
I found a pretty nice set of AJAX Activity Indicators here: http://www.napyfab.com/ajax-indicators/
One example would be this here:

Saves some time in AJAX Development 
September 22nd, 2006 — ASP.NET, HowTo's
I’m finally back from vacation, expect some impressions of Gran Canaria in the near future. For now, here’s a little HowTo for enforcing strong passwords.
Regex is a good way to deal with password validation. If you want your users to choose strong passwords here’s how to do it:
This regular expression will enforce a password to be at least 8 characters and to be a mix of letters and numbers. Additionally they need to have at least one uppercase letter.
^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$
“pAssword555†will be accepted.
Serverside implementation:
public static bool IsPasswordStrong(string password)
{
return Regex.IsMatch(password, @"^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$");
}
Clientside implementation:
<asp:TextBox runat="server" ID="PasswordBox" TextMode="password" />
<asp:RegularExpressionValidator runat="server"
ControlToValidate="PasswordBox"
ValidationExpression="(?=.{8,})[a-zA-Z]+[^a-zA-Z]+|[^a-zA-Z]+[a-zA-Z]+"
Display="Dynamic"
ErrorMessage="Password must be 8 chars long and has to contain letters and numbers." />
Simple and effective, enjoy!
August 23rd, 2006 — ASP.NET
Whenever you execute a Cross Page Post-Back from one site to another by using
<asp:button PostBackUrl=“anotherpage.aspx" runat=“server"/>
be sure to set this on anotherpage.aspx to gain full access to the controls of the previous page:
<%@ PreviousPage VirtualPath=“previouspage.aspx" %>
Afer that, you can access the controls by using the Page.PreviousPage Property.
Example:
Textbox MyNewTextBox = PreviousPage.FindControl("PreviousPageTextBox");
Label1.Text = MyNewTextBox.Text;
Cheers!
July 28th, 2006 — ASP.NET, Development, Microsoft
Microsoft is leaving Java in the dust, but the company still has room to grow in the developer arena, a key executive said. Speaking at the Microsoft FAM (Financial Analyst Meeting) on July 27 in Redmond, Wash., Bob Muglia, Microsoft’s senior vice president of Server and Tools business, said Microsoft’s .Net platform has outpaced Java, particularly the Java Enterprise Edition, over the past five years to become the development platform of choice for enterprise development.
“Five years ago we had problems with J2EE [Java 2 Platform, Enterprise Edition],” Muglia said. However, “We’ve grown from having a quarter of the market to, now, 60 percent,” he said. Microsoft displayed the FAM presentations via Webcast. “J2EE has run its course,” Muglia said.
.. as expected! Along with the .NET Framework 3.0, Java will be far behind.
July 5th, 2006 — ASP.NET, Administration, HowTo's, Microsoft
This is a quick and dirty HowTo ASP.NET on the popular Apache Webserver. It works with ASP.NET v1 and ASP.NET v2!
1) Download and install mod_AspDotNet
2) At the end of your httpd.conf file add the following lines:
#asp.net
LoadModule aspdotnet_module "modules/mod_aspdotnet.so"
AddHandler asp.net asax ascx ashx asmx aspx axd config cs csproj licx rem resources resx soap vb vbproj vsdisco webinfo
<IfModule mod_aspdotnet.cpp>
# Mount the ASP.NET /asp application
AspNetMount /SampleASP "c:/SampleASP"
#/SampleASP is the alias name for asp.net to execute
#"c:/SampleASP" is the actual execution of files/folders in that location
# Map all requests for /asp to the application files
Alias /SampleASP "c:/SampleASP"
#maps /SampleASP request to "c:/SampleASP"
#now to get to the /SampleASP type http://localhost/SampleASP
#It'll redirect http://localhost/SampleASP to "c:/SampleASP"
# Allow asp.net scripts to be executed in the /SampleASP example
<Directory "c:/SampleASP">
Options FollowSymlinks ExecCGI
Order allow,deny
Allow from all
DirectoryIndex index.htm index.aspx
#default the index page to .htm and .aspx
</Directory>
# For all virtual ASP.NET webs, we need the aspnet_client files
# to serve the client-side helper scripts.
AliasMatch /aspnet_client/system_web/(\d+)_(\d+)_(\d+)_(\d+)/(.*) "C:/Windows/Microsoft.NET/Framework/v$1.$2.$3/ASP.NETClientFiles/$4"
<Directory "C:/Windows/Microsoft.NET/Framework/v*/ASP.NETClientFiles">
Options FollowSymlinks
Order allow,deny
Allow from all
</Directory>
</IfModule>
#asp.net
3) Continue with creating C:\AspNetTest.
4) Create a file “index.aspx” and add the following lines to it:
<%@ Page Language="C#" %>
<html>
<head>
<link rel="stylesheet"href="example.css">
</head>
<body>
<form>
<% for (int i=0;i<5;i++= { %>
<font size="<%=I%>"> Sample ASP.NET TEST</font> <br>
<% } %>
</form>
</body>
</html>
5) Restart Apache and visit http://localhost/AspNetTest - it should be working!
Hope that helps..