Wednesday, December 31, 2008

Write Data to SQL Server Tables Using SqlBulkCopy

SqlBulkCopy is a new feature in ADO .NET 2.0 that speed up a copy operation for a large amount of data from your .NET application to SQL Server tables.The SqlBulkCopy class can be used to write data only to SQL Server tables from different types of data source, as long as the data can be loaded to a DataTable instance or read with an IDataReader instance.SqlBulkCopy contains an instance method WriteToServer, which is used to transfer data from a source to the destination table. SqlBulkCopy uses a collection of SqlBulkCopyColumnMapping...

Monday, November 10, 2008

BULK INSERT in MS SQL SERVER

In IT environment, it is always a necessary to import data from data files into database.And recently, I have to deal with import a huge load of data (around 200 MB for 1 CSV input file) from CSV files into MS SQL SERVER. So what I had done to achieve this is by using BULK INSERT.Firstly, prepare a CSV data file with the following content and save as test.csv.July,Singapore,25James,Australia,50May,China,29And then run scripts as following to load all the data from the CSV file into database.--Temp table to store the dataCREATE...

Monday, November 3, 2008

Kill Excel.exe Process in C# .NET

I am trying to get my C# .NET codes to generate reports into excel files. In the Export function, it creates the excel object, adds in the data and then saves the file.All of this works fine. But for some reason, the excel.exe process is still running when I check from task manager.I had tried to quit the excel application. I had tried to use InteropServices.Marshal.ReleaseComObject to get rid of them in .NET. But unfortunately, the excel.exe is still sitting in memory.Finally, I found a way to kill the process in C# .NET. Take...

Thursday, September 18, 2008

ASP .NET Validators don’t work properly with Update Panel

Recently I am working on a project ASP .NET AJAX. Of course it is very common to have some validators in some forms to control the input values, and unfortunately I experienced problem with validators inside the Update Panel. The validators just don’t work properly as I am expected.After google searching and here is the solution that I had found: ASP .NET AJAX Validators.Firstly, downloads the validators.zip file and adds in the DLL into the /bin directory of your project. After that, include the following codes into the pages...

Friday, August 29, 2008

OrElse and AndAlso operators in VB .NET

OrElse and AndAlso are two new logical operators in VB .NET and they have some properties that can enhance your codes in two general categories:1. Avoid executing part of a logical expression.2. Optimize code by not executing any more of a compound expression than required.OrElse and AndAlso are quite similar with the And and Or except that VB .NET supports short-circuiting with the OrElse and AndAlso operators. This means that the expressions will only be executed when necessary. Anyway, the And and Or are still present in...

Tuesday, August 19, 2008

How to get Windows’ Serial Number and Windows Logon User Name in .NET

By using the namespace System.Management, we can easily retrieve the serial number of your Windows OS and windows logon user name.First of all, you need to add a reference of System.Management into your application. To add a reference into your .NET application, you can right-click on References in the solution explorer and select Add Reference.From the Add References dialog box, select System.Management and click OK.The...

Thursday, August 7, 2008

Export Data to Excel (Not HTML Table) in C# .NET

In my previous post - Export Data to Excel (Not HTML Tables), I had shown you how to export to excel in VB .NET. In this post, I will use a C# .NET example instead.Of cause first thing that you need to do is to add Excel.dll (Microsoft excel 11.0 Object Library) into your .NET project references. Here is the codes:private void ExportToExcelFile(System.Data.DataTable dt){   Excel.Application xlsApp = new Excel.ApplicationClass();   Excel.Workbook xlsWorkbook;   Excel.Worksheet xlsWorksheet;   string...

Thursday, July 31, 2008

How to Insert Value into an Identity Column

In database tables, normally identity columns are used as primary keys that automatically generates numeric values for every new inserted row.For this identity (AutoNumber) column, you are not allow to insert your own value. So what if you really want to insert your own value into the column?A very easy way to do this. I will show you how to do this.Firstly, create a table Sample by using the script below into your database.CREATE TABLE Sample (Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,Name VARCHAR(50) NOT NULL )GONormally if...

Friday, July 25, 2008

How to Call Remote Web Services from Client Side JavaScript

Web Services is a collection of universally available functions, which allows different applications from different sources to share among each other without time-consuming custom coding by using the XML, SOAP, WSDL and UDDI open standards across the Internet. You can go to here for more detail explanation on it.Recently, I have been doing some research into how to call a web service from client side JavaScript. ASP .NET AJAX enables you to call a web service from the browser by using client script, namely the page can call...

Friday, July 11, 2008

Displaying Date and time in VB Script with ASP Classic

To display the Date and Time in your ASP page with certain format is very simple indeed. Take a look at the examples as below:<%=time()%> - Get current time - e.g. 9:52:30 AM<%=date()%> - Get current date - e.g. 7/11/2008<%=now()%> - Get current date and time - e.g. 7/11/2008 9:54:03 AM<%=FormatDateTime(now,0)%> - e.g. 7/11/2008 9:55:41 AM<%=FormatDateTime(now,1)%> - e.g. Friday, July 11, 2008<%=FormatDateTime(now,2)%> - e.g. 7/11/2008<%=FormatDateTime(now,3)%> - e.g. 9:57:49 AM<%=FormatDateTime(now,4)%>...

Thursday, June 19, 2008

Couldn’t find ConfigurationManager in System.Configuration?

