Solved

SharePoint WebPart Help

Posted on 2011-03-10
3
605 Views
Last Modified: 2012-05-11
I have a webpart that I created and then I did a build and deployed using wspbuilder.

When I try to add a webpart to a sharepoint page, I get the following error
"   
The "HotLine_WebPart" Web Part appears to be causing a problem. Value does not fall within the expected range.
"

Could you please look at the code I have to see what I did wrong?  Thanks for any help you can provide.  I'm supposed to show this tomorrow at work.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace HotlineWebPart_WP
{
    [Guid("9c641d46-007f-4c25-9d0d-f64e34d7960d")]
    public class HotLine_WebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        private string _myProperty = null;
        //SPSite mySite = new SPSite(@"http://landis");
        SPSite mySite = SPContext.Current.Site;
        SPWeb myWeb;
        SPList myList;

        SPGridView SPGridView1 = new SPGridView();
        SPDataSource spDataSource = new SPDataSource();

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("My Property Group")]
        [WebDisplayName("MyProperty")]
        [WebDescription("Meaningless Property")]
        public string MyProperty
        {
            get
            {
                if (_myProperty == null)
                {
                    _myProperty = "Hello SharePoint";
                }
                return _myProperty;
            }
            set { _myProperty = value; }
        }


        public HotLine_WebPart()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {

                    base.CreateChildControls();

                    myWeb = SPContext.Current.Web;
                    //myWeb = mySite.OpenWeb();
                    myList = myWeb.Lists["HOTLINE"];
                    SPListItemCollection results = myList.Items;

                    //Create the datatable object
                    DataTable table;
                    table = new DataTable();
                    table.Columns.Add("Type", typeof(string));
                    table.Columns.Add("Name", typeof(string));
                    table.Columns.Add("Modified", typeof(string));
                    table.Columns.Add("Modified By", typeof(string));
                    table.Columns.Add("STATUS", typeof(string));
                    table.Columns.Add("PRIORITY CODE", typeof(string));
                    table.Columns.Add("PRIORITY", typeof(string));
                    table.Columns.Add("CASE NUMBER", typeof(string));
                    table.Columns.Add("COMPLETE", typeof(string));

                    //Create the rows for each splistitem
                    DataRow row;
                    foreach (SPListItem result in results)
                    {
                        row = table.Rows.Add();
                        row["Type"] = result["Type"].ToString();
                        row["Name"] = result["Name"].ToString();
                        row["Modified"] = result["Modified"].ToString();
                        row["Modified By"] = result["Modified By"].ToString();
                        row["STATUS"] = result["STATUS"].ToString();
                        row["PRIORITY CODE"] = result["PRIORITY CODE"].ToString();
                        row["PRIORITY"] = result["PRIORITY"].ToString();
                        row["CASE NUMBER"] = result["CASE NUMBER"].ToString();
                        row["COMPLETE"] = result["COMPLETE"].ToString();
                    }
                    
                    //SPGridView1 = new SPGridView();
                    SPGridView1.Enabled = true;
                    SPGridView1.AutoGenerateColumns = false;
                    SPGridView1.ID = "hotView";

                    //Create the bound fields
                    SPBoundField boundField;
                    boundField = new SPBoundField();
                    boundField.HeaderText = "Type";
                    boundField.DataField = "Type";
                    boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    boundField.ItemStyle.Wrap = false;
                    SPGridView1.Columns.Add(boundField);

                    boundField = new SPBoundField();
                    boundField.HeaderText = "Name";
                    boundField.DataField = "Name";
                    SPGridView1.Columns.Add(boundField);

                    boundField = new SPBoundField();
                    boundField.HeaderText = "Modified";
                    boundField.DataField = "Modified";
                    SPGridView1.Columns.Add(boundField);

                    boundField = new SPBoundField();
                    boundField.HeaderText = "Modified By";
                    boundField.DataField = "Modified By";
                    SPGridView1.Columns.Add(boundField);

                    boundField = new SPBoundField();
                    boundField.HeaderText = "STATUS";
                    boundField.DataField = "STATUS";
                    SPGridView1.Columns.Add(boundField);

                    boundField = new SPBoundField();
                    boundField.HeaderText = "PRIORITY CODE";
                    boundField.DataField = "PRIORITY CODE";
                    SPGridView1.Columns.Add(boundField);

                    boundField = new SPBoundField();
                    boundField.HeaderText = "PRIORITY";
                    boundField.DataField = "PRIORITY";
                    SPGridView1.Columns.Add(boundField);

                    boundField = new SPBoundField();
                    boundField.HeaderText = "CASE NUMBER";
                    boundField.DataField = "CASE NUMBER";
                    SPGridView1.Columns.Add(boundField);

                    boundField = new SPBoundField();
                    boundField.HeaderText = "COMPLETE";
                    boundField.DataField = "COMPLETE";
                    SPGridView1.Columns.Add(boundField);

                    this.Controls.Add(new LiteralControl(this.MyProperty));
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            SPSite mySite = SPContext.Current.Site;
            SPWeb myWeb = SPContext.Current.Web;
            SPList myList = myWeb.Lists["HOTLINE"];

            spDataSource.List = myList;
            SPGridView1.DataSource = spDataSource;
            SPGridView1.DataBind();

            SPGridView1.RenderControl(writer);


        }
        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    }
}

Open in new window

0
Comment
Question by:Isaac
3 Comments
 
LVL 14

Assisted Solution

by:KoenVosters
KoenVosters earned 150 total points
ID: 35107191
Does your list hotline exist?
Are you sure taht all the properties in the listitem exist and have the correct name? I can guess that property code is probably going to be property_x0020_code
0
 
LVL 6

Accepted Solution

by:
vikas413 earned 350 total points
ID: 35107256
It looks like your list "HOTLINE" is not available in the site where you are adding the web part. or one of the column is not available in the list or miss spelled.

generally error is returned by Array or collection and when the specified item/index is not available.

hope this will help you to rectify the error.

Regards,
Vikas Patel.
0
 
LVL 5

Author Comment

by:Isaac
ID: 35108982
Hey guys,

I've check and my document library list does exist and all my column names are spelled right.

>>I can guess that property code is probably going to be property_x0020_code <<
What do you mean?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question