Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 635
  • Last Modified:

SharePoint WebPart Help

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
Isaac
Asked:
Isaac
2 Solutions
 
KoenVostersCommented:
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
 
vikas413Commented:
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
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now