Microsoft Study Bible

November 25, 2009

SQL Server Compact on Windows 7 x 64-bit

Today, when I use Visual Studio 2008 to develop a simple SQL Server Compact 3.1 application on Windows x64 platform, throws the following exception:

Message: “An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0×8007000B)”

StockTrace:

  StockTrace

  System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0×8007000B)

  at System.Data.SqlServerCe.NativeMethods.CreateErrorInstance(IntPtr& pError)

  at System.Data.SqlServerCe.SqlCeCommand..ctor()

at WindowsApplication1.NorthwindDataSetTableAdapters.EmployeesTableAdapter.InitAdapter() in D:\Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\NorthwindDataSet.Designer.cs:line 6400

at WindowsApplication1.NorthwindDataSetTableAdapters.EmployeesTableAdapter.get_Adapter() in D:\Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\NorthwindDataSet.Designer.cs:line 6324

at WindowsApplication1.NorthwindDataSetTableAdapters.EmployeesTableAdapter.Fill(EmployeesDataTable dataTable) in D:\Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\NorthwindDataSet.Designer.cs:line 6607

at WindowsApplication1.Form1.Form1_Load(Object sender, EventArgs e) in D:\Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs:line 29 

at System.Windows.Forms.Form.OnLoad(EventArgs e)

at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)

at System.Windows.Forms.Control.CreateControl()

at System.Windows.Forms.Control.WmShowWindow(Message& m)

at System.Windows.Forms.Control.WndProc(Message& m)

at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Think it carefully .SQL Server Compact 3.5 SP1 just can support Win7 x 64 platform, SQL Server Compact 3.1 and 3.5 should run only through WOW64 mode .WOW64 is an Emulation Layer which allow 32-bit applications run on 64-bit platform. This seems that the 32-bit applications run on the 32-bit platform.

That problem is familiar on the internet .Generally speaking ,the answer is to install 64-edition SQL Server Compact 3.5 SP1.However ,what I need to resolve is SQL Server Compact 3.1 .What should we do ?

It is very simple. “Any CPU” is the default target platform Visual Studio 2005/2008 creates the project on. we just change it to be “x86”.So,the complied program will run through WOW64 mode.

October 28, 2009

LINQ to SQL common BaseClass

LINQ is a series of new features provided by Visual Studio 2008, which will expand C# and Visual Basic and can provide powerful query capabilities. As part of LINQ, LINQ to SQL can provide a framework on which the relational data is used as object to run. In one way, it is equal to NHibernate and Castle provided by Microsoft. When we need to access the database, LINQ to SQL will become our first choice.

In LINQ to SQLall varies in relational database model are strongly typed ,which can provide verification and intelisense when they are being complied .And we can use Query expression including query syntax and method syntax, to access data from the database.

 

1. Achieve where method

 

 Howeverthe strongly-typed is not beneficial to do abstract operation on the data .So, the developer had to define some particular class for each entity object, which will cause to a lot of duplicate code .If we can achieve a common BaseClass and encapsulate Public Data operation such as Select ,Where ,Add ,Update and Delete ,it can be useful for us to develop the N-tier application development.

Fortunately, Generics Types can help us make it .The way is to call GetTable<T>() method of  DataContext .For example ,we can achieve where method by passing a Lambda expression to find the result we want to get.

 public IList<TEntity> Where(Func<TEntity, bool> predicate)
{
    InitDataContext();
    
return
m_context.GetTable<TEntity>().Where(predicate).ToList<TEntity>();
}

 

Andwe can even use Dynamic Query to expose some methods to receive conditional expression

public static class DynamicQueryable
{
    
public static IQueryable<T> Where<T>(this IQueryable<T> source, string predicate, params object
[] values)
    {
        
return
(IQueryable<T>)Where((IQueryable)source, predicate, values);
    }
public static IQueryable Where(this IQueryable source, string predicate, params object
[] values)
    {
        
if (source == null) throw new ArgumentNullException(“source”
);
        
if (predicate == null) throw new ArgumentNullException(“predicate”
);
        LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType,
typeof(bool
), predicate, values);
        
return
source.Provider.CreateQuery(
            Expression.Call(
                
typeof(Queryable), “Where”
,              
                
new
Type[] { source.ElementType },
                source.Expression, Expression.Quote(lambda)));
    }
}
public IList<TEntity> Where(string predicate, params object
[] values)
{
    InitDataContext();
    
return
m_context.GetTable<TEntity>().Where(predicate, values).ToList<TEntity>();
}

 