I am trying to pull an appSettings key from my web.config. In .NET 1.1, we usually use:System.Configuration.ConfigurationSettings.AppSettings["strConnection"].ToString();However, in .NET2.0, using this code will cause the following error:'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'So I just change my code to the following, according to the error above. But the problem is I can’t...

Wednesday, June 18, 2008

Export GridView to PDF

Recently I have been trying to generate some reports to PDF file format from ASP. NET2.0 application. There are a lot of open source PDF libraries out there that you can use to export to PDF such as iTextSharp, Gios PDF .NET Library and PDFSharp. You can go to this link to find out more open source PDF libraries in C#.Later I will show you a working solution on how to export GridView to PDF by using one of the free libraries – iTextSharp.ITextSharp is a port of the iText open source java library written entirely in C# for the...

Monday, June 16, 2008

Error “There was an error opening this document. The file is damaged and could not be repaired”

Few days ago, I was trying to export report in Excel, Word and PDF format.The code that I used works perfect for the Excel and Word file, however, when I tried to do the same on PDF file, I got the following error in Adobe Acrobat Reader version 7 and Internet Explorer 6.0:“There was an error opening this document. The file is damaged and could not be repaired.”I had been searched the solution for hours. I had tried various other things such as Response.BinaryWrite, Response.ClearContent, Response.ClearHeaders, and also some...

Monday, June 9, 2008

Difference between String and string

If you notice in your codes, String, which typed, with an uppercase S is colored in light blue. If it is typed in the lowercase string, it is colored in dark blue. What is the difference between String and string actually?Nothing really, string is a C# language keyword, which is an alias for System.String in .NET framework. So do int = System.Int32, bool = System.Boolean and short = System.Int16. The only subtle difference is that string can be used without a using System; directive, while String needs it, otherwise you will...

Wednesday, June 4, 2008

JavaScript not firing with UpdatePanel

Yesterday when I am dealt with textbox inside an UpdatePanel, I noticed that the JavaScript is not fire when the TextChanged handler is triggered.I had the code lines as below to fire my JavaScript function.string strScript = "javascript:alert('Testing')";ClientScript.RegisterStartupScript(typeof(Page),"clientScript", strScript); After working around with the bug, finally I found the cause. The ClientScript.RegisterStartupScript is just cannot working within an UpdatePanel. To work around the problem there are related static...

Friday, May 30, 2008

How to call Server Side Methods from Client Side Script in ASP .NET

Basically, you cannot call server side methods from client side script directly. Server side methods will execute at server side, client side script at client side, they live at two different worlds.However, to call a server side method from client side, you can pass some information to the server as a request for an action, which can trigger a code behind method.One simple solution to communicate with server from client side is to provide a hidden input field to store some flag or information and to read them on the server...

Thursday, May 22, 2008

@@IDENTITY, SCOPE_IDENTITY and IDENT_CURRENT in Transact-SQL

In Transact-SQL, you can use @@IDENTITY keyword to retrieve the value of the identity column whenever an INSERT, SELECT INTO or bulk copy statement is completed. It contains the last value that is generated by the statement.For example, the query below inserts a new record into a table and returns a result set containing the ID of the inserted record.INSERT INTO myTable (myName) VALUES (‘E-JULY’)SELECT @@IDENTITY AS [@@IDENTITY] --Last inserted identity value will be insertedHowever, beware of a potential for a subtle bug in...

Friday, May 16, 2008

Global.asax in ASP .NET

The Global.asax, which also known as ASP .NET application file, is located in the root directory of an ASP .NET application. This file contains codes that are executed in response to application-level and session-level events rose by ASP .NET or by HTTP modules. There are a few things that needs to be take note about Global.asax, as below: The Global.asax is an optional file. You can remove the file if it is unnecessary....

Friday, April 25, 2008

Web.config for VS2005 is rejected by IIS as Badly Formed

I created a very simple website using VS .NET 2005. When I am trying to bring up my web page, I keep getting the following error message as shown below.Server Error in '/APS_WebSetup' Application. ________________________________________Configuration ErrorDescription: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify...

Friday, April 18, 2008

PlaceHolder control in ASP .NET

PlaceHolder control acts as a container to store other web controls to a web page dynamically. It does not produce any output; its main function is used as a container for other controls. You can use the Controls.Controls collection to add, insert or remove controls from a PlaceHolder control.Here is an example of how to add a web control to PlaceHolder control.HTML<form id="Form1" method="post" runat="server"><asp:placeholder id="placeHolder" runat="server"> </asp:placeholder></form>CODE BEHINDDim newLbl...

Authentication and Authorization in ASP .NET

Authentication and authorization are two tightly-coupled concepts to form the core of security for .NET applications.Authentication is the process of determining and verifying the identity of users based on the users’ credentials. Authorization is the process of determining what level of access an authenticated identity should be granted to a given resource.Whenever a user logs on to a system, he/she will be authenticated first before he/she is authorized.AuthenticationThere are three types of authentication in ASP .NET:1. Form...
 

Get paid for your opinions! Click on the banner above to join Planet Pulse. Its totally free to sign up, and you can earn UNLIMITED. Find out more by visiting PLANET PULSE.
Sign up for PayPal and start accepting credit card payments instantly. http://www.emailcashpro.com
July Code Blog Copyright © 2010 Blogger Template Designed by Bie Blogger Template