This is small article to provide the
information that how you can connect to database from Sharepoint WebPart using
ASP.NET web user control.
1. Create a Sharepoint Webpart project as below

2. In Solution Explorer, double-click the AssemblyInfo file
Add the following line to the top of the file
using
System.Security;
Add the following line to the bottom of the file
[assembly: AllowPartiallyTrustedCallers]
3. Add a new ASP.NET web application to the solution

4. Add new web user control

5. Add a GridView to the usercontrol and configure it to retrieve the records
from Customer table of Adventureworks database

6. Delete the CodeBehind attribute in the SampleWebUserControl.ascx file
7. Right-click the MyWebPart project, point to Add, and then click New
Folder. Name the folder Templates.
8. Right-click the Templates folder, point to Add, and then click New
Folder. Name the folder ControlTemplates.
9. Right-click the ControlTemplates folder, point to Add, and then
click New Folder. Name the folder WebPart1.

10. Right-click the SampleWebPart folder, point to Add, and then click
Existing Item. Browse to the SampleWebUserControl.ascx file, and then click
Add as Link in the drop-down box.
11. In the SampleWebApplication project, open the SampleWebUserControl.ascx
file. Replace the existing value of the Inherits attribute with
SampleWebPart.SampleWebUserControl, SampleWebPart, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=9f4da00116c38ec5. This allows the code
behind to be compiled into the SampleWebApplication.dll.
<%@
Control Language="C#"
AutoEventWireup="true"
Inherits="SampleWebPart.SampleWebUserControl,
SampleWebPart,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5"
%>
12. Update the Webpart1.cs with following
public
class WebPart1
: System.Web.UI.WebControls.WebParts.WebPart
{
Control SampleWebUserControl;
String err;
public WebPart1()
{
this.ExportMode =
WebPartExportMode.All;
}
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.










SUBRAMANI SPosted Jan 13, 2015, 4:32 AM
Actual User control deployed in the "@"~/_CONTROLTEMPLATES/15/ReuseControl/GrdUserCntl.ascx " location. Make sure the Safe control on Visual webpart(right click webpart and go to properties and check safe control attribute set to true) rights have full.I found same error but solved when follow these steps.
muthuramaneditedPosted Feb 21, 2011, 4:33 AMEdited Feb 21, 2011, 4:37 AM
Object reference not set to an instance of an object. : 'SampleWebPart.SampleWebUserControl, SampleWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5' is not allowed here because it does not extend class 'System.Web.UI.UserControl'. Please Revert. Regards, Muthuraman M
Jerry biPosted Aug 12, 2010, 5:59 AM
Hi Nipun Tomar, I have followed his steps, but still will not run Ajax. ConfirmButton I add the Ajax, but it is not possible.
shabaPosted May 24, 2010, 9:25 AM
Hi Nipun ...Great Article- very informative and useful! However, I get the following error when on trying to view the webPart "Object reference not set to an instance of an object: The file '/_controltemplates/WebPart1/SampleWebUserControl.ascx' does not exist. I don't understand what I am missing. Your help will greatly be appreciated. Thanks, Shabana