Nik's Technology Blog

Travels through programming, networks, and computers

No defining declaration found for implementing OnValidate(System.Data.Linq.ChangeAction)

If you happen to be getting an error message like the one below, then read on.

Error    1    No defining declaration found for implementing declaration of partial method 'mvcCMS.Models.WebPage.OnValidate(System.Data.Linq.ChangeAction)'    C:\mvcCMS\Models\WebPage.cs    28    22    mvcCMS


I'm using LINQ to SQL designer in Visual Studio to create a database schema and I'm using a partial class to extend the code generated by the designer.

In the example below I am using the pattern used by NerdDinner.com to add business rules/validation to the model classes LINQ to SQL built based on my database schema.

namespace mvcCMS.Models
{
    public partial class WebPage
    {
        public bool IsValid
        {
            get { return (GetRuleViolations().Count() == 0); }
        }
        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (String.IsNullOrEmpty(Title))
                yield return new RuleViolation("Title is required", "Title");
            if (String.IsNullOrEmpty(Text))
                yield return new RuleViolation("Web copy is required", "Text");

            yield break;
        }
        partial void OnValidate(ChangeAction action)
        {
            if (!IsValid)
                throw new ApplicationException("Rule violations prevent saving");
        }
    }
}

Where OnValidate() is a partial method LINQ to SQL provides which enables us to be notified when the object is about to be persisted to the database, so we can check all our business rules have been met before the object is flushed to the database.

An empty OnValidate() method is part of the designer generated code for your data class located in the #region Extensibility Method Definitions and it seems that these Extensibility Method Definitions only get added to the designer code when your tables have primary keys.

When a table is dragged onto the Object Relational Designer in Visual Studio the classes that are generated will only implement INotifyPropertyChanging and INotifyPropertyChanged if your tables have primary keys.  If the classes don't implement these interfaces the code won't implement the OnValidate() method, and if the OnValidate() method doesn't exist your partial class won't compile.

The Solution

The solution is simple.  Add a primary key to your database table, delete the associated data class from the Object Relational Designer and then drag the database table from Server Explorer back onto the Object Relational Designer surface.

You should then find the designer generated code now implements INotifyPropertyChanging and INotifyPropertyChanged and the class contains a definition for OnValidate() in the #region Extensibility Method Definitions.  Your code should now compile.

Language Interoperability in the .NET Framework

.NET is a powerful framework which was built to allow cross-language support.  All .NET code is compiled to Intermediate Language (IL) whether you are developing in C#, VB.NET, J# or any other .NET language.  This means it is possible to build applications with modules written in different languages, because when the application is compiled it will all be compiled to a common language, IL.

This means a class written in one language can inherit from a class written in another language, or an object can directly call a method of another class written in another .NET language.  Visual Studio also allows you to step through all the different modules in the debugger.

To demonstrate the Language Interoperability in .NET, I'm going to call a VB.NET method from the code-behind page of a C# ASP.NET page.  This isn't something you would ordinarily want to do, but its an easy way to show the power of .NET.

In Visual Studio, right-click your website root in Solution Explorer and select add reference.  Scroll down the .NET tab until you find Microsoft.VisualBasic and click ok.

This will modify your Web.Config file by adding an assembly reference to Microsoft.VisualBasic to the assemblies list.

            <assemblies>
                <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Extensions.Design, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
      </assemblies>

To use the VB.NET language within your C# webform you need to add a using Microsoft.VisualBasic; statement at the top of your code-behind page.

In my demo example below I am going to use the VB.NET MonthName method of DateAndTime to get the current month's name and display it on my page using a ASP.NET label control.  The bold code below is VB.NET interspersed with C#.

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;

public partial class language_interop : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int month = DateTime.Today.Month;
        string monthName = DateAndTime.MonthName(month, false);
        labelMonthName.Text = monthName;
    }
}

TargetInvocationException - Exception has been thrown by the target of an invocation

This exception isn't very useful because it's a general exception thrown when a method invoked through reflection throws an exception, took me a while to figure out what the issue was.  Even though I knew the page causing the error.

This exception was thrown by a ASP.NET web form which contained a GridView connected to an ObjectDataSource.

The ObjectDataSource references methods in a data access layer class.  These methods then call stored procedures in the MS SQL database. 

The code worked perfectly in my development environment.

I have the SQL database set up so that the database user ASP.NET uses only has rights to execute the stored procedures it needs to.  The database user cannot run commands against the tables directly, this way I limit the surface area of attack should my application have a weakness that could be exploited.

I double checked the stored procedures were all up-to-date, then double checked all the permissions on the stored procedures, and everything seemed in order, but I still kept getting the stack trace below in Event Viewer:


    Stack trace:    at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance)
   at System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)
   at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
   at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
   at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
   at System.Web.UI.WebControls.GridView.DataBind()
   at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
   at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()
   at System.Web.UI.Control.EnsureChildControls()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Custom event details:

