Friday, July 16, 2010

Could not access the Search administration database. A generic error occurred while trying to access the database to obtain the schema version info.

While installing SP2010 on Windows7 for development, you might face this error "Could not access the Search administration database"

Exception: System.Runtime.InteropServices.COMException (0xC0041236): Could not access the Search administration database. A generic error occurred while trying to access the database to obtain the schema version info.  



Resolution:


Check your SQL Server Client10.0 configuration settings, by going to Start Menu -> SQL Server 2008 R2 -> Configuration Tools -> SQL Server Configuration Manager.


And check whether Share Memory option is enabled under "Client Protocols". It should be enabled.






Run again the SharePoint 2010 Products Configuration Wizard




Happy Programming!!!!


Monday, July 05, 2010

.Net - const Vs readonly

In programming,  some time we used "const", some time "readonly" constants.  Why? Always we should be very cautious when using "const". why?


Here we are, while programming we should always trade off between performance vs flexibility. "const" is faster than "readonly". But "readonly" has more flexibility than "const". There are two types of constants
1) Compile time constants
2) Run time constants


Compile time constants
Compile time constants are replaced with exact value when it compiled. what is it mean? Well. When we declare a variable 


public const int iUserCount = 500;
and when we do some operation like
for (int iCount = 0; iCount < iUserCount; iCount++)


Then x will actually get replaced in the compile time and if you see in the IL using assembler it will be 
for (int iCount = 0; iCount < 500 ; iCount++)
Run Time constants
When we use "readonly", it will not put the actual value instead of reference. Thats why it is slower than Compile time constants. But there is an advantage.


1) With Const we can only declare primitive types and string. But with readonly we can declare all the types.
2) Runtime will be resolved at the runtime


Why we need very cautious about "const".


We should be very cautious about "const" when maintenance come into picture. The reason is, assume you have an assembly "A" where you have declared the
public const int iUserCount = 500; 


And in the Assembly "B" you are using the iUserCount in 
for (int iCount = 0; iCount < iUserCount; iCount++). 
When compiling both, Assembly "B" will have the statement as 
for (int iCount = 0; iCount < 500; iCount++)


Later when you modify the iCount value as 1500 in assembly "A", 
then you have to recompile the assembly "B" also. 
until unless it will have the same 
for (int iCount = 0; iCount < 500 ; iCount++) 
and produce wrong result. So maintenance become nightmare. 
So prefer readonly then const.


Happy programming!!!









Friday, July 02, 2010

Map Network folder (UNC path) from Windows service

This post explains how to map the network folder path(UNC Path) from windows services. The UNC path may be in same machine or same network domain or different domain.


Introduction


This article explains how to map the network folder path(UNC Path) from windows services. The UNC path may be in same machine or same network domain or different domain.

Background

As per my knowledge there is no Managed API to attach and detach UNC network path which is in same or another domain to the local system. Also there is a limitation to access the attached drive from Windows service. This is why i have written this code snippets. Actually i have planned to write a functions for most of the MPR.dll functions. But due to time constraint i have implemented only two API's. I will update this article with new functions, when i get time. Also i am expecting your valuable comments regarding the way i implemented the code and the way i used design patterns. Forgive me, if i said anything wronly :)


Using the code

Before get into the code, i will give some information about windows service.

A Windows service is an application that starts when Windows is booted and runs in the background as long as Windows is running. Windows services by default are run as a virtual user: "LocalSystem" that has administrative rights on the system. The working directory will be the Windows system directory (typically C:\WINNT) and the default temp directory will typically be C:\WINNT\TEMP. Since this is not a real user, this presents some challenges if user-specific data needs to be stored by the application, as there is no home directory for this user. LocalSystem also has no access to network file shares and similar resources; if a service needs to access files on the network, it generally needs to be configured to run as a domain user with access to those files.

So if your windows service needs to access files from shared drive which is in another domain, then you need map the drive using the built-in user credentials through program. This code snippets will be used to map drive. The NetworkHelperStruct.cs partial class contains all the unmanaged flags and extern declaration of unmanaged api which is in mpr.dll and NetworkHelper.cs file contains the implemented code for those api. The WNetAddConnection function will map the drive and WNetCancelConnection will disconnect the drive. The properties.xml is used to configure the networkpath, localdrive, username and password. The below section is excerpt from config xml. you can add more section based on your functionalities.

Screenshot - Helperapi_config.gif

Sample code to use networkhelper:

NetworkHelper networkhelper;
        networkhelper = NetworkHelper.GetInstance ();
        bool result;
        result = networkhelper.MapDrive ("M:", true);
        result = networkhelper.MapDrive ("N:", true);
        result = networkhelper.MapDrive ("X:", true);
        result = networkhelper.WNetCancelConnection("X:",false);
        //result = networkhelper.WNetAddConnection ("K:", remote, "meenakshisundaram", "", false);
        //result = networkhelper.WNetCancelConnection("K:",false);


You can get the source code from my article where i have posted in codeproject

 http://www.codeproject.com/KB/system/Map_Network_folder_UNC_.aspx

Happy coding.

Deploying sharepoint solution - IIS Restart issue

IIS Restart Issue "Access denied"


Quite often the developer will face a below issue while deploying the (WSP) solution in VS2010 running on Windows 2008. Especially if you have not logged in as administrator.


'Recycle IIS Application Pool': 0x80070005Access denied"


The root cause of the issue is

  1. You don't have IIS restart privilege on window2008 
  2. Even if you have it, you have not added your login account in to SharePoint sitecollection administrator.
To solve the issue
To see whether you have privilege or not, you can open a command prompt and type "IISReset". 
If you able to restart, then it will restart successfully otherwise it will deny it.  The reason is, Though you are in administrators group, windows 2008 will not run on the administrator privilege. Every time when you do an operation like IIS restart or Installation, it will prompt for UAC (User Account Control). You should accept the dialog box and elevate the privilege to run the operation. 

Or you can right click the application whatever you want to run and click the menu option "Run as Administrator" which will solve the issue.
If you want to avoid UAC and directly run through you admin account then, go to Control panel -> Manage account -> Hide UAC


Second if you have all the privilege and still you are getting error in VS2010 as "'Recycle IIS Application Pool': 0x80070005Access denied", then you have to add your account in to site collection admin.
1) You can do this by going to sharepoint administration page.
2) Navigate to the site collection administration option
3) Add your name into site collection administrator as a second owner. SharePoint will allow a second owner for site colleciton

After adding it , now you can deploy the solution through VS 2010.


Happy coding.