Forcing Data Types on literal constants

The following type characters can be used to force a literal value to be a certain data type


"a"c 'Char
#1/1/1900# 'Date
0D 'Decimal
0@ 'Decimal
0.0R 'Double
0.0# 'Double
0I 'Integer
0% 'Integer
0L 'Long
0& 'Long
0S 'Short
0.0F 'Single
0.0! 'Single

See MSDN for more details

PrintPreviewControl

If you are using the PrintPreviewControl and what the page displayed to be refreshed with a new document, you must call the PrintPreviewControl.InvalidatePreview method. In 1.1 this did it automatically everytime a document was set, but now you have to do it manually, thats progress for you!

StatementPrintPreviewControl.Document = aDocument
StatementPrintPreviewControl.InvalidatePreview()

T-SQL Update with Inner Join

Below is an example of an Update that has an Inner Join

UPDATE 
    Taskevent
SET 
    RebateProcessed = 1
FROM
    TaskEvent
INNER JOIN
    TaskEventAsset 
    ON TaskEvent.Id = TaskEventAsset.Id
WHERE
    PatientId = @PatientID
AND
    JobDate < @PriorTo
AND
     ConcentratorId = @ConcentratorId

SQL Grant Execute

Found an article that had a nice SQL script to assign execute permissions to a user in SQL2000

Here is the script

DECLARE @sql nvarchar(4000)

DECLARE @db  sysname ;
SET @db = DB_NAME()

DECLARE @u   sysname ; 
SET @u = QUOTENAME('')

SET @sql ='select ''grant exec on ''   
        + QUOTENAME(ROUTINE_SCHEMA) + ''.''
        + QUOTENAME(ROUTINE_NAME) + '' TO ' + @u
        + ''' FROM INFORMATION_SCHEMA.ROUTINES ' + 
        'WHERE OBJECTPROPERTY(OBJECT_ID(ROUTINE_NAME),''IsMSShipped'') = 0'

EXEC master.dbo.xp_execresultset @sql,@db

CreateUserWizard – CreatedUser Event

When the CreatedUser event is fired and the LoginCreatedUser is set to true, the new user is only logged in once the event is completed! To get the new user information use the following code…

using System.Web.Security;
...
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    TextBox serialNumberTextBox = 
        (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("SerialNumberTextBox");
    SerialNumber serial = new SerialNumber(serialNumberTextBox.Text);
    serial.UserId = Membership.GetUser(CreateUserWizard1.UserName).ProviderUserKey.ToString();
    serial.DateRegistered = DateTime.Now;
    serial.Save();
}

CreateUserWizard – ErrorMessage

I can’t find anyway of using the ErrorMessage that is part of the CreateUserWizard. I can FindControl OK and set the values, but when it is rendered to the page its blank. Had to resort to adding my own literal to the page so that I can see my errors.

Remember "null" is not the same as "DBNull"

When supplying null values to stored procedures, remember that you have to set the value to DBNull…

if (_UserId == null)
{
    cmd.Parameters.Add(new SqlParameter("@UserId",System.DBNull.Value ));
}
else
{
    cmd.Parameters.Add(new SqlParameter("@UserId", _UserId));
}

Download content rather than view it in a Browser

Add the following line to ensure the user is prompted to download a file

Response.AddHeader "content-disposition","attachment; filename=fname.ext"

ASP.Net Membership Configuration

To configure an existing site to use ASP.Net Membership, but storing the data in the site database rather than creating an MDF in the APP_Data folder.

Run the utility c:\windows\microsoft.net\framework\v2.0.50727\aspnet_regsql

Follow the wizard to connect to the relevant server/database, this will need to be repeated for both test and production.

In the <system.web> section of the web.config ensure that an entry as below is included…

<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
  <providers>
    <clear />
    <add
      name="SqlProvider"
      type="System.Web.Security.SqlMembershipProvider"
      connectionStringName="SecurityConnection"
      applicationName="CHANGETHIS"
      enablePasswordRetrieval="false"
      enablePasswordReset="true"
      requiresQuestionAndAnswer="true"
      requiresUniqueEmail="true"
      passwordFormat="Hashed" />
  </providers>
</membership>

In the <connectionStrings> section add the appropriate connection string with a key of “SecurityConenction”

While editing the site within VS2005, from the WebSite menu selecti ASP.Net Configuration. This will open a web page that allows you to create users and rules for the application. Rules that are defined are stored in the Web.Config, so do this for the development site only, as you won’t have access to the live config file. Any folders with restricted access will also have a web.config file created in them, these will need to be promoted to live.

To release to production, change your security connection to point to the production database, set up the relevant users, but not the rules. Then when promoting code to production manually update the root config with the required authorisation, membership and connection strings, that can be taken from the development config file.

TryCast – better than DirectCast?

In VB.Net Framework V2.0 a new command was introduced called TryCast It works the same as DirectCast but if the cast fails instead of throwing an exception the object is set to Nothing

Take a look at MSDN for more details.

Previous Entries