Generic List , DataTable


ตัวอย่างการทำงานระหว่าง DataTable  , List

1.Query ค่ามาจาก DB อยู่ใน DataTable แล้ว convert ลงไปอยู่ใน List

ListColumnBase = (From dr In ds.Tables(0).Rows Select New String(dr(0).ToString())).ToList()

2.Compare ระหว่าง List

Dim result As IEnumerable(Of String) = ListColumnInFile.Except(ListColumnBase)

3.ColumnName ที่ได้มามีช่องว่าง เช่น “Account Number” ต้องใส่ปีกกา เป็น “[Account Number]”

ListColumnInFile = (From List In ListColumnInFile Select New String(“[” & List.ToString() & “]”)).ToList()

4.เอาค่าใน List มาต่อ String เพื่อไปใช้อย่างเช่นไปเป็น where condition

sql = String.Join(” varchar(max),”, ListColumnInFile)

5.Column Name ที่เป็น Schema อยู่ใน DataTable Cast ออกมาแล้วเอาชื่อ Column ไปใส่ใน List

ListColumnInFile = (From dc In dtSchema.Columns.Cast(Of DataColumn)() Select dc.ColumnName).ToList()

6. Select DataTable to List (Of String)

Dim _ListSecurityCode As DataRow()
_ListSecurityCode = DTSecurityCode.Select(“Portfolio = ‘” & Port & “‘ and Method = ‘” & Method & “‘”)

Dim _ListInSecurityCode As IEnumerable(Of String) = From row In _ListSecurityCode.AsEnumerable() Select row.Field(Of String)(“Security”)

Sequence install SQL Server and VS (Visual Studio)


You should install SQL 2008 Developer first, this will rule out the need for VS installing SQL which comes with it. Or you could do like others suggested and choose custom VS installation.
My favorite way is this:
  1. SQL 2008 Developer
  2. Visual Studio Professional 2008
  3. Run Windows Updates
  4. Install Resharper 🙂
  5. Install RedGate SQL ToolBelt
But it seems that to get some features to work the proper order is:
  1. Visual Studio Professional 2008 (with SQL Unchecked)
  2. SQL 2008 Developer
  3. Run Windows Updates
  4. Install Resharper/Redgate Tools
Resharper and Redgate in my opinion are far the best tools for developing in C#/SQL.

Datetime quarter


DateTime date;
int quarterNumber = (date.Month-1)/3+1;
DateTime firstDayOfQuarter = new DateTime(date.Year, (quarterNumber-1)*3+1,1);
DateTime lastDayOfQuarter = firstDayOfQuarter.AddMonths(3).AddDays(-1);

Web Service URL Dynamic in web config


How to make your Web Reference proxy URL dynamic

By 30 Nov 2005
 

Introduction

I have been asked before, how to make the URL property for a web reference to a web service in a project, configurable in a config file instead of compiled in the web reference proxy. This is most useful when you want to deploy a project with a web reference between different business environments (like between Test/QA and Production) without recompiling the project. There is a simple way to do this that requires no coding at all on the developer’s part (provided you are using Visual Studio for .NET).

Getting Started

To follow this article, first you need to have a compiled web service to reference, and a project in which you wish to add a web reference. After you have added your web reference (which creates the proxy class with references to the web service for you automatically using VS .NET), you need to set your solution view to ‘Show All’:
Solution Explorer Show All Files
This shows the Reference.cs file for the Reference.map. This is the proxy class file that VS.NET generates automatically for you when you add a web reference.
Open up this file and notice under the constructor for the proxy class that the URL is hard coded for you inside the constructor:
Sample screenshot

Changing the URL from Static to Dynamic

We are going to change the hard coded URL in the proxy class Reference.cs to a key in the web.config of the web service client project. An appSettings section will be added automatically with the current URL, and code will be placed in the Reference.cs proxy class constructor to look for the URL there. All this will be done by changing one property setting on the proxy reference.
If you look at the web reference properties (below):
Sample screenshot
you can see that there is a configuration setting called ‘URL Behavior’. This setting is by default set to Static.
Sample screenshot
To make the URL in the Reference.cs map class code behind look for the web service URL in your web.config file, we need to change this setting to Dynamic:
Sample screenshot
Doing this in VS.NET does two things for you. It changes the Reference.cs file to have the code to look for the WSDL URL in the project’s web.config file:
Sample screenshot
And it adds the URL as a key value to the projects web.config file under appSettings:
Sample screenshot
Now you can set the URL to different servers for deployment in different environments, without having to change the code. You just change the URL in your config file for your project.

