Archive | C# RSS feed for this section

OLE DB (MS Access) database connection problem in 64bit operating with C#

19 May

From last couple of weeks I am developing a desktop software called “TSF Core” built with C#. This software is replacement of a web (Apache, MySQL) admin panel based one web service (WSDL). I am using MS Access as a cash database to make the more process faster.

I was getting an exception with my database connection in 64bit vista ultimate bit it was working fine in XP 32bit, 2000 and VISTA 32 bit OS.

I am using Microsoft.Jet.OLEDB.4.0; to connect with .mdb. Connection string is something like that:

new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=tsf_barcode_reader.mdb");

I got a strange result when I Googled on it. There is no OLEDB version for 64Bit Operating system.

But there is a good solution for that at least it worked for my software. We can specify the software platform in project definition. To do this please open "my_project.csproj" in notepad or any other plain text editor and add the following code under "PropertyGroup" tag.

<PlatformTarget>x86</PlatformTarget>

Code will be some ting like that:

Previouse code:

<PropertyGroup Condition=" ‘$(Configuration)|$(Platform)’ == ‘Debug|AnyCPU’ ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" ‘$(Configuration)|$(Platform)’ == ‘Release|AnyCPU’ ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>

Edited Cdoe

<PropertyGroup Condition=" ‘$(Configuration)|$(Platform)’ == ‘Debug|AnyCPU’ ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" ‘$(Configuration)|$(Platform)’ == ‘Release|AnyCPU’ ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>

In this situation the operations system will call 32 bit compatible database connection provider for that software and the problem will be solved.

Advertisements

Organize a C# project to customize it from outside of source code

1 Apr

For any project we feel to process some configuration variables so that we can customize the software from out of sources code. It is important to make those variables available in each part of the project minimizing the system load. We can put the values either in an xml or Database. In my last C# project I’ve followed an interesting process.

I used a Single Tone Pattern Class to process my variables and declare the constructor “private” so that we can’t create more than one instance with “new” keyword.

private Dictionary sconfig;

Dictionary is a fantastic element to store temporary data. Here I used it something like associative array.

private static BCReaderConfiguration _bcrConfigObj = null;
public static BCReaderConfiguration getObject()
{
if (_bcrConfigObj == null)
{
_bcrConfigObj = new BCReaderConfiguration();
_bcrConfigObj.setConfig();
return _bcrConfigObj;
}
else
{
return _bcrConfigObj;
}
}

This the most interesting part of my coding. getObject() is a static method so that I can call it from anywhere with the name of class. Like:

BCReaderConfiguration.getObject();

I have declared a private variable with the type of my configuration class (“BCReaderConfiguration”) and storing the first instance of this object. Each time I call the function form other part of my coding first I check if the object already created. If this object is not created (First time call) then it’s creating new object otherwise returning the previous one. This process will save my Physical Memory as well as time because we are using same object for thousands of calls.

Let’s consider an XML file where all configuration variables are saved. Here is the example XML file :

<sconfig>
<node><svalue soption="check_update_title">Wait Checking for update</svalue> </node> <node><svalue soption="bad_database_structure">Bad Database structure!</svalue> </node> <node><svalue soption="guest_success_msg">Welcome!</svalue> </node>
<node><svalue soption="guest_failed_msg">Sorry</svalue> </node>
<node><svalue soption="general_inconvenience_msg">Sorry! For inconvenience</svalue> </node> <node><svalue soption="about">Version 1.0</svalue></node>
</sconfig>

Each “node” tag preserving one configuration value inside the tag and attribute “svalue soption” containing the variable name.

private XmlDocument getConfigXML ()
{
XmlDocument document = new XmlDocument();
return document.Load(“xmls\\config.xml”);
}

getConfigXML() is a simple function to read the xml and return the Xml type object.

private void setConfig()
{
this.sconfig = new Dictionary();
// Collect the xml data

XmlDocument sconfigXmlDoc = this.getConfigXML ();

XmlNodeList list = null;
list = sconfigXmlDoc.GetElementsByTagName(“node”);

foreach (XmlNode val in list)
{
this.sconfig[val[“svalue”].GetAttribute(“soption”)] = val[“svalue”].InnerText;
}
}

This is a pretty easy section. Here I am populating all my data in a 2D Dictionary. XmlNodeList is a C# data type to process Xml node from XmlObject.

Well, we are all most done. Now we need a Public function that can return our desired configuration variable.

public String get_config(String soption)
{
try
{
return this.sconfig[soption];
}
catch
{
return “”;
}
}

I used “get_config()” method that accept a Configuration variable name as parameter and return the desired value and in case of any exception it will return an empty string.

Finally, to get any configuration variable we will call something like this:

BCReaderConfiguration.getObject().get_config(“guest_success_msg”)

Full source Code here:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
namespace barcodereader
{
class BCReaderConfiguration
{
private Dictionary sconfig;
private static BCReaderConfiguration _bcrConfigObj = null;

private BCReaderConfiguration()
{
}

// Single Tone object generator
public static BCReaderConfiguration getObject()
{
if (_bcrConfigObj == null)
{
_bcrConfigObj = new BCReaderConfiguration();
_bcrConfigObj.setConfig();
return _bcrConfigObj;
}
else
{
return _bcrConfigObj;
}
}

private XmlDocument getConfigXML ()
{
XmlDocument document = new XmlDocument();
return document.Load(“config.xml);
}

// Populate the configuration in to array
private void setConfig()
{
this.sconfig = new Dictionary();
XmlDocument sconfigXmlDoc = this.getConfigXML();
XmlNodeList list = null;
list = sconfigXmlDoc.GetElementsByTagName(“node”);

foreach (XmlNode val in list)
{
this.sconfig[val[“svalue”].GetAttribute(“soption”)] = val[“svalue”].InnerText;
}
}

// Return the Configuration value
public String get_config(String soption)
{
try
{
return this.sconfig[soption];
}
catch
{
return “”;
}
}
}
}