Connect to C# Program Data Source from Microsoft Access - c#

I have a small C# console program that retrieves an item list from quickbooks, and I am trying to figure out how to expose that data to Microsoft Access. It is in XML format.
I want to retrieve the data in real-time, since it only takes about a second to get the data, whenever Access calls for it. I am using Access 2003 and VS 2010.
If there is a way to do this with VBA that would work fine as well. I can get the XML data using VBA already, but I don't know how to go from there.
Here is the code I use in C#:
public string DoQBQuery(XmlDocument doc)
{
bool sessionBegun = false;
bool connectionOpen = false;
RequestProcessor2 rp = null;
string ticket = "";
try
{
//Create the Request Processor object
rp = new RequestProcessor2();
//Connect to QuickBooks and begin a session
rp.OpenConnection2("", "QB Transaction Item Retriever", QBXMLRPConnectionType.localQBD);
connectionOpen = true;
ticket = rp.BeginSession("", QBFileMode.qbFileOpenDoNotCare);
sessionBegun = true;
//Send the request and get the response from QuickBooks
string responseStr = rp.ProcessRequest(ticket, doc.OuterXml);
//End the session and close the connection to QuickBooks
rp.EndSession(ticket);
sessionBegun = false;
rp.CloseConnection();
connectionOpen = false;
return responseStr;
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error");
if (sessionBegun)
rp.EndSession(ticket);
if (connectionOpen)
rp.CloseConnection();
throw;
}
}

"I am trying to figure out how to expose that data to Microsoft Access. It is in XML format"
I've never tried this before, but I think this is possible by putting the c# classes that accomplish what you discussed in a dll and calling from Access. Here is a post from this site about that.
A Simple C# DLL - how do I call it from Excel, Access, VBA, VB6?
P.S. Given I've never tried this before, I would have put this in the comment section of your original post, but I'm new to this site and am unable to do that.

Related

Executing Bind to LDAP instance using .NET LDAPConnection

