Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SharePoint WebPart Help

Posted on 2011-03-10
3
Medium Priority
?
632 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 600 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 1400 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

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 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