views:

405

answers:

4

I have created a .net web part and deployed it on the sharepoint site. When I preview it, the sharepoint throws up an error saying "An error occured when previewing the Web Part"

The code in the web part is as follows

        Dim myweb As Microsoft.SharePoint.SPWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context)
        'Dim mylist As Microsoft.SharePoint.SPList = myweb.Lists(System.Configuration.ConfigurationSettings.AppSettings("BedList").ToString())
        Dim mylist As Microsoft.SharePoint.SPList = myweb.Lists("{80F6F320-E1F5-42EB-A2FB-895EAE00F589}")
        Dim items As Microsoft.SharePoint.SPListItemCollection = mylist.Items

        For Each item As Microsoft.SharePoint.SPListItem In items
            returnString = returnString + "<br/>" + item("Status").ToString() + " For " + item("Title").ToString + "<br/>"
        Next

Does anyone know why this would not work. I can not get hold of the execption thrown. any help would be greatful

A: 

You could try checking whether myList is nothing before doing myList.Items

Paul Rowland
I have added the check it still throws the same error
Antony Delaney
+2  A: 

First you should make your exceptions visible by changing the following in the web.config

CallStack = true

    <SafeMode MaxControls="200" CallStack="true" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">

CustomErrors = Off

<customErrors mode="Off" />

Then you will see the real exception

You can also try obtain the current web using the following code:

Dim myweb As Microsoft.SharePoint.SPWeb = SPContext.Current.Web

Other problem could be related with wrong listId ou ListName when you try to obtain the list.

Hope it helps

Paulo Oliveira
When I do that An error occured when previewing the Web Part. at Microsoft.SharePoint.WebPartPages.WebPartPreview.Render(HtmlTextWriter writer)
Antony Delaney
I can see the stack but no exception thrown
Antony Delaney
A: 

Also look for <compilation..> tag and set it to

<compilation debug="true"..>

Anders Rask
A: 

Did you add the "BedList" key to the correct web.config file?

Tudor Olariu