Tony’s Weblog

ASP.NET, MVC, AJAX, SilverLight, C#, WCF Web Service, SQL 2005 and other interesting stuff.

Archive for the ‘Uncategorized’ Category

SQL Prompt Visual Studio .NET Add-on

Posted by Tony on September 1, 2008

SQL Prompt is Visual Studio .NET add-on. It makes writing queries very fast and easy and It does parse queries as you type and suggests keywords, table names, and join conditions (amongst others).

The demo below shows some of what SQL Prompt can do, though it also has support for Updates, stored procedures, and many other SQL statements. 14-day trail download is available here

Main features of SQL Prompt Visual Studio .NET add-on:

  • Intelligent code completion for fast, accurate script building
  • Cross-database and linked-server query support
  • Integrates with SSMS, SSMS Express, Query Analyzer and Visual Studio 2005
  • Keyword formatting, code snippet integration, column picker and many more features

Posted in Uncategorized | Leave a Comment »

Compression Decompression in .NET

Posted by Tony on September 1, 2008

DeflateStream and GZipStream classes present in System.IO.Compression namespace provide compression and decompression capabilities. GZipStream compresses the serialized content of application, with great results. These classes operate on file streams.

Compression_Decompression_DotNET


The compression through these classes will likely be less than what can be achieved with a file compression utility. But the good thing is that we have this functionality include in .NET Framework 2.0 and above. As Microsoft said this namespace will be further enhanced and modified in the next versions.

Posted in Uncategorized | Leave a Comment »

Implement a simple captcha in C#.NET

Posted by Tony on September 1, 2008

1. Create a page with name “Captcha.aspx”

2. Place the following html code in body part of the page

   1: IMG height="30" alt="" src=BuildCaptcha.aspx width="80">
   2: asp:TextBox runat="Server" ID="txtCaptcha">
   3: <asp:Button runat="Server" ID="btnSubmit"
   4:            OnClick="btnSubmit_Click"
   5:            Text="Submit" />
   6:
   7: 3. On “btnSubmit_Click” place the following code
   8:
   9: if (Page.IsValid && (txtCaptcha.Text.ToString() ==
  10:      Session["RandomStr"].ToString()))
  11: {
  12:   Response.Write("Code verification Successful");
  13: }
  14: else
  15: {
  16:   Response.Write( "Please enter info correctly");
  17: }

3. Include the following code in “BuildCaptcha.aspx

   1: Bitmap objBMP = new Bitmap(60, 20);
   2: Graphics objGraphics = Graphics.FromImage(objBMP);
   3: objGraphics.Clear(Color.Wheat);
   4: objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
   5:
   6:
   7: //' Configure font to use for text
   8: Font objFont = new Font("Arial", 8, FontStyle.Italic);
   9: string randomStr = "";
  10: char[] myArray = new char[5];
  11: int x;
  12:
  13: //That is to create the random # and add it to our string
  14: Random autoRand = new Random();
  15: for (x = 0; x < 5; x++)
  16: {
  17:   myArray[x] = System.Convert.ToChar(autoRand.Next(65,90));
  18:   randomStr += (myArray[x].ToString());
  19: }
  20:
  21: //This is to add the string to session, to be compared later
  22: Session.Add("RandomStr", randomStr);
  23:
  24: //' Write out the text
  25: objGraphics.DrawString(randomStr, objFont, Brushes.Red, 3, 3);
  26:
  27: //' Set the content type and return the image
  28: Response.ContentType = "image/GIF";
  29: objBMP.Save(Response.OutputStream, ImageFormat.Gif);
  30: objFont.Dispose();
  31: objGraphics.Dispose();
  32: objBMP.Dispose();

4. There you go you can test the page with captcha. Happy Programming!

Posted in Uncategorized | Leave a Comment »

What Is the Service Factory?

Posted by Tony on September 1, 2008

Generally speaking, software factories represent a collection of guidance that helps architects and developers build a specific kind of application. Therefore, the Service Factory helps developers and architects build Web service applications. Web service applications are more than just the technologies that send and receive SOAP messages and expose WSDL contracts—they also include the functionality that is needed to fulfill the service’s behavior. Figure 1 illustrates the parts of the application, and their relationship with one another, the Service Factory will help you build.

Figure 1. Structure of a service built with the Service Factory

If you are already building services using WCF, the Service Factory also helps you build and secure those services.

The Service Factory provides the following benefits:

· Increased quality. It provides tested and accepted reusable assets, guidance, and examples that address common scenarios and challenges. It is based on patterns derived from proven industry and Microsoft practices.

· Increased predictability. The patterns & practices team works closely with the development teams responsible for building current and future generations of service and data access technologies. All services built with the Service Factory will be built in a predictable way, arrive at a predictable result, and will enable a predictable migration path to future technologies.

· Increased productivity. It includes automated code and configuration file generation for Visual Studio 2005. With this automation, developers can easily apply guidance in consistent and repeatable ways. Developers can also effectively use the .NET Framework without having to devote significant time to learning the necessary APIs.

· Increased flexibility.

The Service Factory is carefully designed according to proven practices, but it is also open and highly customizable. Architects and development leads can customize the factory to include the conventions, policies, and practices specific to a team or organization.

Posted in Uncategorized | Leave a Comment »

2008 Summer Olympic Games, Beijing

Posted by Tony on August 8, 2008

Posted in Uncategorized | Leave a Comment »

How to Delete Dups from Table

Posted by Tony on August 4, 2008

How to Identify and Delete Duplicate SQL Server Records

