Solved

SharePoint WebPart Help

Posted on 2011-03-10
3
625 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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