http://www.codeproject.com/Articles/12317/How-to-make-your-Web-Reference-proxy-URL-dynamic

Difference Between Int32.Parse(), Convert.ToInt32(), and Int32.TryParse()


Introduction

Int32.parse(string)

Int32.Parse (string s) method converts the string representation of a number to its 32-bit signed integer equivalent. When is a null reference, it will throw ArgumentNullException. If is other than integer value, it will throw FormatException. When represents a number less than MinValue or greater than MaxValue, it will throw OverflowException. For example:
string s1 = "1234"; 
string s2 = "1234.65";
string s3 = null;
string s4 = "123456789123456789123456789123456789123456789";

int result;
bool success;

result = Int32.Parse(s1); //-- 1234
result = Int32.Parse(s2); //-- FormatException
result = Int32.Parse(s3); //-- ArgumentNullException
result = Int32.Parse(s4); //-- OverflowException

Convert.ToInt32(string)

Convert.ToInt32(string s) method converts the specified string representation of 32-bit signed integerequivalent. This calls in turn Int32.Parse () method. When is a null reference, it will return rather than throw ArgumentNullException. If is other than integer value, it will throw FormatException. When srepresents a number less than MinValue or greater than MaxValue, it will throw OverflowException. For example:
result = Convert.ToInt32(s1); //-- 1234 
result = Convert.ToInt32(s2); //-- FormatException
result = Convert.ToInt32(s3); //-- 0
result = Convert.ToInt32(s4); //-- OverflowException

Int32.TryParse(string, out int)

Int32.Parse(string, out int) method converts the specified string representation of 32-bit signed integer equivalent to out variable, and returns true if it is parsed successfully, false otherwise. This method is available in C# 2.0. When is a null reference, it will return rather than throw ArgumentNullException. If is other than an integer value, the out variable will have rather than FormatException. When represents a number less than MinValue or greater than MaxValue, the out variable will have rather than OverflowException. For example:
 success = Int32.TryParse(s1, out result); //-- success => true; result => 1234 
success = Int32.TryParse(s2, out result); //-- success => false; result => 0
success = Int32.TryParse(s3, out result); //-- success => false; result => 0
success = Int32.TryParse(s4, out result); //-- success => false; result => 0
Convert.ToInt32 is better than Int32.Parse since it returns rather than an exception. But again, according to the requirement, this can be used. TryParse will be the best since it always handles exceptions by itself.

http://www.codeproject.com/Articles/32885/Difference-Between-Int32-Parse-Convert-ToInt32-and

DateTime MinValue not equal SqlDateTime MinValue