/*****************************

*****************
Example of a complex duplicate data delete script.
**********************************************/

/**********************************************
Set up test environment
**********************************************/
SET NOCOUNT ON

–Create test table
IF OBJECT_ID(‘tDupData’) IS NOT NULL
DROP TABLE tDupData
GO

CREATE TABLE tDupData
(
lngCompanyID INTEGER
,strCompanyName VARCHAR(20)
,strAddress VARCHAR(10)
,dtmModified DATETIME
)

–Create test data
INSERT INTO tDupData VALUES (1,’CompanyOne’,’Address1′,’01/15/2003′)
INSERT INTO tDupData VALUES (2,’CompanyTwo’,’Address2′,’01/15/2003′)
INSERT INTO tDupData VALUES (3,’CompanyThree’,’Address3′,’01/15/2003′)
INSERT INTO tDupData VALUES (1,’CompanyOne’,’Address1′,’01/15/2003′)
— Simple Dup Data and complex dup data
INSERT INTO tDupData VALUES (2,’CompanyTwo’,’Address’,’01/16/2003′)
— complex dup data
INSERT INTO tDupData VALUES (3,’CompanyThree’,’Address’,’01/16/2003′)
— complex dup data
GO

/**********************************************
Finish set up
**********************************************/

/**********************************************
Complex duplicate data
**********************************************/

–Clean table out to include only one row per company
–Create temp table to hold duplicate data
CREATE TABLE #tempduplicatedata
(
lngCompanyID INTEGER
,strCompanyName VARCHAR(20)
,strAddress VARCHAR(10)
,dtmModified DATETIME
)

–Clean out simple duplicate data first
–Identify and save dup data into temp table
INSERT INTO #tempduplicatedata
SELECT * FROM tDupData
GROUP BY lngCompanyID,strCompanyName,strAddress, dtmModified
HAVING COUNT(*) > 1

–Confirm number of dup rows
SELECT @@ROWCOUNT AS ‘Number of Duplicate Rows’

–Delete dup from original table
DELETE FROM tDupData
FROM tDupData
INNER JOIN #tempduplicatedata
ON  tDupData.lngCompanyID = #tempduplicatedata.lngCompanyID
AND tDupData.strCompanyName = #tempduplicatedata.strCompanyName
AND tDupData.strAddress = #tempduplicatedata.strAddress
AND tDupData.dtmModified = #tempduplicatedata.dtmModified

–Insert the delete data back
INSERT INTO tDupData
SELECT * FROM #tempduplicatedata

–Check for dup data.
SELECT * FROM tDupData
GROUP BY lngCompanyID,strCompanyName,strAddress,dtmModified
HAVING COUNT(*) > 1

–Clean out temp table
TRUNCATE TABLE #tempduplicatedata

–Identify and save dup data into temp table
INSERT INTO #tempduplicatedata (lngCompanyID,strCompanyName)
SELECT lngCompanyID,strCompanyName FROM tDupData
GROUP BY lngCompanyID,strCompanyName
HAVING COUNT(*) > 1

–Confirm number of dup rows
SELECT @@ROWCOUNT AS ‘Number of Duplicate Rows’

–Update temp table to add strAddress and dtmModified
UPDATE #tempduplicatedata
SET strAddress = tDupData.strAddress
,dtmModified = tDupData.dtmModified
FROM #tempduplicatedata
INNER JOIN tDupData
ON #tempduplicatedata.lngCompanyID = tDupData.lngCompanyID
AND #tempduplicatedata.strCompanyName = tDupData.strCompanyName

–Delete dup from original table
DELETE FROM tDupData
FROM tDupData
INNER JOIN #tempduplicatedata
ON  tDupData.lngCompanyID = #tempduplicatedata.lngCompanyID
AND tDupData.strCompanyName = #tempduplicatedata.strCompanyName
AND tDupData.strAddress = #tempduplicatedata.strAddress
AND tDupData.dtmModified = #tempduplicatedata.dtmModified

–Verify original table only has three rows of data
SELECT * FROM tDupData

–Drop temp table
DROP TABLE #tempduplicatedata

–drop test table
IF OBJECT_ID(‘tDupData’) IS NOT NULL
DROP TABLE tDupData
GO

This is a little more complicated than the simple duplicate data delete script, but easy to figure out once you see it. A word of caution here, you should investigate any child tables before you delete data from a table in order to prevent creating orphan rows. You can ether delete the data from the child tables first or update them to reflect the identity key value of the data row in the main table you are going to keep. The choice will be determined by your situation and any operating standards you may have.

While having to clean up duplicate data is not something you should have to do every day, the processes you learn from playing with these two scripts should give you a starting point the next time you find duplicate information in your database.

Posted in Uncategorized | Leave a Comment »

Convert byte array to an Image

Posted by Tony on August 2, 2008

Dim con As New
SqlConnection(“Server=yileiw2;uid=sqlauth;pwd=sqla uth;database=pubs”)
Dim da As New SqlDataAdapter(“Select * From pub_info”, con)
Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet()

con.Open()
da.Fill(ds, “Pub_info”)
Dim myRow As DataRow
myRow = ds.Tables(“Pub_info”).Rows(0)

Dim MyData() As Byte
MyData = myRow(“logo”)

Response.Buffer = True
Response.ContentType = “Image/JPEG”
Response.BinaryWrite(MyData)

MyCB = Nothing
ds = Nothing
da = Nothing

con.Close()
con = Nothing

Posted in Uncategorized | Tagged: | Leave a Comment »

June Lake

Posted by Tony on July 28, 2008

Posted in Uncategorized | 1 Comment »