I then decided to check the stored procedures.  Then it dawned on me that one of the stored procedures used EXEC sp_executesql, which requires the database user to have, in my case, SELECT permissions on the actual table itself.

Once I granted these permissions to the database user, the web form loaded correctly.

ASP.NET Content Disposition Problem in IE7

I've just spent quite a while debugging a problem with content disposition I was having with Internet Explorer 7, the code works fine in Firefox but causes this error message to occur in IE7.

"Internet Explorer cannot download xxx from xxx."

"Internet Explorer was not able to open this Internet site.  The requested site is either unavailable or cannot be found.  Please try again later."

content-disposition-error

This was my original snippet of C# code:

Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = docToDisplay.Type.ContentType.ToString();
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(docToDisplay.FileName));
Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.BinaryWrite(docToDisplay.FileContent);
Response.End();
Response.Flush();
Response.Close();

Response.Cache.SetCacheability

I eventually figured out that the following line on code was causing the issue.

Response.Cache.SetCacheability(HttpCacheability.NoCache);

I then did a quick search for "Response.Cache.SetCacheability(HttpCacheability.NoCache);" and discovered another developer who have had the same Content-Disposition issue.  Unfortunately for me that page didn't get returned when I was searching for the Internet Explorer error message.

This was the response to the post by Microsoft Online Support:

"Yes, the exporting code you provided is standard one and after some further
testing, I think the problem is just caused by the httpheader set by
Response.Cache.SetCacheability(HttpCacheability.No Cache)
I just captured the http messages when setting and not setting the above
"NOCache" option and found that when the http response returned the
Cache-Control: no-cache
header. So we can also reproduce the problem when using the following code:
page_load...
{
Response.CacheControl = "no-cache";
ExportDataGrid(dgSheet,"test.xls");
}
IMO, this should be the clientside browser's behavior against "no-cache"
response with stream content other than the original text/html content. So
would you try avoid setting the CacheAbility or the "Cache-Control" header
to "no-cache" when you'd like to output custom binary file stream?
Thanks,
Steven Cheng
Microsoft Online Support"

After removing the Response.Cache.SetCacheability line the file downloads correctly in Internet Explorer.

Create a Simple Windows Service to Request a URL at Set Intervals

I needed a simple Windows Service to request a web page at set intervals indefinitely. Windows Services are the best way of doing this as they have the ability to start automatically when the computer boots up and can be paused, stopped and restarted. You can also get them to write events to the Windows Event log.

I found this Windows Service sample tutorial on The Code Project and downloaded the code to familiarise myself with the basics. The tutorial lacked a timer and the code to request a URL though so I had to add this functionality.

Visual Studio Standard edition doesn't have a Windows Service template, but you can still create a Windows Service, you just need to do a bit of extra work.

After some research and a bit of coding I added two new methods:

private void ServiceTimer_Tick(object sender, ElapsedEventArgs e)
{
this.timer.Stop();
DoWork();
this.timer.Start();
}

void DoWork()
{
WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

Stream data = client.OpenRead(URL to request here);
client.Dispose();
data.Dispose();
}

I overrode the OnStart() method of ServiceBase to enable the timer and start it. I also overrode the OnStop() method to disable the timer.

The DoWork() method simply creates an instance of WebClient and reads in the URL you want to request.

Then in the constructor I set the timer interval and added an event handler to raise the ServiceTimer event when the interval elapses. The event handler simply stops the timer, calls the DoWork() method and then restarts the timer.

public static void Main()
{
ServiceBase.Run(new Service3());
}

public Service3()
{

InitializeComponent();
// Duration 1 hour
double interval = 3600000;

timer = new Timer(interval);
timer.Elapsed += new ElapsedEventHandler(this.ServiceTimer_Tick);

}

To install the Service you need to publish the project in Visual Studio. Then use InstallUtil.exe following the process below:

  1. Open a Visual Studio .NET Command Prompt
  2. Change to the bin\Debug directory of your project location (bin\Release if you compiled in release mode)
  3. Issue the command InstallUtil.exe MyWindowsService.exe to register the service and have it create the appropriate registry entries
  4. Open the Computer Management console by right clicking on My Computer on the desktop and selecting Manage
  5. In the Services section underneath Services and Applications you should now see your Windows Service included in the list of services
  6. Start your service by right clicking on it and selecting Start

Each time you need to change your Windows Service it will require you to uninstall and reinstall the service. Prior to uninstalling the service make sure you close the Services management console. To uninstall the service simply reissue the same InstallUtil command used to register the service and add the /u command switch.

e.g. InstallUtil.exe /u MyWindowsService.exe

When you install the service on a server you can find the InstallUtil.exe in the .NET framework folder e.g. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

 

Authenticate with MailEnable SMTP using ASP.NET 2.0