I'm trying to execute the binding to an LDAP instance using .NET Objects.
Sorry but this is the first time I fight against this kind of enemy (and hope it will be the last one as well!).
This is what I actually do:
LdapDirectoryIdentifier serverId = new LdapDirectoryIdentifier(primaryIP, securePort);
NetworkCredential credentials = new NetworkCredential(username, password);
using (LdapConnection conn = new LdapConnection(serverId, credentials))
{
try
{
//conn.SessionOptions.ProtocolVersion = 3;
conn.SessionOptions.SecureSocketLayer = true;
conn.AuthType = (AuthType)authType;
conn.Bind();
Console.WriteLine("OK!!");
}
catch (LdapException lex)
{
Console.WriteLine($"Errore {lex.ErrorCode}: {lex.Message}");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
Where:
primaryIP is the name of LDAP instance
securePort is 636
username and password are absolutely correct (I've checked them logging in into the intranet)
I've found many examples, and everything seems pretty plain and simple. Anyway I can't make through it.
Tried also with all the AuthTypes available, with no luck.
As said, the user exists because I've been able to log into different apps that use this kind of authentication.
Ok, got it on my own.
The username must be set with the full DN.
Now it works correctly.

LDAP signing and channel binding for the march 10th update in c# .net

I have been looking into how these two new settings will effect with our c# code that connects to an ldap server and performs user lookups
Using the code below to connect to an AD i have found a few scenarios that no longer work when these settings are switched on.
private static LdapConnection ConnectAndBind(
string server,
int port,
int timeout,
string userName,
string pwd,
AuthType authType,
bool useSSL,
bool useV3)
{
var con = new LdapConnection(new LdapDirectoryIdentifier(server, port));
if (useSSL)
{
con.SessionOptions.SecureSocketLayer = useSSL;
con.SessionOptions.VerifyServerCertificate = VerifyServerCertificate;
con.SessionOptions.QueryClientCertificate = QueryClientCertificate;
}
con.Timeout = new TimeSpan(0, 0, timeout);
con.SessionOptions.ProtocolVersion = useV3 ? 3 : 2;
try
{
con.AuthType = authType;
con.Credential = new NetworkCredential(userName, pwd);
con.Bind();
}
catch (Exception e)
{
throw new ProviderException(
ProviderException.ErrorIdentifier.AuthenticationFailed,
LanguageLogic.GetString("AuthenticationProvider.ConnectError"),
e.Message);
}
return con;
}
This is used in the context of a webforms/mvc asp.net (4.5) app once connected its used to import user details in the the app
but at the moment depending on how the registry keys for the two settings on the AD server are set i am finding some situations where it does not connect (the error returned is that the supplied credentials are invalid)
The first two tables are kinda how i expected it to work with non signed/non ssl basic bind not working
how ever i cannot find a reason why when the Channel binding is set to required (table 3) it does not work for the other 3 red entries
Has any one else been working on this that could shed some light on the matter. would newer version of .net support this setting.
Thanks for any info
UPDATE 1
so i downloaded Softerra LDAP browser. i get the same results using that . so i dont think its my code.
as soon as i turn on the reg key for Channel Binding i get the specified credentials are invalid for those connection methods over SSL
i have updated the AD server with all the latest patches but no change.

Why I can't insert a file into a SharePoint list via code? It goes into exception, seems that the access is denied

I am very new in SharePoint (I am using SharePoint 2013) and I am experiencing a strange problem. This is very strange because in another section of my application it works fine (in another subsite).
So basically into SharePoint I have a SharePoint list named Protocollo.
My code contains the following lines that add a document (a file) into a subfolder of the previous SharePoint List:
internal static int InsertItem(Dictionary<string, object> prot, Documento doc, bool assignVisibility, bool newItem)
{
int state = 0;
SPListItem item = null;
UOR currUOR = null;
List<UOR> path = new List<UOR>();
SPWeb web = SPContext.Current.Web;
string siglaAOO = web.Properties["sigla_aoo"];
DBConnection dbConfig = ArxeiaProtocollo.Util.ProtUtils.InitializeDBConnection();
dbConfig.Database = siglaAOO;
string username = web.CurrentUser.LoginName;
try
{
SPList list = web.Lists["Protocollo"];
web.AllowUnsafeUpdates = true;
SPFolderCollection folders = list.RootFolder.SubFolders;
SPFolder annoFolder;
DateTime dateProt = Convert.ToDateTime(prot["Data protocollo"]);
try
{
annoFolder = folders[dateProt.Year.ToString()];
}
catch
{
annoFolder = folders.Add(dateProt.Year.ToString());
}
SPFolder meseFolder;
try
{
meseFolder = annoFolder.SubFolders[dateProt.Month.ToString("D2")];
}
catch
{
meseFolder = annoFolder.SubFolders.Add(dateProt.Month.ToString("D2"));
}
SPFolder dayFolder;
try
{
dayFolder = meseFolder.SubFolders[dateProt.Day.ToString("D2")];
}
catch
{
dayFolder = meseFolder.SubFolders.Add(dateProt.Day.ToString("D2"));
}
SPFile spFile = dayFolder.Files.Add(doc.Nome, doc.File, true);
............................................................
............................................................
............................................................
}
As you can see the previous code retrievce the Protocollo list from the current website allowing updates on it by:
SPList list = web.Lists["Protocollo"];
web.AllowUnsafeUpdates = true;
Then into this list it creates (it doesn't exist) a gerarcic folders structure for year (annoFolder), month (meseFolder) and day (dayFolder).
It works fine, I tried to delete these folder structure from my SharePoint site and performing this method it is created again, infact this is what I obtained:
As you can see it correctly creates this folder structure into my SharePoint list (named Protocollo) into the current website.
Ok finnally my code try to insert a document into the last subfolder (the dayfolder) by:
SPFile spFile = dayFolder.Files.Add(doc.Nome, doc.File, true);
I am passing to the Add() method: the name of the file, the byte array representing the file and the true boolean value.
The problem is that performing this line I obtain the following exception that is not providing information:
{Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}
Then in my front end it appears a "denied access" popup window.
The strange thing is that another sites in my SharePoint that uses the same code have no problem. Another strange thing is that manually uploading the file into this location of the list it works fine so I think that it should not be a user permission problem.
Some idea? What can I try to do to solve this strange problem?
SharePoint codes run using the Application Pool user in IIS not the user that you have logged in to SharePoint, so it is common to get an access denied error even when you have access. So I would suggest you check the permission for the AppPool account on the Protocollo library. Or you can use SPSecurity.RunWithElevatedPrivileges if you have trust in the user that will run the code.
Beware of the pitfalls though.
Here is a sample usage:
Guid siteId = SPContext.Current.Site.ID;
Guid webId = SPContext.Current.Web.ID;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteId))
{
using (SPWeb web = site.OpenWeb(webId))
{
// Your code here
}
}
});

Error While posting Data to a web application from a windows service."The remote server returned an error: (500) Internal Server Error."

I'm getting this error while trying to post data to a hosted web application from a windows service.
PostSubmitter post = new PostSubmitter();
post.Url = "http://192.168.0.1/Invoice/Invoice1.aspx";
post.PostItems.Add("subscriberid", subscriberid.ToString());
post.PostItems.Add("StartDate", StartDate);
post.PostItems.Add("EndDate", EndDate);
post.PostItems.Add("AdvanceBillDate", AdvanceBillDate);
post.Type = PostSubmitter.PostTypeEnum.Post;
try
{
string res = post.Post();
}
catch (Exception exp)
{
}
This is code snippet of my windows service which posts data to web application.
Does any one know the reason.I'm using asp .Net C#
Compare your request from C# with one done in a browser.
Use fiddler to do this.
You should be able to compare everything from header values, to complete post data, etc. and be able to figure out what you have missing. I would suspect you are leaving out required a value and the server application is throwing a (likely unexpected) exception.
Finally i got wat was missing.Actually i was posting data to the web application and reading it using Request.QueryString......Which is actually how Get Method is read.So modified my code as
PostSubmitter post = new PostSubmitter();
post.Url = "http://192.168.0.1/Invoice/Invoice1.aspx";
post.PostItems.Add("subscriberid", subscriberid.ToString());
post.PostItems.Add("StartDate", StartDate);
post.PostItems.Add("EndDate", EndDate);
post.PostItems.Add("AdvanceBillDate", AdvanceBillDate);
post.Type = PostSubmitter.PostTypeEnum.Get;
try
{
string res = post.Post();
}
catch (Exception exp)
{
}

Meet with error -- "Updates are currently disallowed on GET requests"

I am using SharePoint Server 2007 Enterprise with Windows Server 2008 Enterprise. I have deployed a publishing portal. I am developing a ASP.Net web application using VSTS 2008 + C# + .Net 3.5 + ASP.Net + SharePoint Server 2007 SDK.
Here is my code snippets and I got error -- "Updates are currently disallowed on GET requests". Any ideas how to fix?
Microsoft.SharePoint.SPException: Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb.
protected void Page_Load(object sender, EventArgs e)
{
try
{
SPWebApplication webApp = SPContext.Current.Site.WebApplication;
SPSiteCollection siteCollections = webApp.Sites;
foreach (SPSite item in siteCollections)
{
SPWeb webItem = item.OpenWeb();
webItem.AllowUnsafeUpdates = true;
}
SPSite newSiteCollection = siteCollections.Add("sites/myblog",
"New Sample Site", "New Sample Site Description", 1033, "BLOG#0",
"foo\\foouser", "Owner name", "owneremail#foo.com");
}
catch (Exception ex)
{
Response.Write(ex.ToString() + "\t" + ex.StackTrace);
}
}
The problem why you are not allowed to read/write to database on GET request is because your code will be exploitable via a cross-site scripting. Read about AllowUnsafeUpdates consequences here.
Anyway, if you like, you can set this SPWeb.AllowUnsafeUpdates to true, but use it like this:
try {
web.AllowUnsafeUpdates = true;
...
}
finally {
web.AllowUnsafeUpdates = false;
}
Add a check to ensure that you are getting a POST instead of a GET before you attempt to allow updates. Make sure that whatever is making the change does it via a POST request rather than using URL parameters and a GET.
if (IsPostBack)
{
...
}

Categories

Resources