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;
}
protected override
void Render(HtmlTextWriter
writer)
{
try
{
SampleWebUserControl.RenderControl(writer);
}
catch (Exception
e)
{
writer.Write(e.Message + " : "
+ err);
}
}
protected override
void CreateChildControls()
{
base.CreateChildControls();
try
{
this.Controls.Clear();
SampleWebUserControl = this.Page.LoadControl("~/_controltemplates/WebPart1/SampleWebUserControl.ascx");
}
catch (Exception
e)
{
err = e.Message;
}
}
}
13. Add connection string for your sql server in sharepoint web.config mine at (C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config)
<connectionStrings>
<add
name="AdventureWorksConnectionString"
connectionString="Data
Source=.;Initial Catalog=AdventureWorks;Persist Security Info=True;User ID=sa;Password=wintellect"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
14. Set the Start browser url to a Sharepoint website
15. Deploy the webpart project
16. Open the sharepoint site and click the Site Actions - > Site Settings
17. Select Galleries -> Web Parts
18. You now see a new entry in Web Part gallery (WebPart1.webpart)
19. Click on the newly added webpart to see the preview
20. Now lets edit a page and add your newly added web part to it. Click on the
Site Actions-> Edit Page
21. Click on the Add a Web Part and select Webpart1.webpart from the list
22. And "Exit edit mode" and you get a final screen that will show the Customer
records from Adventure works database in your sharepoint website using webpart
that host a Web User Control.