Sal Rosales

Sal Rosales

  • NA
  • 9
  • 0

value not within expected range

Aug 20 2008 7:22 PM

Hi,

I've written some code that seems to throw an error after the second or third time this specific page is clicked on. So if I go to the page the first time, the page loads up fine, I'm able to click the submit button and my data is written to the database (sharepoint). But upon clicking on the same link a second, third, etc time, I get a 'Value did not fall within the expected range' error. Then after some time the error will go away. I really can't figure out what is going on. Can you take a look at my code and just see if anything jumps out?

thank you,

#region Information

///~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// Project : Daily Contractor Check-In

///~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#endregion

using System;

using System.Web.UI.WebControls;

using System.Web.UI;

using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebPartPages;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Diagnostics;

using System.Data;

namespace CorpSecurity

{

public class ContractorCheckIn : Microsoft.SharePoint.WebControls.LayoutsPageBase

{

protected PlaceHolder PlaceHolder1;

protected Button btnCheckInContractor;

System.Collections.Generic.KeyValuePair<string, string> contractorInfo;

Dictionary<CheckBox, KeyValuePair<string, string>>

alllist = new Dictionary<CheckBox, KeyValuePair<string, string>>();

#region OnPreInit

/// <summary>

/// Event for Page Intialization

/// </summary>

/// <param name="e"></param>

protected override void OnPreInit(EventArgs e)

{

///Set the MasterPage

this.MasterPageFile = SPContext.Current.Web.MasterUrl;

base.OnPreInit(e);

}

#endregion

#region Populate Contractor ListBox control

/// <summary>

/// Fill the List box with Contractors

/// </summary>

/// <param name="rssloc"></param>

///

#region Create Child Controls

/// <summary>`

/// Creates the Child Controls

/// </summary>

protected override void CreateChildControls()

{

base.CreateChildControls();

this.Page.Title = "Check-In Contractor";

///Create a HtmlTable to display the site content

btnCheckInContractor = new Button();

btnCheckInContractor.CausesValidation = true;

btnCheckInContractor.CssClass = "ms-ButtonHeightWidth";

btnCheckInContractor.Text = "Check-In";

btnCheckInContractor.Click += new EventHandler(btnCheckInContractor_Click);

PlaceHolder1.Controls.Add(btnCheckInContractor);

Table tblMain = new Table();

tblMain.BorderStyle = BorderStyle.Solid;

tblMain.Width = 750;

TableRow tr;

TableRow trn;

TableCell td1;

TableCell td2;

TableCell td3;

 

SPSite spSiteContractor = SPContext.Current.Site;

SPWeb spWebContractor = spSiteContractor.OpenWeb();

///Contractors

SPList spListContractor = spWebContractor.Lists["Contractors"];

SPListItemCollection listItems = spListContractor.Items;

string txtVendor;

string txtName;

string txtExpDate;

//Set table row headers

trn = new TableRow();

td1 = new TableCell();

td2 = new TableCell();

td3 = new TableCell();

trn.Cells.Add(td1);

trn.Cells.Add(td2);

trn.Cells.Add(td3);

td1.Font.Size = 8;

td1.Font.Bold = true;

Label label = new Label();

label.Text="Contractor Name";

td1.Controls.Add(label);

td2.Font.Size = 8;

td2.Font.Bold = true;

Label Twolabel = new Label();

Twolabel.Text = "Vendor";

td2.Controls.Add(Twolabel);

td3.Font.Size = 8;

td3.Font.Bold = true;

Label ThreeLabel = new Label();

ThreeLabel.Text = "Exp. Date";

td3.Controls.Add(ThreeLabel);

 

tblMain.Rows.Add(trn);

for (int i = 1; i < listItems.Count;i++ )

{

SPListItem item = listItems[i];

txtVendor = string.Empty;

txtName = string.Empty;

txtExpDate = string.Empty;

if (item["Company_x0020_Name"] != null)

{

txtVendor = item["Company_x0020_Name"].ToString();

}

if (item["Name_x0020__x0020__x0020__x0020_"] != null)

{

txtName = item["Name_x0020__x0020__x0020__x0020_"].ToString();

}

if (item["Expiration_x0020_Date"] != null)

{

txtExpDate = item["Expiration_x0020_Date"].ToString();

}

CheckBox chk = new CheckBox();

contractorInfo = new KeyValuePair<string, string>(txtVendor,txtName);

alllist.Add(chk, contractorInfo);

chk.Text = txtName;

tr = new TableRow();

td1 = new TableCell();

td2 = new TableCell();

td3 = new TableCell();

tr.Cells.Add(td1);

tr.Cells.Add(td2);

tr.Cells.Add(td3);

td1.Controls.Add(chk);

td1.Font.Size = 8;

td2.Text = txtVendor;

td2.VerticalAlign = VerticalAlign.Bottom;

td2.Font.Size = 8;

td3.Text = txtExpDate;

td3.VerticalAlign = VerticalAlign.Bottom;

td3.Font.Size = 8;

tblMain.Rows.Add(tr);

}

 

PlaceHolder1.Controls.Add(tblMain);

tblMain.Dispose();

 

btnCheckInContractor = new Button();

btnCheckInContractor.CausesValidation = true;

btnCheckInContractor.CssClass = "ms-ButtonHeightWidth";

btnCheckInContractor.Text = "Check-In";

btnCheckInContractor.Click += new EventHandler(btnCheckInContractor_Click);

PlaceHolder1.Controls.Add(btnCheckInContractor);

 

}

#region Check-In Contractors

/// <summary>

/// Handles addint items into Contractor Check-in list

 

private void btnCheckInContractor_Click(object sender, EventArgs e)

{

SPSecurity.RunWithElevatedPrivileges(delegate()

{

try

{

//SPSite spSiteContractor = SPContext.Current.Site;

try

{

//SPWeb spWebContractor = spSiteContractor.OpenWeb();

SPWeb spWebContractor = SPContext.Current.Web;

SPList spListCheckIn = spWebContractor.Lists["Contractor Check-In"];

///Get the selected Contractors

Table tbl = (Table)PlaceHolder1.Controls[1];

foreach (TableRow ctrl in tbl.Rows)

{

if (ctrl.Controls.Count != 0)

{

if (ctrl.Controls[0].Controls[0] is CheckBox)

{

CheckBox theItem = (CheckBox)ctrl.Controls[0].Controls[0];

string strVendor = string.Empty;

string strName = string.Empty;

//spSiteContractor.AllowUnsafeUpdates = true;

spWebContractor.AllowUnsafeUpdates = true;

if (theItem.Checked)

{

SPListItem spListCheckInNewItem = spListCheckIn.Items.Add();

strVendor = alllist[theItem].Key;

strName = alllist[theItem].Value;

spListCheckInNewItem["Vendor"] = strVendor;

spListCheckInNewItem["Title"] = strName;

spListCheckInNewItem.Update();

// PlaceHolder1.Controls.Add(new LiteralControl(strName));

strVendor = "";

strName = "";

}

//spSiteContractor.AllowUnsafeUpdates = false;

spWebContractor.AllowUnsafeUpdates = false;

}

}

}

}

finally

{}

}

finally

{ }

//place redirect code here

Response.Redirect(SPContext.Current.Web.Url+"/Pages/Thank%20You.aspx");

});

}

#endregion

 

}

}

#endregion

#endregion


Answers (3)