Ferdous's Blog

Technical Speaking on ASP.Net, C#, Sharepoint 2007, Silverlight, SQL Server, Architecture, Development Methodology, ... etc

Recent Posts

Tags

News

  • MJ Ferdous!! is a Software Engineer professional from 6 years ago. Now he is working in OT Group S.P.A., Italy as Sr. Software Engineer. He is responsible for research and developent (R & D) on SharePoint 2007 platform and also provides some consultancy for their clients for sharepoint development. He is also Technical Author of DevMedia Group, Brazil for their Large Portal Mrbool.com as well as owner & moderator of several groups of sharepoint and asp.net. More than 22 video lessons have been already published on Sharepoint 2007 and Silverlight 2. He always loves to work with hard & complex issues and like to share it in his Blog & Groups. Basically he works in Microsoft platform in Dot.net technology. His personal site: http://mjferdous.com/

Community

Email Notifications

Bloggers

Quick Sharepoint References

My Links

Archives

February 2009 - Posts

Data Import from excel file and build an automated insert Script to export into excel file or to export into Sql Server table
Sometimes it is necessary to import data from Excel file and insert into SQL Server table or building an automated script for inserting data into SQL Server table or building an automated script for inserting data and save into another excel file to insert later. Today I will discuss all the possibilities to solve this kind of problems.  You need to follow one or more than one steps of the following depending on your requirement.
Step1: How to Import Data from Excel to SQL Server 2005
SQL Server OpenRowSet command makes the data transformation easily. You can use the following simple command to get the data from Import.xls file and stored into new table tmpTable1. You can perform an additional check that the table is already existing or not. You have to specify the parameter for driver, file path and query into OpenRowSet().  
 
SELECT * INTO [dbo].[tmpTable1] FROM OPENROWSET ('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=c:\Import.xls;','SELECT * FROM [Sheet1$]')
 
Note: The following error could be raised if you do not configure the Surface Area Configuration below.
Error: SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.
 
Enable OPENROWSET and OPENDATASOURCE SUPPORT  from the Surface Area Configuration to avoid error
 
