Friday, February 29, 2008

How to check if class has been used in Web or Windows application

After a while, I decided to participate in a newsgroups tonight and found an interesting question. "Is there a programmatic way for the class lib to determine what kind of app is running - web or windows". The worst, guy requested official response from Microsoft support and didn't get an answer. After 3 days his question wasn't answered, he re-posted it. So, is there are something wrong? Looks like not.
The simple answer from my side was, to implement following function somewhere in his shared assembly:
public bool IsWebApp(){
return (HttpContext.Current != null);
}

So, if there is Current HttpContext than function has been called from ASP.NET application and if there is not such an object, function is called from Windows Forms application.
Simple enough.

Tuesday, February 26, 2008

Handling Miliseconds in .NET and SQL Server

Have you taken a look at number of such a questions in online community? It looks like that a lot of .NET developers have a problems reading and updating milliseconds value from SQL Server databases. So what is a real problem here?
Let's take a look at common scenario. Developers read data from a table which has Datetime column ColDateTime. Let's say they are storing values in some DataTable table1. Suddenly, if you want just to print a Value of that field, with

table1.Rows[0].Cells["ColDateTime"].Value.ToString()

you will get value like '2008/02/26 2:26:53 PM'. But you value in database is actually '2008/02/26 14:26:53.3480'. So milliseconds are missing. The only way to get them is to convert value from database in DateTime variable and then print out that variable:

DateTime dtDbValue = System.Covert.ToDateTime(table1.Rows[0].Cells["ColDateTime"]);
dtDbValue.ToString();


Now, you have a real value: '2008/02/26 14:26:53.3480'

Connected to this is also reverse process, where people lose milliseconds during update. Take a look at following statements:

DateTame dtValue = DateTime.Now;

string sql = "update table1 set ColDateTime = '" + dtValue.ToString() + "'";

-- create DataCommand, assign CommandText and execute.

You will see that your column has been updated with the wrong value. Again milliseconds cut.

People try very often to format ToString method call to looks like:
string sql = "update table1 set ColDateTime = '" + dtValue.ToString("yyyy/MM/dd hh:mm:ss.ffff") + "'";

but this cause an exception on SQL Server side saying that varchar value can not be converted to Datetime type. Strange, and maybe lack of documentation, but the solution is to format your DateTime value with different format - using 3 f and not 4:

string sql = "update table1 set ColDateTime = '" + dtValue.ToString("yyyy/MM/dd hh:mm:ss.fff") + "'";


Compare databases fast, safe, free... DBTyP.NET - in version 2008 supports Oracle besides SQL Server and MySQL. DBTyP.NET 2008 is coming... in 3 weeks.

Saturday, February 2, 2008

Great Help Authoring Tool

We have just discovered and get impressed with Rolehelp. We are still testing different help authoring tools but this one with their simplicity, great user interface, feature reach capabilities and creation of 7 different help formats simply fascinate us. Of course, we have never wanted to spend 1000$ for such a tool, therefore Roledata offer of 59$ and free demo version with limitation of 50 topics is very, very attractive one. So, if you need fast, simple, free or at least cheap help authoring tool, visit them and you will get one great tool.
I'm sure we will use it in our data and schema compare solution in version 2008.




Synchronize and compare the structure and data of your SQL Server, Oracle, and MySQL databases - DBTyP.NET by BYPsoft

Tuesday, January 29, 2008

MSDN Launches Code Galery

We found very interesting and wanted to share it with all of you that MSDN launches yesterday Code Gallery dedicated to developers community for sharing samples, tools, articles, participating in discussions...
Code Gallery has been used till now by internal Microsoft employess for sharing code samples and tools and now is availalble to the community. Anyone with LIVE ID can post there. Let's see how this will work.

Sunday, January 27, 2008

Primary keys without "real" tables

Have you queried some when information_schema.table_constraints or sys.key_constraints to discover a primary keys in your database? And as a result from select * from information_schema.table_constraints you noticed that there are primary keys without TABLE_SCHEMA and TABLE_NAME in resultset?
You can be confused with this but the explanation is simple. Your primary keys defined in table-valued functions have been listed here. So if you have a function defined as

CREATE FUNCTION [dbo].[fnTest]
(
@foo int
)
returns @resultTable table (test1 int, test2 int primary key (test1))
as
begin
insert into @resultTable(test1, test2)
select 1, @foo

return
end

you will have as CONSTRAINT_NAME something like "PK__fnTest__2D27B809" and TABLE_SCHEMA and TABLE_NAME will be NULL.

Friday, November 23, 2007

Visual Studio 2008 - Automatic Properties in .NET 2.0

In the next couple of blogs we will present shortly some cool and interesting stuff we found about Visual Studio 2008 and .NET Framework 3.5. For all cool stuff see Visual Studio 2008 Official page.
One of the first items we noticed and played around are Automatic Properties. What does it mean? Untill now it was common to define a class as
public class Test
{
public Test() { }

private string _firstName;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
}
where for each property you usually had a private member variable.
But now, Visual Studio 2008 and .NET framework 3.5 introduces Automatic Proeprties where your class can be defined as

public class Test
{
public Test() { }
public string FirstName { get; set; }
}
and you will notice that a private member variable is missing and a whole body of the property implementation itself (it has just a stub definition get; set; )
Imagine how much you reduce a time for class scripting. Readability is also much better.
But you will probably ask, where does .NET keep your data now? If you take a look at IL you will find that actually a compiler generates a field (variable) for you:
'<>k__AutomaticallyGeneratedPropertyField0'. Whole construction is done just by using CompilerGeneratedAttribute. But, what we had in mind, this attribute is part of .NET Framework 2.0, what lead us to... Hey, Automatic Properties are possible in .NET 2.0 than?! And this is really true. Open your Visual Studio 2008 (Express), create such a construction, as a Target Framework for a project choose .NET Framework 2.0 and you will see it runs. WOW!!! We like this very much and will use it in DBTyP.NET as soon as possible.

Monday, November 12, 2007

Formatting bounded DateTime fields in ASP.NET

If you ever tried to work with BoundField object bound to a DateTime field with the DataFormatString, it must happen, that first time you didn't get your DateTime value proeprly formatted.
<asp:BoundField DataField="MyNotFormattedDate" DataFormatString="{0:MM/dd/yyyy}" />

Looks like that you have done everything correct but still getting your value formatted using its ToString() method like "11/13/2007 10:05:12 PM". So, the problem is not in your definition but in ASP.NET which tries to prevent cross site scripting attacks, the field value is HTMLEncoded. And HTMLEncoding occurs before applying any formatting, making your formatting string without effects. To Get your field formatted as you define, you should tell object not to use HTMLEncoding like

<asp:BoundField DataField="MyFormattedDate" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false"/>

Compare databases with the best tool - DBTYP.NET Studio.