I've recently been trying to send emails using ASP.NET 2.0 on a web server which uses MailEnable. MailEnable had relaying locked down to avoid opening the server up to spammers, but I wasn't able to authenticate with MailEnable to relay emails to external domains.

I tested my code was working by temporarily checking the "Allow relay for local sender addresses" checkbox in the SMTP properties on MailEnable and my email was relayed successfully. However as soon as I uncheck this option I get this error message in the SMTP W3C logs:

503+This+mail+server+requires+authentication+when+attempting+to+send+to+a+non-local+e-mail+address.+Please+check+your+mail+client+settings+or+contact+your+administrator+to+verify+that+the+domain+or+address+is+defined+for+this+server.

I eventually got it working. I changed the authentication type from "MailEnable integrated authentication" to "Authenticate against the following username/password." then I supplied a username and password to MailEnable.
After restarting the SMTP service in MailEnable. I was able to relay emails using the following C# ASP.NET 2.0 code snippet...


using System.Net.Mail;

MailAddress source = new MailAddress("admin@localdomain.com", "Server");
MailAddress recipient = new MailAddress("external@mail.com", "External");

MailMessage enquiryMail = new MailMessage(source, recipient);
enquiryMail.Subject = "Test";
enquiryMail.Body = "Test content";
enquiryMail.IsBodyHtml = true;

SmtpClient smtpServer = new SmtpClient("mail.localdomain.com");
smtpServer.Credentials = new NetworkCredential("username", "password", "localdomain.com");
smtpServer.Send(enquiryMail);

Product Review: uCertify PrepKit Exam Simulator

I was kindly sent a uCertify PrepKit for review back in December last year for the Microsoft C# .NET 2.0 Web-based Client Development exam (70-528). I'm looking to take the Microsoft MCTS .NET Framework 2.0 Web Applications certification this year, and needed an exam simulator and part of my study.

uCertify start-up logo

I've been so busy lately its been difficult to find the time to sit down and put the exam simulator through its paces. Anyhow I've spent a good few hours testing my .NET knowledge with this PrepKit to allow me to confidently evaluate it.

The PrepKit features a bunch of questions that closely follow the style of questions featured in the Microsoft exam, obviously the PrepKit does not contain real exam questions, but uCertify claim they are "realistic", and they are supposed to get you used to the kind of questions you should expect to see when you come to take the real exam.

uCertify PrepKit main menu

The tests in the PrepKit contain between 15 and 40 questions each and you’re given 120 minutes to complete each one, but I found that choosing a shorter time and reducing the amount of questions I needed to answer allowed me to spend more time using the PrepKit, because I don’t often have 2 hours of uninterrupted revision time.

There are two different modes to choose from before starting a test. Learn mode and Test mode, Learn mode allows you to get feedback on the current answer immediately whereas in Test mode you can only review the answers at the end of the practice test.

uCertify PrepKit test question page

When you complete a test you can review the questions and go back and look at any questions you may have answered incorrectly. You can also choose to re-take just the questions you got wrong. When you re-take the test the multiple choice answers change order to keep you on your toes!
During a test you can pause the timer to take a call, make a coffee etc, tag, print, review and bookmark questions.

uCertify PrepKit test history page

Every test you take with the PrepKit gets recorded in the Test History section, from here you can go back and review all the practice tests you've taken, review all the questions you got wrong, re-do the whole tests or re-do only the questions you got wrong.

Custom tests can also be created to turn your weaknesses, based on your test history or certain topics into your strengths.

Besides the practice tests the PrepKit contains study notes, quizzes and tips and flash cards to help assist you in understanding the topic.

Enumerate Available Database Providers in ASP.NET

Using the DbProviderFactories class in ADO.NET you can retrieve a list of available database factories using the GetFactoryClasses method. This can be useful if you host your ASP.NET site on a shared server and don't have access to the web server and machine.config file.

The output of the GetFactoryClasses method is a DataTable of available factory classes, these are the factory classes that the .NET runtime will have access to.

Here's the kind of output you'll get...

NameDescriptionInvariantNameAssemblyQualifiedName
Odbc Data Provider .Net Framework Data Provider for Odbc System.Data.Odbc System.Data.Odbc.OdbcFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
OleDb Data Provider .Net Framework Data Provider for OleDb System.Data.OleDb System.Data.OleDb.OleDbFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
OracleClient Data Provider .Net Framework Data Provider for Oracle System.Data.OracleClient System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
SqlClient Data Provider .Net Framework Data Provider for SqlServer System.Data.SqlClient System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Here's the code you'll need in your code behind file.

DataTable providersList = null;
providersList = System.Data.Common.DbProviderFactories.GetFactoryClasses();
GridView providerGridView = new GridView();
providerGridView.DataSource = providersList;
providerGridView.DataBind();
form1.Controls.Add(providerGridView);