Step2: How to build a data insert query script programmatically by stored procedure
The following stored procedure will return you the Insert Commands in the result Set. You can copy it from the results Grid and run from the Query Editor on SQL Server 2005 Management Studio
i.e.  EXEC SP_DataInsertScript 'InsertTblName'
===================================================================================
Create Procedure SP_DataInsertScript
@InsertTblName varchar(128)
as
/*
exec SP_DataInsertScript 'InsertTblName'
*/
DECLARE @TblName varchar(128)
SELECT @TblName = 'tmpTable1'
 
   
      create table #temp (id int identity (1,1), ColType int, ColName varchar(128))
 
      insert      #temp (ColType, ColName)
      select case when DATA_TYPE like '%char%' then 1 else 0 end ,
            COLUMN_NAME
      from information_schema.columns
      where       TABLE_NAME = @TblName
      order by ORDINAL_POSITION
     
      if not exists (select * from #temp)
      begin
            raiserror('No columns found for table %s', 16,-1, @TblName)
            return
      end
 
declare     @id int ,
      @maxid int ,
      @cmd1 varchar(7000) ,
      @cmd2 varchar(7000) ,
 
      select      @id = 0 ,
            @maxid = max(id)
      from #temp
 
      select      @cmd1 = 'select '' insert ' + @InsertTblName + ' ( '
      select      @cmd2 = ' + '' select '' + '
      while @id < @maxid
      begin
            select @id = min(id) from #temp where id > @id
 
            select      @cmd1 = @cmd1 + ColName + ','
            from #temp
            where id = @id
 
            select @cmd2 =    @cmd2
                        + ' case when ' + ColName + ' is null '
                        +     ' then '''' '
                        +     ' else '
                        +      case when ColType = 1 then ''''''''' + ' + ColName + ' + ''''''''' else 'convert(varchar(20),' + ColName + ')' end
                        + ' end + '','' + '
            from #temp
            where id = @id         
      end
 
 
      select @cmd1 = left(@cmd1,len(@cmd1)-1) + ' ) '' '
      select @cmd2 = left(@cmd2,len(@cmd2)-8) + ' from ' + @tblName
 
     exec (@cmd1 + @cmd2)
      drop table #temp 
===========================================================================
Step3: How to Dump Insert Query into another Excel File
If you want to dump resultset into Excel File then you need to store the resultset in another temporary table and export from table into Excel file using bcp command. Let me explain it how to do:
DECLARE @QueryString varchar(1000)
SET XACT_ABORT ON
BEGIN TRAN
CREATE TABLE testObjects1 ( QueryString varchar(1000) NOT NULL )
INSERT testObjects1 EXEC SP_DataInsertScript 'InsertTblName'
COMMIT TRAN
 
The above command will store the result set of  into testObjects1 Table. Now you have execute bcp command to export into excel file.

Run the following command from the Query Window

 

EXEC Master..xp_cmdshell 'bcp [dbo].testObjects1 out C:\Result.xls -U {UserName} -P {Pass} -S {ServerName} -T –c{or –n(binary)}'

Note: You have to Enable xp_cmdshell from the Surface Area Configuration to run the above command

OR

You can also run the following command from the command Promt
bcp [dbo].testObjects1 out C:\Result.xls -U {UserName} -P {Pass} -S {ServerName} -T –c{or –n(binary)}
The Result Excel file should be generated with Insert command here C:\Result.xls 
References:
Some other old posts on SharePoint 2007 from my existing blog

SharePoint 2007 simple Deployment script

First Time Deployment in the Production Environment using Script | Move sites between Production and Development Environment | Content Deployment using STSADM

Sharepoint: Best way of hiding the “Sign in” link in the client web browser

It is a common problem in the sharepoint 2007 developemt to hide the “Sign in” link in the client web browser. I didnt find any perfect solution for that. Let's think one scenario. For example, You are developing a WCM website in MOSS and have two entry points to the website - one web app for internal use to edit the site (i.e. you have to authenticate), and one for the website (i.e. for anonymous access).

You have created a master page which obviously has to contain controls that allow users to log in and author content. However since the same master page is used for both web apps, you need to hide those controls when the site's being accessed anonymously. You don't want there to be a 'Sign in' link when viewed anonymously.
 

Sharepoint: Different custom workflow for same page creation and modification

Before doing this posting i need to say something about my recent sharepoint post. because One question could be arise that "why all of my recent post is related to sharepoint 2007?" 

The answer is: i am working with sharepoint 2007 for last few months specially for one of our client. I am responsible for all Reserch and Development work and architectural setup. You will also find some important post in the next days.

Sharepoint: How to do Custom List Edit Form

How to customize the EditForm.aspx when you Edit a list item in a SharePoint 2007 list (EditForm.aspx).

Sharepoint Service Error: A deployment or retraction is already under way for the solution "xxx.wsp" , and only one deployment or retraction at a time is supported

Sometime this error has been occured from Windows SharePoint Services Administration command when the user do retractsolution or deploysolution like the follwing

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN>stsadm -o retractsolution -name "%PackageName%" -local -url %TargetWebUrl%

or

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN>stsadm -o deploysolution -name "%PackageName%" -local -allowGacDeployment -url %TargetWebUrl%

A deployment or retraction is already under way for the solution "xxx.wsp", and only one deployment or retraction at a time is supported

it occurs if anomalies deployment is running for the same package in the backend due to some previous deployment has been crashed before for some reason.

To avoid this problem do the followings: 

Look for the deploy jobId to break the running by the following command:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN>stsadm-o enumdeployments
• Cancel the deploy typing the following command:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN>stsadm-o canceldeployment-id "GUID jobId"
• Check that deploy has been canceled by typing the following command:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN>stsadm-o enumdeployments

Hope it should work.

Video on Sharepoint 2007 Development

Some of my sharepoint video lessons has been published in Mr.bool E-Learning http://www.mrbool.com/portal/ Portal. I am a technical author of this site. You will see more technical lesson in the next days. It is totaly free to download. Please put your important feedback.

Author of DevMedia

http://www.devmedia.com.br/space.asp?id=206364

SharePoint 2007 & VS.Net 2005 Extension for WSS 3.0–Event Receiver–Part 01
In this lesson you will learn how to use Visual Studio 2005 to create a list Event receiver class to handle both synchronous and as asynchronous events.
http://www.mrbool.com/articles/viewcomp.asp?comp=10956

SharePoint 2007 & VS.Net 2005 Extension for WSS 3.0–Event Receiver–Part 02
In this lesson you will learn how to use Visual Studio 2005 to create a list Event receiver class to handle both synchronous and as asynchronous events.
http://www.mrbool.com/articles/viewcomp.asp?comp=11004

SharePoint 2007 & VS.Net 2005 Extension for WSS 3.0–List–Lesson 01
In this lesson you will learn how to customize a list to add columns, more content types, create views as well as saving as template.
http://www.mrbool.com/articles/viewcomp.asp?comp=11091

SharePoint 2007 & VS.Net 2005 Extension for WSS 3.0–List–Lesson 02
In this lesson you will learn how to customize a list Forms to create a new layout for NewForm.aspx, EditForm.aspx using Sharepoint Designer.
http://www.mrbool.com/articles/viewcomp.asp?comp=11178

Sharepoint Custom Themes && Custom Cascading Style Sheet(CSS) Guideline

How to Customize theme code for MYTHEME theme. It is only necessary if you want to create another one like MYTHEME theme. Here are the instructions of creating Custom Theme with MYTHEME standard.

 

How to: Customize Themes

You can add new themes or customize existing ones for application to Web sites in Microsoft Windows SharePoint Services 3.0. This programming task shows how to customize an existing theme.

To create and customize from an existing theme

  1. Copy one of the theme folders in Local_Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\THEMES and give the folder a unique name.

Note: In this example, the name is MyTheme. This folder contains cascading style sheets (CSS) files, image files, and other files that define the styles, formatting, and color for the various user interface (UI) elements that are used in the theme.

  1. Find the .inf file in the copied folder, and rename it with the name given to the folder. i.e. in this example rename that .inf file with MyTheme.INF
  2. Open the .inf file and assign the same name to the title in the [info] and [titles] sections of the file.
  3. There is one theme.css file in that MyTheme folder, open that, Customize the styles defined in the following as needed.

Field-Label  Text: It is for all sharepoint text label and field in the form. You have to insert the following code to change in MyTheme standard.

 

Code:

.ms-formbody {

    background: transparent;

    border-top: 1px solid #ffffff;

}

.ms-formlabel  {

    border-top: 1px solid  #ffffff;

    color: #000000;

}

h3.ms-standardheader{

color:#000000;

font-weight:normal;

font-family:vardana;

}

Button: You have to integrate the following code in the theme.css inside corresponding theme(i.g. “MYTHEME”) folder to change all button style.

Code: 

.ms-ButtonHeightWidth

{

width:12.0em;

font:8pt verdana;

height:2.1em;

padding-top:0.1em;

padding-bottom:0.4em;

color:#00231b;

background-color:#cbdeda;

border:#00231b 1px solid;

font-weight:bold;

cursor:hand; 

}

.ms-NarrowButtonHeightWidth

{

width:9em;

height:2em;

font:8pt verdana;

padding:0;

color:#00231b;

background-color:#cbdeda;

border:#00231b 1px solid;

font-weight:bold;

cursor:hand;

}

.ms-ButtonHeightWidth2

{

height:2.1em;

font:8pt verdana;

width:11.72em;

padding-top:0.1em;

padding-bottom:0.4em;

color:#00231b;

background-color:#cbdeda;

border:#00231b 1px solid;

font-weight:bold;

cursor:hand;

}

 

Modify the image files in the copied folder by using the business graphics software of your choice.

  1. Add a file for thumbnail and preview images for your custom theme to the Local_Drive:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\IMAGES directory. In this example, the file is called myPreview.gif.
  2. Add a theme template definition to SPTHEMES.XML, which is the file that determines which themes are available as options on the Site Theme page. This XML file is located in the Local_Drive:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\1033 directory.

The following example specifies a template for the custom theme.

Xml

<Templates>


   <TemplateID>mytheme</TemplateID>


   <DisplayName>My Theme</DisplayName>


   <Description>Description</Description>


   <Thumbnail>images/myPreview.gif</Thumbnail>


   <Preview>images/myPreview.gif</Preview>


</Templates>


  1. Reset Internet Information Services (IIS) by typing iisreset at the command prompt so that your custom theme appears in the list of options on the Site Theme page and can be applied to SharePoint sites.

Robust Programming

Warning   Changes that you make to SPTHEMES.XML might be overwritten when you install updates or service packs for Windows SharePoint Services, or when you upgrade an installation to the next version.

 Ref: http://msdn.microsoft.com/en-us/library/aa979310.aspx

 

My first post on this Blog

I am MJ Ferdous!!, Software Engineer professional in Microsoft Platform from 6 years ago.

Now Responsible for research and developent (R & D) on SharePoint 2007 platform and also provides some consultancy on sharepoint and ASP.Net development.

I am also Technical Author of DevMedia Group, Brazil for their Large Portal www.Mrbool.com  as well as owner & modaretor of some groups of sharepoint and asp.net.

My Existing blog is http://geekswithblogs.net/ferdous/Default.aspx

From now, my latest post will also available here.

Enjoy!! my post.

Cheers 

Ferdous

Posted: 02-16-2009 12:09 PM by MJ Ferdous | with no comments
Filed under: