Solved

SharePoint WebPart Help

Posted on 2011-03-10
3
581 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

SharePoint Designer 2010 has tools and commands to do everything that can be done with web parts in the browser, and then some – except uploading a web part straight into a page that is edited in SPD. So, can it be done? Scenario For a recent pr…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now