Software Architecture & Development for Windows and the Web
Skip Navigation Links
Home
Services
About Us
Contact

Site Links

Skip Navigation Links
Home
Announcements
Applications
Services
Rapid Development
Clients
News
Presentations
Development Tips
Tips & Tricks
About Us
Contact Us

 
 

Tip & Tricks

Validation of viewstate MAC error on session expiry

This can be a very confusing error. It refers to web farms but occurs on single servers when a session expires. As usual the solution is easy when you know how.

The error can be prevented by generating a <machineKey /> element in the <system.Web> section of the web.config file.

A machineKey generator is avaiable at http://aspnetresources.com/tools/machineKey

Conditional Formating of DataGridView

For example set a row to red color if it is older than a specified date.

  Protected Sub GridViewFollowUpsPending_RowDataBound(ByVal sender As Object, _
 ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
 Handles GridViewFollowUpsPending.RowDataBound
        'set the color to read if over due
        If e.Row.RowIndex >= 0 Then   'not header row
            Dim ProcDate As DateTime
            ProcDate = Convert.ToDateTime(e.Row.Cells(DAL.vwfollowupspendingCOL.ProcDate + 1).Text)
            Dim ODays As Integer = 5

            If DateDiff(DateInterval.Day, ProcDate, DateTime.Now) > ODays Then
                e.Row.ForeColor = Drawing.Color.Red
            End If
        End If
 End Sub


ASP.NET Bound DropDownList Unselected State

If you databind to the ASP.NET DropDownList there is no native way to have an "unselected" item. You are restricted to the options contained in the datasource. However there is a work around that can acheive this:

          DropDownListBlockLocal2.AppendDataBoundItems = True
        DropDownListBlockLocal2.Items.Add("")
        DropDownListBlockLocal2.Items(0).Value = "0"
        DropDownListBlockLocal2.DataSource = _
DAL.tbllocalDataTableWhere(" (IsInactive <> 'Y' or IsInactive is null) ORDER BY Rank")
        DropDownListBlockLocal2.DataValueField = "LocalID"
        DropDownListBlockLocal2.DataTextField = "Local"
        DropDownListBlockLocal2.DataBind()


MySql.Data.MySqlClient.MyqlException: Parameter '@xxx' must be defined

This error is perplexing but it is cause by a change in default behaviour of MySQL version 5.2.0+

Adding "Allow User Variables=True" to the connection string will solve this.


Add a ComboBox in a DataGridView Cell

Dim GVCell As New DataGridViewComboBoxCell
GVCell.Items.Add("One")
GVCell.Items.Add("Two")
Me.DataGridView2.Item(ColNo, RowNo) = GVCell

Type Application.DataSet is not defined

This can be an infuriating error becuase it happens after using the Dataset wizard / designer in Visual Studio and the error comes from the desiner generated code.

It is usually caused because in the solution threre is an object with the same name as the project /solution.

For example if the project is called CustProj and there is a CustProj.vb class also.

Renaming the class will solve the problem

DataGridViewComboBoxCell value is not valid error

This error occurs on a DataGridView when it is populated using and .xsd dataset and placing a combo box on a foreign key to the datagrid table.

Strangely the DataAdapter is not automatically filled in these cases and it must be done manually through code.

The solution is to manually fill the data adapter of the Foreign key table. This was not being automatically filled and this was the cause of the problem.

In the Page_Load Event:

Me.TblUserTypesTableAdapter.Fill(Me.DonateDataSet.tblUserTypes)

Textbox Set Focus and Select on ASP.NET page

TextBoxName.Focus()
TextBoxName.Attributes.Add("onfocusin", " select();")


Client Side Confirm of a Button Click

Some buttons like "Delete Everything" you want to make sure have not been pressed accidentally

You can bring up a client side confirmation message by placing the following in the page_load event

ButtonDel.Attributes.Add("OnClick", "return confirm('This will delete everything. Continue ?');")

If the OnClick client side event returns false the codebehind code for the button click will not execute.


Getting the User's Time Zone in ASP.NET

It is common practice to set servers to GMT. You can be pretty sure that not all your users will be in the same time zone as the server.

So how can you display the time to a user in their own time zone?

It's best to create a seperate .aspx page to do this say TimeZone.aspx

This Java script function gets the time offset from GMT of the time in the users browser. Place this in the <HEAD> section of the page. You will also need to place an asp textbox TextBox1 on the page.

 <script language="javascript">
         function GetUTCOffset()
        {
            var dt = new Date()
            var offset = dt.getTimezoneOffset();
            document.form1.TextBox1.value = offset   
         }
 </script>

At the bottom of the page after the </form> tag place the following javascript to automatically submitt the page

<script language="javascript">
       document.forms[0].submit();
</script>

Then in the page load event of the page

If IsPostBack = False Then
     Page.ClientScript.RegisterStartupScript(Me.GetType, "MyFunction", "GetUTCOffset();", True)

Else
     Session("TimeZoneOffset") = TextBox1.Text
     Response.Redirect("Default.aspx")
End IF

The session variable will contain the offset (in minutes) from GMT and can be added to the server time to display the users time in the correct time zone.


Acessing Folders Above the Root Folder in ASP.NET

If you try to set the Directory path up from the application root folder like this ... FileManager1.RootDirectories(0).DirectoryPath = Server.MapPath("../../FilesFolder")

The following error occurs
"Cannot use a leading .. to exit above the top directory"

However there is a work around by setting the path as follows:
FileManager1.RootDirectories(0).DirectoryPath = Server.MapPath("") & "..\..\FilesFolder"

By counting the levels deep the app path is it should be possible to set the root folder to any other folder on the same drive as the application.

**** Note this does not seem to work in VS2008 Cassini web server - but does work under IIS 6 (haven't tried IIS7)

A better solution is to create a vitual directory on the path you are trying to access.


Setting a Default Button on an ASP.NET page.

It used to require messy javascript but in .NET 2.0 and 3.5 the and controls have a DefaultButton property that allows you to set the button that will be used when the Enter key is pressed.



The Controls collection cannot be modified because the control contains code blocks (i.e. )

This error is most often caused by having javascript in the section of a masterpage. Removing this scirpt or placing it with a tag can resolve the problem



Visual Studio Edit Macros error "Interface not Registered"

This error is strangely caused by some registry settings disappearing.

Creating these following registry entiries will cure the problem.

HKEY_CLASSES_ROOT\Interface\{0000000C-0000-0000-C000-000000000046}
  (DefaultValue)="IStream"
HKEY_CLASSES_ROOT\Interface\{0000000C-0000-0000-C000-000000000046}\NumMethods
  (DefaultValue)="14"
HKEY_CLASSES_ROOT\Interface\{0000000C-0000-0000-C000-000000000046}\ProxyStubClsid32
  (DefaultValue)="{00000320-0000-0000-C000-000000000046}"



Accessing Session Variable from a .DLL

The normal way to access do this in a website project object is to use the following:

HttpContext.Current.Session("RefererId")

Fear not,  it works - all you need to do is set a reference in your project to the System.Web.dll 

This can be done by right clicking the project in solution explorer and selecting Add Reference... 



Setting a Web Reference in a VB.NET dll

In a lot of places people tell you that this is easy but in Visual Studio 2008 there is no "Add Web Reference" function if you are in a class Library.

As usual it's easy if you know how.

1. Right click on your project and select the "Add Service Reference" Option

2. On the dialog box click the "Advanced ..." button.

3. On the next dailog click the "Add Web Reference ..." button.

You will then see the normal add web reference dialog that is available in web sites and applications. Surprisingly after you add the first web reference the option to add more becomes available in project contect menu.



Redirect to a new window in ASP.NET

Strangely there is no standard way of doing this in ASP.NET. 

Response.Redirect("NewPage.aspx")

will always move the current window to a new page. Surprising that the .NET framwork that can do a million things that you never need to use can't do some thing like this that you need to use all the time.

There is a work around however that uses the creation of some java script to open a new window

Dim ViewScript as String
ViewScript = "window.open('http://www.bms.com.au');"
Response.Write(ViewScript)



HTML Editor you can use in your aspx pages

FreeText box is a great free HTML editor that you can use to give your users that ability to edit html right inside your appliation. No knowlege of HTML tags is required.

You can get free text box fromhttp://freetextbox.com/

:NEWSBREAK!!! There is now also a cool edtitor that come free with the May 2009 release of Microsoft Ajax Control Kit as well as many other cool controls.



Setting a Value to Null in SQL Server Management Studio

If you just press delete to and set a field to blank you will get an Invalid Value for the cell if it is not a text type.

Ctrl 0 will set the value of a cell to NULL.




Installing Starter Kit .vsi files

The new ASP.NET start kits can be very confusing - for example the classified add starter kit class.vsi.

You download class.vsi run it and nothing seems to happen - I would have expected at least to get soem instructions or a readme displayed.

What has acutally happened is a new project type gets installed in Visual Studio 2008 (and 2005) and you can create the starter kit project directly from VS 2008.



Google IP Address Gadget

Very useful

However if you try this in a standalone DLL HttpContext is not defined.


 

 


   © Copyright 2009 Business Model Systems (Aust) Pty Ltd. All Rights Reserved 
L