Data Entity operation

Of course, for an AbstractBaseClass, there is no problem to query .Because there is no need to care the properties of the entity or the composition of the expression Lambda when we call these methods. (more…)

October 26, 2009

What does ASP.NET 3.5 Extensions bring to us?

Filed under: Developer tools and applications — Tags: , , , , — Jackson @ 9:32 pm

1. Overview

The release of the new technology .NET 3.5 and Visual Studio 2008 makes milestone contribution to the .NET strategy of Microsoft. In the Web development, it includes a powerful HTML Web designer which provides split-view editing, and the perfect CSS. Meanwhile, it makes its support to JavaScript better, including Intelligent hints and debug.

 

Microsoft issued the first preview version of ASP.NET 3.5 Extensions only 20 days after it released .NET 3.5. The final version is released in the first half of the year 2008, which contains more features of ASP.NET:

1. ASP.NET MVC Framework

2. Improvement of ASP.NET AJAX

3. ASP.NET Dynamic Data support 

4. ASP.NET Silverlight support

5. ADO.NET Data services

 

In this article, I will display some of the new features of ASP.NET 3.5 Extensions though several simple examples.

2. ASP.NET MVC framework

The concept of MVC has been raised for many years. It divides the application implementation into three parts: Model to maintain the data status, View to display the user interface, Controller to handle the user interaction, operate Model and select View to display the data. The first concern of ASP.NET MVC Framework is separation for testing more conveniently. Meanwhile, it uses aspx page, template page and user control as View and provides powerful URL Routing Engine. I will show these features by examples.

 

Step1: set up Model, use LINQ to SQL to establish a Product data model

 

 

 

Step2 set up View, display the detailed information of Pdoduct by using an aspx page which inherited from a Generic ViewPage

 

public partial class Product_Product : ViewPage<ProductInfo>
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
 

 

 

 

   show the data in aspx page, use <%=%> directly to label output 

<div>
<h2>ProductDetail:</h2>
ProductId

<%= ViewData.ProductId %><br /><br />
Name
<%= ViewData.Name %><br /><br />
Descn
<%= ViewData.Descn %>
</div>
 
 

 

 

Step3set up Controllerinherit from Controller base classand add Actionat the same time, use product view to realize the data function and input data object that needs to be displayed.

public class ProductController : Controller
{
[ControllerAction]
public void Index()
{
MSPetShopDataContext db = new MSPetShopDataContext();
ProductInfo productinfo = db.ProductInfos.Single(p => p.ProductId == “BD-03″);
RenderView(”Product”, productinfo);
}
}
 

 

 

 

Step4select Path for Configurationmake configurations Application_Start

 

void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(
new Route
{
Url = “[controller]/[action].mvc”,
Defaults = new { action = “Index”},
RouteHandler = typeof(MvcRouteHandler)
}
);
}
 
 

 

basicly, the proceedings are the four steps for setting up a application based on ASP.NET MVC Framework.

   


3. ASP.NET AJAX improvement
 
 

 

In the ASP.NET 3.5 Extensions, the most important improvement to ASP.NET AJAX is a better support to browser history that we can control the Back and Forward button more conveniently. It provides two ways for us to choose, using server-side control or Client-script. In the following, I will illustrate how to use server-side control to control the browser history.


       add ScriptManager Control
and set its EnableHistory attribute as truepermit the history management of browser and OnNavigate handled as OnNavigateHistory function for dealing with navigation event. At the same time, set the EnableStateHash attribute as false to make modifications easier to perform. In real situations, we can decide whether to perform the Hash encryption as required:

 

<asp:ScriptManager runat=”server” ID=”ScriptManager1″
OnNavigate=”OnNavigateHistory”
EnableHistory=”true”
EnableStateHash=”false” />

 