I think the difference between SQL’s and .NET’s Date data types stems from the fact that SQL Server’sdatetime data type, it’s minimum and maximum values, and it’s precision are much older than .NET’s DateTime datatype.
With the advent of .NET, the team decided that the Datetime data type should have a more naturalminimum value, and 01/01/0001 seems a fairly logical choice, and certainly from a programming language, rather than database perspective, this value is more natural.
Incidentally, with SQL Server 2008, there are a number of new Date-based datatypes (DateTime,DateTime2DateTimeOffset) that actually do offer an increased range and precision, and closely map to the DateTime datatype in .NET. For example, the DateTime2 data type has a date range from 0001-01-01 through 9999-12-31.
The standard “datetime” data type of SQL Server always has had a minimum value of 01/01/1753 (and indeed still does have!). I must admit, I too was curious as to the significance of this value, so did some digging.. What I found was as follows:
During the period between 1 A.D. and today, the Western world has actually used two main calendars: the Julian calendar of Julius Caesar and the Gregorian calendar of Pope Gregory XIII. The two calendars differ with respect to only one rule: the rule for deciding what a leap year is. In the Julian calendar, all years divisible by four are leap years. In the Gregorian calendar, all years divisible by four are leap years, except that years divisible by 100 (but not divisible by 400) are not leap years. Thus, the years 1700, 1800, and 1900 are leap years in the Julian calendar but not in the Gregorian calendar, while the years 1600 and 2000 are leap years in both calendars.
When Pope Gregory XIII introduced his calendar in 1582, he also directed that the days between October 4, 1582, and October 15, 1582, should be skipped—that is, he said that the day after October 4 should be October 15. Many countries delayed changing over, though. England and her colonies didn’t switch from Julian to Gregorian reckoning until 1752, so for them, the skipped dates were between September 4 and September 14, 1752. Other countries switched at other times, but 1582 and 1752 are the relevant dates for the DBMSs that we’re discussing.
Thus, two problems arise with date arithmetic when one goes back many years. The first is, should leap years before the switch be calculated according to the Julian or the Gregorian rules? The second problem is, when and how should the skipped days be handled?
This is how the Big Eight DBMSs handle these questions:
  • Pretend there was no switch. This is what the SQL Standard seems to require, although the standard document is unclear: It just says that dates are “constrained by the natural rules for dates using the Gregorian calendar”—whatever “natural rules” are. This is the option that DB2 chose. When there is a pretence that a single calendar’s rules have always applied even to times when nobody heard of the calendar, the technical term is that a “proleptic” calendar is in force. So, for example, we could say that DB2 follows a proleptic Gregorian calendar.
  • Avoid the problem entirely. Microsoft and Sybase set their minimum date values at January 1, 1753, safely past the time that America switched calendars. This is defendable, but from time to time complaints surface that these two DBMSs lack a useful functionality that the other DBMSs have and that the SQL Standard requires.
  • Pick 1582. This is what Oracle did. An Oracle user would find that the date-arithmetic expression October 15 1582 minus October 4 1582 yields a value of 1 day (because October 5–14 don’t exist) and that the date February 29 1300 is valid (because the Julian leap-year rule applies). Why did Oracle go to extra trouble when the SQL Standard doesn’t seem to require it? The answer is that users might require it. Historians and astronomers use this hybrid system instead of a proleptic Gregorian calendar. (This is also the default option that Sun picked when implementing the GregorianCalendar class for Java—despite the name, GregorianCalendar is a hybrid calendar.)
This above quotation from taken from the following link:

http://stackoverflow.com/questions/805770/sqldatetime-minvalue-datetime-minvalue-why

Skin in visual studio


เราต้องไฟล์ (.skin ) ไว้ใน folder App_Themes เสียก่อน

จากในในไฟล์ .skin เราก็ไปสร้าง skin id เพื่่อที่จะไปเลือก css class อีกที
อันนี้ตัวอย่าง TextBox

ตัวอย่าง Dropdownlist

เวลาเอาไปใช้งานกับ control แทนที่จะใช้ CssClass เราก็มาใช้ SkinID แทนเวลาจะแก้ไข CssClass Name ก็แก้ที่ SkinID ทีเดียวจบ

standard control lose visual studio 2010


สำหรับคนที่พัฒนา Web โดยใช้ VS2010 น่ะครับ
ในกรณีที่จู่ๆ standard control ใน ToolBox หายใป ให้ปิด VS ทั้งหมด 
แล้วลอง พิมพ์คำสั่งต่อไปนี้บน command promt ของ VS By user admin

1. devenv /ResetSettings แล้ว Enter แล้วรอแป๊บนุง

2. devenv /setup แล้ว Enter แล้วรอแป๊บนุง นานนิดหน่อย
3. devenv /resetskippkgs แล้ว Enter แล้วรอแป๊บนุง นานนิดหน่อย
4. devenv /InstallVSTemplates แล้ว Enter แล้วรอแป๊บนุง นานนิดหน่อย

ทีนี้ลองเปิด project มาใหม่ จะพบว่า control standard ทั้งหมดกลับมาแว้วววววว
(หากยังไม่ได้ก็ตัวใครตัวมัน วะฮะฮ่าาา)

ปล.นี่เป็นเหตุการณ์สดๆร้อนๆ เลยเอามาฝากเป็น knowledge กันไป