C# 在InfoPath 2010中使用C运行SQL通配符查询
我构建了一个InfoPath 2010表单,用于查询和编辑已输入Access 2010数据库的数据。我遇到的问题是,没有编写代码就无法直接通过InfoPath运行通配符。经过广泛搜索,我找到了以下示例,我已尝试使用InfoPath表单中的字段重新创建该示例: 我遇到的问题是,在运行代码后,我得到的NullReferenceException未被行上的用户代码错误处理: 字符串strPayDate=xnPaymentLookupQuery。SelectSingleNode@Pay_Date,this.NamespaceManager.Value 下面是我的全部脚本。有人对问题的起因有什么建议吗C# 在InfoPath 2010中使用C运行SQL通配符查询,c#,sql,ms-access-2010,infopath2010,C#,Sql,Ms Access 2010,Infopath2010,我构建了一个InfoPath 2010表单,用于查询和编辑已输入Access 2010数据库的数据。我遇到的问题是,没有编写代码就无法直接通过InfoPath运行通配符。经过广泛搜索,我找到了以下示例,我已尝试使用InfoPath表单中的字段重新创建该示例: 我遇到的问题是,在运行代码后,我得到的NullReferenceException未被行上的用户代码错误处理: 字符串strPayDate=xnPaymentLookupQuery。SelectSingleNode@Pay_Date,thi
using Microsoft.Office.InfoPath;
using System;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using mshtml;
namespace PaymentLookup
{
public partial class FormCode
{
// Member variables are not supported in browser-enabled forms.
// Instead, write and read these values from the FormState
// dictionary using code such as the following:
//
// private object _memberVariable
// {
// get
// {
// return FormState["_memberVariable"];
// }
// set
// {
// FormState["_memberVariable"] = value;
// }
// }
// NOTE: The following procedure is required by Microsoft InfoPath.
// It can be modified using Microsoft InfoPath.
public void InternalStartup()
{
((ButtonEvent)EventManager.ControlEvents["btnQuery"]).Clicked += new ClickedEventHandler(btnQuery_Clicked);
}
public void btnQuery_Clicked(object sender, ClickedEventArgs e)
{
//Create an XPathNavigator object for the main data source
XPathNavigator xnMain = this.MainDataSource.CreateNavigator();
//Create an AdoQueryConnection from the main data source by "casting" the default
//data connection to an "AdoQueryConnection"
AdoQueryConnection cn = (AdoQueryConnection)this.DataConnections["Main connection"];
//Obtain the default SQL command for the form.
string strOrigSQLCommand = cn.Command.ToString();
// Obtain the query node that you want to change.
XPathNavigator xnPaymentLookupQuery = xnMain.SelectSingleNode("/dfs:myFields/dfs:queryFields/q:PaymentBatchesToDate/Pay_Date", this.NamespaceManager);
//Obtain the text that was entered for the wildcard character search, and then clear the current query parameter so that InfoPath will leave the current query parameter blank.
string strPayDate = xnPaymentLookupQuery.SelectSingleNode("@Pay_Date", this.NamespaceManager).Value;
xnPaymentLookupQuery.SelectSingleNode("@Pay_Date", this.NamespaceManager).SetValue(string.Empty);
//Have InfoPath construct an SQL command that is based on all other field values.
string strMySQLCommand = cn.BuildSqlFromXmlNodes(xnPaymentLookupQuery);
//Save the other query items, and then clear the other query items before the next query.
string strPortfolioID = xnPaymentLookupQuery.SelectSingleNode("@Portfolio_ID", this.NamespaceManager).Value;
xnPaymentLookupQuery.SelectSingleNode("@Portfolio_ID", this.NamespaceManager).SetValue(string.Empty);
string strTaxEntity = xnPaymentLookupQuery.SelectSingleNode("@Tax_Entity", this.NamespaceManager).Value;
xnPaymentLookupQuery.SelectSingleNode("@Tax_Entity", this.NamespaceManager).SetValue(string.Empty);
string strFileType = xnPaymentLookupQuery.SelectSingleNode("@File_Type", this.NamespaceManager).Value;
xnPaymentLookupQuery.SelectSingleNode("@File_Type", this.NamespaceManager).SetValue(string.Empty);
string strPayType = xnPaymentLookupQuery.SelectSingleNode("@Pay_Type", this.NamespaceManager).Value;
xnPaymentLookupQuery.SelectSingleNode("@Pay_Type", this.NamespaceManager).SetValue(string.Empty);
string strPaymentBatch = xnPaymentLookupQuery.SelectSingleNode("@Payment_Batch", this.NamespaceManager).Value;
xnPaymentLookupQuery.SelectSingleNode("@Payment_Batch", this.NamespaceManager).SetValue(string.Empty);
//Add Pay_Date to the query so that Pay_Date can support wildcard characters.
if (strPayDate != string.Empty)
{
if (strMySQLCommand != string.Empty)
strMySQLCommand += " AND ";
//Check whether the user entered the wildcard character (%) as part of the title.
if (strPayDate.Contains("%"))
strMySQLCommand += "[Pay Date] LIKE '" + strPayDate + "'";
else
strMySQLCommand += "[Pay Date] LIKE '" + strPayDate + "%'";
}
//Construct the full query string.
string strSQLQuery = strOrigSQLCommand;
if (strMySQLCommand != string.Empty)
strSQLQuery += " WHERE " + strMySQLCommand;
//Set the command and run the query.
cn.Command = strSQLQuery;
cn.Execute();
//Restore all the user entries to the Query fields so that the user entries will
//be available if you want to change and to rerun the query.
xnPaymentLookupQuery.SelectSingleNode("@Portfolio_ID", this.NamespaceManager).SetValue(strPortfolioID);
xnPaymentLookupQuery.SelectSingleNode("@Tax_Entity", this.NamespaceManager).SetValue(strTaxEntity);
xnPaymentLookupQuery.SelectSingleNode("@File_Type", this.NamespaceManager).SetValue(strFileType);
xnPaymentLookupQuery.SelectSingleNode("@Pay_Type", this.NamespaceManager).SetValue(strPayType);
xnPaymentLookupQuery.SelectSingleNode("@Payment_Batch", this.NamespaceManager).SetValue(strPaymentBatch);
xnPaymentLookupQuery.SelectSingleNode("@Pay_Date", this.NamespaceManager).SetValue(strPayDate);
//Restore the default table command (for the next time).
cn.Command = strOrigSQLCommand;
//Clean up
xnMain = null;
cn = null;
xnPaymentLookupQuery = null;
}
}
}
很可能是你打电话给 xnPaymentLookupQuery。SelectSingleNode@Pay_Date,此为.NamespaceManager 正在返回null,因此当您尝试访问Value属性时,它将失败 您需要对该方法返回的内容进行空检查:
string strPayDate = string.Empty;
var strPayDateNode = xnPaymentLookupQuery.SelectSingleNode("@Pay_Date", this.NamespaceManager);
if(strPayDateNode != null)
{
strPayDate = xnPaymentLookupQuery.SelectSingleNode("@Pay_Date", this.NamespaceManager).Value;
}
与论坛网站不同,我们不使用感谢、感谢的帮助或签名。看见