我正在尝试在sharepoint2013 webpart中使用名为ASPTokenInputLib的第三方dll。 当我将它加载到bin目录中时,它工作了一段时间,但是当我尝试将它移动到GAC时,它将无法工作,如果我现在也将它加载到bin中,它就会停止工作。 我得到的错误是对象引用未设置为对象的实例。 sharepoint日志显示
System.NullReferenceException:未将对象引用设置为对象的实例。 at ASPTokenInputLib.ASPTokenInput.OnLoad(EventArgs e)
我的包清单文件包含
<Assemblies>
<Assembly Location="ASPTokenInputLib.dll" DeploymentTarget="GlobalAssemblyCache" />
<Assembly Location="BidSearchWebPart.dll" DeploymentTarget="GlobalAssemblyCache">
<SafeControls>
<SafeControl Assembly="BidSearchWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=59049686a9425568" Namespace="BidSearchWebPart.VisualWebPart1" TypeName="*" />
</SafeControls>
</Assembly>
如果我删除了webpart中的任何ASPTokenInput控件,webpart正确加载,所以我认为在gac中找不到控件是一个问题(我已经使用了进程监视器,它正在寻找GAC中的正确位置找到控件)。出于某种原因虽然它无法加载DLL。
非常感谢任何帮助。谢谢!
我的ascx.cs文件是
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Web.Script.Serialization;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using ASPTokenInputLib;
namespace BidSearchWebPart.VisualWebPart1
{
[ToolboxItemAttribute(false)]
public partial class BidSearchWebPart : WebPart
{
private enum Columns
{
BidRef, BidName, Client, Area, TeamLead
}
private string TeamLeadsData, ExecLeadsData, StagesData, SectorsData, ServicesData, GradesData, CostCentresData, ClientsData, AreasData;
public BidSearchWebPart()
{
TeamLeadsData = RunSP("uspSelectTeamLeads", "TeamLead", "Name", ConnectionString());
ExecLeadsData = RunSP("uspSelectExecTechLeads", "ExecutiveTechnicalLead", "Name", ConnectionString());
StagesData = RunSP("uspSelectStages", "StageID", "StageDescSearch", ConnectionString());
SectorsData = RunSP("spSelectMarketSectors", "SectorID", "MktSector", CCDConnectionString());
ServicesData = RunSP("spSelectAllServices", "ServiceID", "Service", CCDConnectionString());
GradesData = RunSP("uspSelectGrades", "GradeID", "GradeDesc", ConnectionString());
CostCentresData = RunSP("uspSelectCostCentres", "CostCentre", "Name", ConnectionString());
ClientsData = RunSP("spSelectAllClients", "CompanyRef", "CoName", CCDConnectionString());
AreasData = RunSP("uspSelectAreas", "AreaID", "AreaName", ConnectionString());
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitializeControl();
}
protected void Page_Load(object sender, EventArgs e)
{
List<string> ddValues = new List<string>();
if (!Page.IsPostBack)
{
ddValues.Add("Greater than");
ddValues.Add("Less than");
ddCC.DataSource = ddValues;
ddFV.DataSource = ddValues;
ddCC.DataBind();
ddFV.DataBind();
}
btnSearch.Click += btnSearch_Click;
gvSearchResults.RowDataBound += gvSearchResults_RowDataBound;
tiTeamLead.DataSource = ASPTokenInput.DataSourceType.DataSet;
tiTeamLead.DataSet = TeamLeadsData;
tiExecTechLead.DataSource = ASPTokenInput.DataSourceType.DataSet;
tiExecTechLead.DataSet = ExecLeadsData;
tiStage.DataSource = ASPTokenInput.DataSourceType.DataSet;
tiStage.DataSet = StagesData;
tiSector.DataSource = ASPTokenInput.DataSourceType.DataSet;
tiSector.DataSet = SectorsData;
tiService.DataSource = ASPTokenInput.DataSourceType.DataSet;
tiService.DataSet = ServicesData;
tiGrade.DataSource = ASPTokenInput.DataSourceType.DataSet;
tiGrade.DataSet = GradesData;
tiCostCentre.DataSource = ASPTokenInput.DataSourceType.DataSet;
tiCostCentre.DataSet = CostCentresData;
tiClient.DataSource = ASPTokenInput.DataSourceType.DataSet;
tiClient.DataSet = ClientsData;
tiArea.DataSource = ASPTokenInput.DataSourceType.DataSet;
tiArea.DataSet = AreasData;
}
void btnSearch_Click(object sender, EventArgs e)
{
//if (string.IsNullOrEmpty(txtProjectNo.Text) && string.IsNullOrEmpty(txtProjectName.Text) && string.IsNullOrEmpty(txtClient.Text) && string.IsNullOrEmpty(txtKeyword.Text)) // Do nothing if search text boxes are blank
//{
// return;
//} else {
gvSearchResults.PageIndex = 0;
DoSearch();
//}
}
protected void gvSearchResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvSearchResults.PagerTemplate = null;
gvSearchResults.PageIndex = e.NewPageIndex;
DoSearch();
}
public void DoSearch()
{
DataTable dt = new DataTable();
string where = GetWhereClause();
if (!string.IsNullOrEmpty(where))
{
dt = FillTable("spSelectBidSearchResults", where);
}
gvSearchResults.DataSource = dt;
gvSearchResults.PageSize = 10;
gvSearchResults.PagerTemplate = null;
gvSearchResults.DataBind();
}
private DataTable FillTable(string storedProc, string where)
{
SqlConnection conn = new SqlConnection(ConnectionString());
conn.Open();
SqlCommand cmdProject = new SqlCommand(storedProc, conn);
cmdProject.CommandType = CommandType.StoredProcedure;
cmdProject.Parameters.AddWithValue("@whereclause", where);
SqlDataAdapter da = new SqlDataAdapter(cmdProject);
DataTable dt = new DataTable();
da.Fill(dt);
da.Dispose();
conn.Close();
return dt;
}
private string Symbol(string ddText)
{
if (ddText.Equals("Greater than"))
{
return ">";
}
else
{
return "<";
}
}
private string GetWhereClause()
{
string where = "";
where = GetWhereForCol(txtBidName.Text, "", "BidName", "like", where);
where = GetWhereForCol(txtDescription.Text, "", "Description", "like", where);
where = GetWhereForCol(GetTokens(tiTeamLead), "", "TeamLead", "token", where);
where = GetWhereForCol(GetTokens(tiExecTechLead), "", "ExecutiveTechnicalLead", "token", where);
where = GetWhereForCol(txtConstructionCost.Text, ddCC.Text, "ConstructionCost", "numeric", where);
where = GetWhereForCol(txtFeeValue.Text, ddFV.Text, "FeeValue", "numeric", where);
where = GetWhereForCol(GetTokens(tiStage), "", "Stage", "token", where);
where = GetWhereForCol(GetTokens(tiSector), "", "PrimarySector", "token", where, "SecondarySector");
where = GetWhereForCol(GetTokens(tiService), "", "PrimaryService", "token", where);
where = GetWhereForCol(GetTokens(tiGrade), "", "Grade", "token", where);
where = GetWhereForCol(GetTokens(tiCostCentre), "", "PrimaryCostCentre", "token", where);
where = GetWhereForCol(GetTokens(tiClient), "", "ClientRef", "token", where);
where = GetWhereForCol(GetTokens(tiArea), "", "Area", "token", where);
return where;
}
private string GetWhereForCol(string text, string ddText, string colName, string colType, string where, string otherCol = "")
{
if (!string.IsNullOrEmpty(text))
{
if (colType.Equals("like"))
{
where = AddToWhere(where, colName + " LIKE '%" + text + "%'");
}
else if (colType.Equals("numeric"))
{
string symbl = Symbol(ddText);
where = AddToWhere(where, colName + " " + symbl + " " + text);
}
else if (colType.Equals("token"))
{
if (!string.IsNullOrEmpty(otherCol))
{
where = AddToWhere(where, "(" + colName + " in (" + text + ")");
where += " OR " + otherCol + " in (" + text + "))";
}
else
{
where = AddToWhere(where, colName + " in (" + text + ")");
}
}
}
return where;
}
private string AddToWhere(string where, string clause)
{
if (!string.IsNullOrEmpty(clause))
{
if (string.IsNullOrEmpty(where))
{
where = clause;
}
else
{
where += " AND " + clause;
}
}
return where;
}
private string GetTokens(ASPTokenInput ti)
{
StringBuilder strTokens = new StringBuilder();
List<ASPTokenInput.Item> items = ti.SelectedItems;
foreach (ASPTokenInput.Item item in items)
{
if (strTokens.Length > 0)
{
strTokens.Append(", ");
}
strTokens.AppendFormat("'{0}'", item.id);
}
return strTokens.ToString();
}
public string GetSPSingleValue(string spName, string spParamName, string spParamVal, string connectionStr)
{
try
{
SqlConnection conn = new SqlConnection(connectionStr);
SqlCommand cmd = new SqlCommand(spName, conn);
cmd.Parameters.AddWithValue(spParamName, spParamVal);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
string result = cmd.ExecuteScalar().ToString();
conn.Close();
return result;
}
catch
{
return "";
}
}
void gvSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label l;
DataRowView rowView = (DataRowView)e.Row.DataItem;
string bidRef = rowView["BidRef"].ToString();
string teamLeadID = rowView["TeamLead"].ToString();
string teamLeadName = GetSingleRecord("uspSelectTeamLeadByID", ConnectionString(), "@TeamLead", teamLeadID);
l = (Label)e.Row.Cells[(int)Columns.TeamLead].FindControl("lblTeamLead");
l.Text = teamLeadName;
if (siteCollectionExists(bidRef))
{
l = (Label)e.Row.Cells[(int)Columns.BidRef].FindControl("lblBidRef");
HyperLink hl = new HyperLink();
hl.NavigateUrl = "http://bidstore.gleeds.net/bids/" + bidRef;
hl.Text = bidRef;
e.Row.Cells[(int)Columns.BidRef].Controls.Add(hl);
l.Text = "";
}
}
}
//Check if site collection exists at given web application
private static bool siteCollectionExists(string bidNr)
{
bool returnVal = false;
var r = SPContext.Current.Site.WebApplication.Sites.Where(site => site.Url.Contains(bidNr));
foreach (SPSite s in r)
{
returnVal = true;
}
return returnVal;
}
public string TeamLeads()
{
return TeamLeadsData;
}
public string ExecutiveTechnicalLeads()
{
return ExecLeadsData;
}
public string Stages()
{
return StagesData;
}
public string Sectors()
{
return SectorsData;
}
public string Services()
{
return ServicesData;
}
public string Grades()
{
return GradesData;
}
public string CostCentres()
{
return CostCentresData;
}
public string Clients()
{
return ClientsData;
}
public string Areas()
{
return AreasData;
}
public string GetSingleRecord(string spName, string connectionStr, string param, string value)
{
SqlConnection conn = new SqlConnection();
SqlCommand command = new SqlCommand(spName, conn);
conn = new SqlConnection(connectionStr);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue(param, value);
command.Connection = conn;
conn.Open();
string result = command.ExecuteScalar().ToString();
conn.Close();
return result;
}
public string RunSP(string spName, string idCol, string nameCol, string connectionStr)
{
ArrayList data = new ArrayList();
SqlConnection conn = new SqlConnection();
SqlCommand command = new SqlCommand(spName, conn);
conn = new SqlConnection(connectionStr);
command.CommandType = CommandType.StoredProcedure;
command.Connection = conn;
conn.Open();
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
data.Add(new TokenInputRow(dr[idCol].ToString(), dr[nameCol].ToString()));
}
conn.Close();
return Serialize(data);
}
private static string Serialize(object obj)
{
JavaScriptSerializer ser = new JavaScriptSerializer();
return ser.Serialize(obj);
}
private string ConnectionString()
{
return "Data Source=10.2.40.17;Initial Catalog=BidOpening;Persist Security Info=True;User ID=GTL_BidOpening_User;Password=G@rd3n12";
}
private string CCDConnectionString()
{
return "Data Source=10.2.40.17;Initial Catalog=CCDLive;Persist Security Info=True;User ID=GTL_CCDLive_User;Password=G@rd3n";
}
}
public class TokenInputRow
{
public string id;
public string name;
public TokenInputRow(string _id, string _name)
{
id = _id;
name = _name;
}
}
}
我能够逐步完成页面加载,但我无法进入ASPTokenInput dll。页面加载完成后代码失败。 ti控件包含在我的.ascx文件中,因此
%@ Register TagPrefix="ati" Assembly="ASPTokenInputLib" Namespace="ASPTokenInputLib" %> ati:ASPTokenInput ID="tiTeamLead" runat="server" HintText="Start typing TeamLead..." />