SharePoint - Not able to use sql connection in sharepoint webpart

Asked By Shatrunjay Shukla on 20-Mar-12 02:16 PM
I have created web part in .net 4.0 then create manifest.xml then create packaging(setup).Before that i have configure 12 hives on sharepoint server(..../12/config) for sql permission then from command prompt we issue stsadm command it got successfull then again from sharepoint administration server we have deployed the web part.

Now we have open our sharepoint site Action->site setting->web part->click on New add the web part
then created web part page but when we try to add web part on web part page then " failed, Object reference not created" message appear

Please suggest what should i do, provide the complete steps

Note: On urgent basis

Shatrunjay Shukla
[)ia6l0 iii replied to Shatrunjay Shukla on 20-Mar-12 09:40 PM
There are one solution that I have tried , and one other that I have read. You can check if those many be of some help. 

a) There is a setting on the Sharepoint server that does not allow to run web parts deployed in Bin with the required higher priveleges.  You should "move" the web part to Global Assembly Cache (GAC). You can just drag and drop in GAC. You should also not forget to delete the assembly from your current bin folder. The run time will then check GAC instead of local bin first. Please reset IIS after you move the asssembly to GAC. 

b) By default, the sharepoint web.config has the trust level set to minimum. You may choose to override in this case. open the wss_mediumtrust and the wss_minimaltrust.config files. Take a backup of the files and then do the following modifications. Go to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\xx\config\ folder and copy-paste the nodes from wss_mediumtrust.config files. 
a) Security Class
b) IPermission
Look for this error on the web,and you will find the exact node xml to be copied.

Hope this helps.
Jitendra Faye replied to Shatrunjay Shukla on 21-Mar-12 12:01 AM
You can connect with SQL server database using custom webpart.

Refer this link here you will get step by step process to connect with database.
D Company replied to Shatrunjay Shukla on 21-Mar-12 12:31 AM

A  useful help  to avoid this problem is . u just edit the  wss_minimaltrust.config file. set unrestricted ="true"

<SecurityClass Name="SqlClientPermission"Description="System.Data.SqlClient.SqlClientPermission,System.Data,Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

In the  SecurityClasses section <IPermission class="SqlClientPermission" version="1" Unrestricted="true" />to the PermissionSet section with the attribute Name="ASP.Net".
Even u can also check the same with wss_mediumtrust.config for the SqlClientPermissions entries and try to
add them to wss_minimaltrust.config.


Shatrunjay Shukla replied to [)ia6l0 iii on 21-Mar-12 01:15 AM
Is there any changes in web.config of sharepoint site where we want to use web part.
Actually i did all the changes but still some issue so i need step step solution so that i follow the same steps
Shatrunjay Shukla replied to [)ia6l0 iii on 21-Mar-12 01:16 AM
I have to use Bin directory only, in GAC it is working fine bcoz GAC is Full trust so i avoid to deploy in GAC
kalpana aparnathi replied to Shatrunjay Shukla on 21-Mar-12 04:20 AM

Go to the below directory . Hope you have default installation path, else you can change the path accordingly

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG

after that open the wss_mediumtrust.config and wss_minimaltrust.config files. Be sure to take back up of both these files before modifying it.

After doing this Copy the below line from wss_mediumtrust.config for the permission

Description="System.Data.SqlClient.SqlClientPermission, System.Data,
Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"

And paste it in inside node of wss_minimaltrust.config

After pasting ,Copy the below line from wss_mediumtrust.config file

   ipermission class="SqlClientPermission" version="1" Unrestricted="true"

And also Paste it in node of wss_minimaltrust.config

Hope will helps!!!!!!!!!!!!!!!!!!!!

Shatrunjay Shukla replied to kalpana aparnathi on 21-Mar-12 04:44 AM
Thank u very much