Tuesday, September 20, 2005

How do you throw an exception?

Q: How do you throw an exception?

A: The Throw() method is used to induce an error into the error handling routine. To generate an error, simply declare an object of type "Exception" with the desired message. Then call the Throw() method to send this error message to the error handling routine ("Catch" block). Once Throw() is called, all intermediate code will be skipped and execution will transfer to the "Catch" block, where Ex.Message="Something wrong happened!" See the code example listed below:

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Try

'Some code logic here...

Dim ex As New Exception("Something wrong happened!")
Throw ex

'Some code logic here...

Catch ex As Exception 'Catch a generic exception.
Dim strMsg As String
strMsg = "Error: " & Ex.Message & ControlChars.CrLf
strMsg &= "Stack Trace: " & Ex.StackTrace & ControlChars.CrLf

lblError.Text = strMsg
End Try

End Sub

Friday, September 16, 2005

How do you force a user to always login to web app?

Q: How do you force a user to always login?

A: In the Login.aspx page, the following code would be placed in the btnLogin_Click():

If wsAuth.Auth(strUID, strPWD) = True Then
'Successful Login

FormsAuthentication.SetAuthCookie(txtUserID.Text, False)
Response.Redirect("MainMenu.aspx")
Else
'Failure
ErrorLabel.Text = "Authentication did not succeed. Check user name and password."
End If



In each subsequent web page, check for authentication before loading the page. The following code would be placed in the Page_load event handler:

If Not IsPostBack Then
If Not (Request.IsAuthenticated) Then
Session.Abandon()
Response.Redirect("Login.aspx")

End If
End If


Forcing a Login and redirecting to the user's requested URL.
Q: If a user opens a browser and enters a URL to a specific page within a web application without loggin in first, how do you force him to login then redirect him to the page requested?

A: If you use the statement Response.Redirect("default.aspx?URL=" + HttpUtility.UrlEncode(Request.RawUrl)) in each page's Page_Load(), it will force users to the login page. In addition since you're passing the RawURL (the URL initially entered by the user), you can later redirect the user back to this URL only AFTER the user has logged in. This would be accomplished using the following code in the Page_Load() of each page of the application subsequent to logging in.

If Not IsPostBack Then
If Not (Request.IsAuthenticated) Then
Session.Abandon()
Response.Redirect("Login.aspx?URL=" + HttpUtility.UrlEncode(Request.RawUrl))
End If
End If


In Login.aspx, after the user has successfully logged in, we can redirect him back to the URL the user originally requested by accessing the URL passed as a parameter to the Login page.
For example, add the following code to the :

If wsAuth.Auth(strUID, strPWD) = True Then
'Successful Login
FormsAuthentication.SetAuthCookie(txtUserID.Text, False)

Response.Redirect(Request.QueryString(URL))
Else
'Failure
ErrorLabel.Text = "Authentication did not succeed. Check user name and password."
End If

Monday, September 12, 2005

.Net Events

Listed below are several .Net events to note on your calendar:

1. SharePoint SIG: September 12, 2005 @ 6:30 PM. For more info see http://gcpcug.org/sigdetail.php?pick_sig=44

2. C#/VB.NET SIG: September 27, 2005 @ 6:30 PM. For more info see http://www.clevelanddotnet.info

3. MSDN Event: October 04, 2005 @ 1:00 PM, Regal Severance Town Center. For more info, see http://www.msdnevents.com

4. SQL Server 2005, Visual Studio 2005 & BizTalk Server 2006 Launch, Tuesday, November 08, 2005 @ 9:00 AM in Detroit, MI. For
more info, see http://msdn.microsoft.com/launch2005

Friday, September 9, 2005

SQL Server & .Net Encryption

Q: I would like to encrypt credit card numbers and ssn numbers in the SQL database. The data will be submitted to the database from a .NET application and will be retrieved from a .NET application. Is there anything within .NET that can encrypt and decrypt the fields?

A: There is a System.Security.Cryptography class within .Net that supports various forms of encryption. Also, SQL Server 2000 has a built-in Encrypt() function that can be used. In addition, you should consider encrypting the data crossing the wire from the browser to the server, that's where many security attacks occur.

Wednesday, September 7, 2005

What is System.DBNull.Value used for?

Q: What is System.DBNull.Value used for?



A: System.DBNull.Value is the value of NULL retrieved from a DB. Before assigning a database field to a variable, you should always check to see if the value returned from the DB is NOT null. Otherwise, an exception could be thrown.



Example:

If Not drPkgRec("MeetDate") Is System.DBNull.Value Then

Dim strMeetingPlace = drPkgRec("MeetPlace")

End If



Note: The Keyword "Is" is used, not "=".



Tuesday, September 6, 2005

Database Mirroring in SQL Server 2005

I came across an article on TechNet about Database Mirroring. This is an in-depth article that should cover all the details (i.e. what is it, how to use it, etc). http://www.microsoft.com/technet/prodtechnol/sql/2005/dbmirror.mspx

Friday, September 2, 2005

The identifier that starts with "SELECT * FROM ...." is too long. Maximum length is 128.

This morning as I was working on a project, I tested a SQL statement in Query Analyzer, but got the following error:

Server: Msg 103, Level 15, State 7, Line 1The identifier that starts with "SELECT * FROM ...." is too long. Maximum length is 128.

After searching on the internet, I found a solution at http://support.microsoft.com/default.aspx?scid=kb;EN-US;294839. Microsoft recommends installing SP1 to resolve this and it worked.

Microsoft Office SharePoint Portal Server 2003 Discovery Kit

