I am trying to submit an InfoPath 2010 form using code behind (VSTA 2010). My Form only include 1 Text Box (TextValue) & one button that labeled as "Submit" that will trigger the form submission code.The form name as "SubmitByCode".
The Code is at the below.
using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using Microsoft.SharePoint;
using System.Data;
namespace SubmitByCode
{
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["Submit"]).Clicked += new ClickedEventHandler(Submit_Clicked);
}
public void Submit_Clicked(object sender, ClickedEventArgs e)
{
//create the navigator
XPathNavigator nav = MainDataSource.CreateNavigator();
int nextID = GetMaxID() + 1;
//set the form name
string formName = "MyForm" + DateTime.Now.Year.ToString() + " - " + nextID.ToString();
//set the field to the form name
nav.SelectSingleNode("/my:myFields/my:TextValue", NamespaceManager).SetValue(formName);
//reference the submit connection
//DataConnection spConn = DataConnection["SharePoint Library Submit"];DataConnection SPConnection = (DataConnection)this.DataConnections["SharePoint Library Submit"];
//submit the form
SPConnection.Execute();
}
public int GetMaxID()
{
SPContext ctx = SPContext.Current;
using (SPSite site = new SPSite(ctx.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
try
{
SPList oList = web.Lists["Order Attachments"];
SPQuery query = new SPQuery();
query.Query = "";
SPListItemCollection items = oList.GetItems(query);
DataTable dtMax = new DataTable();
dtMax = items.GetDataTable();
return (dtMax == null) ? 0 : Convert.ToInt32(dtMax.Compute("Max(ID)", string.Empty));
}
finally
{
if (site != null)
site.Dispose();
}
}
}
}
}
}But when I try to submit the form I got an error as "'Microsoft.Office.InfoPath.DataConnection' is a 'type' but is used like a 'variable'" at the
DataConnection SPConnection = DataConnection("SharePoint Library Submit");statement. I think my references are OK but I cannot understand what this message is saying. Please can someone try to solve this error.
Regards,
Chiranthaka