Create Browser history point at the time of clicking button with the way of AddHistoryPoint

 

 

public void ButtonClick(object sender, EventArgs e)
{
LabelHistoryData.Text = ((Button)sender).Text;
ScriptManager.GetCurrent(this).AddHistoryPoint(key, LabelHistoryData.Text);
} 

 

Navigation processing

public void OnNavigateHistory(object sender, HistoryEventArgs e)
{
LabelHistoryData.Text = Server.HtmlEncode(e.State[key]);
} 

 

For demonstrationthree button should be added on the page

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="Button3" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Panel runat="server" CssClass="box" ID="Content" Height="40px">
Date and Time:
<%= DateTime.Now.ToLongTimeString() %>
<br />
Page's refresh state:
<asp:Label runat="server" ID="LabelHistoryData" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<p />
<asp:Button runat="server" ID="Button1" Text="Key 1" OnClick="ButtonClick" />
<asp:Button runat="server" ID="Button2" Text="Key 2" OnClick="ButtonClick" />
<asp:Button runat="server" ID="Button3" Text="Key 3" OnClick="ButtonClick" /> 

 

  

In the preceding, I mainly use the server-side approach to realize the support to browser history in ASP.NET AJAX, you can also use client script to manage browser history.

 

 

 

 

 

 

August 31, 2009

Visual Web Developer Express vs Visual Studio 2008?

I am currently reading Beginning ASP.NET with C# and it recommended using Visual Web Developer Express(VWD).But ,I have a battery of questions.Could I use Visual Web Developer Express for all the projects I am planning to do in Visual Studio 2008?Can the Free Express edition satisfy my needs?Or, Is there a need to buy Visual Studio even though I have a VWD?Could you tell me What I can do or can not when I just use the Visual Web developer Express 2008?

If you read this ,do you still recommended me to use UWDE,or any reasons ?
However,I was just thinking about what the Express version lacks ,compared with the Visual Studio.

1.Not Extensibile with other add-ons or third party tools.If I use Visual Web Developer 2008 Express Edition ,I can’t install and use Web Application Projects ,CSS Properties Window,HTML/ASP.net Spell Spell Checker, ReSharper etc like the third party tools.
2.Can not add a Class Library project. If you want to add a Class Library project or a Web Controls Library project to the solution,to be surprised ( Class libraries are popular way to sharing business logic code or any other utility code. ),you can not manage it . Because VWD Express 2008 only supports a single type of project: Web site.Also,VWD 2008 SP1 allows Web Application and Class Library Projects in the solution.
3.Lack of Source Code Control,while a good Source Code Control system can provide us with change history ,branches ,merges ,etc.SCC is key for a professional developer in team development.
4. no Accessibility checker.It is indispensible for you to develop Web sites that must be accessible.
5.Lack of the ability for automatic generation of resources for localization.This means that your Web can not be localized in multiple languages.
6.You want to attach debugger to a process in VWD Express ,sorry you can’t.For example,when you need to step through code in existing classic ASP pages in order to understand how it works or you have to maintain classic ASP pages, you need ASP debugging which requires ability to attach debugger to a running process, the Visual Web Developer Express Edition can not help you .
7. No Native code debugging.For example ,some legacy code, especially in classic ASP code may be using COM objects written in C++. Mixed mode debugging is not supported in Express.
8.Not support opening or editing SharePoint Web sites.
So much you can not do ,maybe you will give up the free Express edition.You will find Visual Studio 2008 more favorable.

However,VWD Express 2008 still has its merits.
The obvious advantage of VWD over VS2008 is that it is free and if you can work smart with it given the missing features, it may be the more pragmatic option for you. If those are features that you can’t live without, VS2008 may be a wise investment - you also get all of the features missing from other Express products (Visual Basic 2008, Visual C# 2008, etc).

In addition,Visual Web Developer Express 2008 is a free web tool that allows you to build CSS, HTML ASP.NET, C#, VB, and JavaScript and supports additional frameworks like ASP.NET MVC, AJAX, Silverlight and jQuery.

So,what should I choose ?Or,I can have a cake and eat it too ?

Powered by WordPress

Close
E-mail It