The SharePoint Portal Server 2003 Discovery Kit includes a series of discovery labs that go beyond learning about a particular component of SharePoint Portal Server 2003 to understanding how to build a divisional portal site solution, how to deploy Web applications that integrate tightly with the portal site environment, how to use SQL Server 2000 Reporting Services to track and analyze portal site usage, and lastly, how to customize the portal site user interface.

There are 6 labs in the SharePoint Portal Server 2003 Discovery Kit. The lab manuals includes a description of the business scenario that is addressed by the lab, the teaching objectives of the lab, step-by-step instructions, dependencies for each lab and the expected amount of time needed for the lab assuming a basic familiarity with Windows SharePoint Services and limited SharePoint Portal Server 2003 experience.

http://www.microsoft.com/downloads/details.aspx?familyid=03607516-cbec-4724-b4a4-aa7f09304ba5&displaylang=en

Thanks to Steve Luper (Microsoft) for the info.

Tuesday, August 30, 2005

Questions from the August 2005 SIG Meeting

Listed below are the answers to some of the questions posed at the August SIG meeting about SharePoint 2003.

Q: For a web part that displays Outlook contacts in a SharePoint site, will it require Exchange?
A: If you use "My Inbox" and tweak it a bit to point to any mail folder in your Exchange mailbox. (e.g. https://exchange.mycompany.com/exchange/1602/contacts/) In this case,
it does require Exchange. On the other hand, there are special list types called Contacts Lists built-in to SharePoint. These are not integrated directly with Outlook. The data sits in the SharePoint SQL database. However, there is an option to "Link to Outlook". This gives you a link to the SharePoint Contacts list from your Outlook client. You can also import contacts from Outlook into the SharePoint contact list. In both cases, Exchange is probably not required, but this has not been tested.


Q: How do you deploy an application from QA to production?
A: First, build a CAB file to contain assemblies(.DLL), content files(.XML, .DWP), etc. Second, use STSADM.EXE in WSS to backup an existing site, if needed. For more information on STSADM.EXE see http://support.microsoft.com/?kbid=889236. Third, unzip the CAB file to install all the required files on the SharePoint server.


Q: Are there any issues with installing VS.Net Team systems and SharePoint 2003 on the same machine?
A: As of now, VS.Net Team Systems is available only in a beta version, and anytime a beta version is installed, issues maybe encountered such as ...
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=9dd6ab39-625c-46bb-bc88-973b268973d7
VS.Net 2005 Team System requires SharePoint, and the installation notes that come with it explain how to do this. If you’re doing a clean install you will need to install SharePoint with the MSDE database, because SharePoint doesn’t work with SQL 2005 yet. The SQL 2005 DB and MSDE can coexist, because MSDE is installed to a different instance.
If you’re going to install VS.Net 2005 on an existing system with SharePoint, you need to make sure the SQL 2005 install doesn’t update the existing SQL 2000 environment.

Thanks to Nate B. and other members of the Berbee team for helping answer some of these questions.

Friday, August 26, 2005

Visual Studio .Net has detected that the specified web server is not running asp.net version 1.1, You will be unable to run ASP.Net Web Applications

Problem: If you start Visual Studio .Net 2003 and get the following error:

"Visual Studio .Net has detected that the specified web server is not running asp.net version 1.1, You will be unable to run ASP.Net Web Applications or services"

There are several ways to go about fixing this error.

Solution:
From "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322", run "aspnet_regiis.exe -r". The following messages will appear

Start replacing ASP.NET DLL in all Scriptmaps with current version (1.1.4322.0).
Finished replacing ASP.NET DLL in all Scriptmaps with current version (1.1.4322.0).


When execution has completed, you can start VS.Net and load a web application without any problems.

Wednesday, August 24, 2005

Passing variables to a web page in .Net

To pass a variable from one web page to another page, use Response.Redirect() method(Response.Redirect("newpage.aspx?variablex=newvalue")) to pass a variable to a newpage.aspx.

In the receiving page (newpage.aspx), use the Request.QueryString() function to extract the variable desired (Dim strPassed As String = Request.QueryString("variablex")). When executed, the value of strPassed will be "newvalue".

Tuesday, August 23, 2005

Authenticating web app users against Active Directory

I found a great article (http://support.microsoft.com/kb/326340) on microsoft.com that provides click-by-click instructions on how to authenticate users logging into a web application against Active Directory.

The article is complete and easy to understand, but misses to mention 2 crucial points. Within Login.aspx, you need to specify "Imports MyApp.FormsAuth". This will import the FormsAuthAD Namespace, otherwise declaring a variable of type "LDAPAuthentication" will result in a compile time error.

Also, if you cut & paste the code, be sure that any references to "FormsAuthAD" are changed to the name of your project. This will ensure you import the correct class from the correct assembly. Aside from these points, the article is nicely written and easy to understand.

Monday, August 22, 2005

Storing a time zone value in SQL Server's datetime field

Q: Is there a way to store the time zone value in a datetime field in SQL Server? I receive the value of ‘12/1/2005 12:31:23 -5:00’ and when I try to store it in SQL it fails. Do I need to format the date/time differently, or I am forced to create a separate time zone field?

A: SQL Server does not support storing a timezone in a datetime field. However, to overcome that issue, you can convert all date/time values to the UTC timezone so you have a standard timezone to work with. When displaying the data to the user, you would then convert the time to the client’s timezone.

In regards to storing the value ‘12/1/2005 12:31:23 -5:00’ into SQL Server, try using the format '2005-08-09 00:00:00.000'.