PostHeaderIcon Windows 7 odbcad32

In windows 7 64 bit there are two odbcad32.exe applications to add ODBC sources. The odbcad32.exe located in system32 is for all the 64 bit drivers, the odbcad32.exe located in WOW64 is for all the 32 bit drivers.

As a software developer you run into the issue that sometimes only the 32 bit odbc driver is avialable but the application by default runs in 64 bit mode which will cause the database connection to fail.

You do not have to change every single assembly Platform target to x86 in order to resolve this issue. You only have to change the Platform target for the starting point of your application as long as that starts in 32 bit all other assemblies will be loaded sa 32 bit as well and it will load the correct ODBC driver.

PostHeaderIcon The output char buffer is too small to contain the decoded characters

The output char buffer is too small to contain the decoded characters, encoding ‘Unicode (UTF-8)’ fallback ‘System.Text.DecoderReplacementFallback’.


if (brEventReader.PeekChar() != 0)
{
  return new string(brEventReader.ReadChars(stringLength)).Trim();
}
else
{
  brEventReader.ReadChars(stringLength);
  return "";
}

In general, this arises when the incorrect encoding is set on the BinaryReader (the default one is UTF8) from what is encoded in the stream. PeekChar() (and ReadChar()) methods in BinaryReader will attempt to decode the bytes in the stream to fit in with the specified encoding and can fault if they are not compatible

The solution is to not use the PeekChar or ReadChar methods on the binary reader but to always use the Bytes methods.

Changed to


byte[] tvByteArray = new byte[stringLength];
tvByteArray = brEventReader.ReadBytes(stringLength);

System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
string tvTemp = enc.GetString(tvByteArray);

//remove all the \0 and trim the string
return tvTemp.Replace("\0", "").Trim();  

PostHeaderIcon CSS Select Parent Element

It is impossible to modify the parent element when something is happening to a child example.

For example if a textbox has focus I would like to change the parent div element background color.

The way to do this is with javascript. I know it is not ideal to have to use Javascript to get the desired result but in this case it only takes two lines of javascript and it looks pretty sharp.

To get the desired effect you need to add an event to the child onFocus and onBlur event. You can use different events to get differente effects such as the onMouseEnter, onMouseLeave to have a hover effect.


<style>
/*These are not required for the example but it makes it appear nicer*/
input
{
  border:1px solid #28557c;
}

div
{
  padding:3px;
}
</style>

<script>
function TextBoxOnFocus(obj)
{
  obj.parentNode.style.backgroundColor = '#4789c3';
}

function TextBoxOnBlur(obj)
{
  obj.parentNode.style.backgroundColor = '';
}
</script>

<div style="background-color:#bad3e9;">
<div>
  <input type=text onfocus="TextBoxOnFocus(this)" onblur="TextBoxOnBlur(this)"/>
</div>
<div>
  <input type=text onfocus="TextBoxOnFocus(this)" onblur="TextBoxOnBlur(this)"/>
</div>
<div>
  <input type=text onfocus="TextBoxOnFocus(this)" onblur="TextBoxOnBlur(this)"/>
</div>
</div>

Demo, click on the textboxes to see the background div’s color change.

Since this only requires one line of code you can even inline the javascript.

PostHeaderIcon ScriptManager, OnLoad Event, RegisterStartupScript

When making use of all three within the same page you have to be carefull to make sure that one script is not clobbering another script, or that one script is not causing another script to fail.

Pay attention to the following scenerios to save you a lot of headaches.

If you use a ScriptManager don’t use the body onload event, place the following line of javascript at the bottom of the page within script tags.
Sys.Application.add_load(InitializePage);

If you use RegisterStartupScript ensure that the script is terminated with a semicolon other wise when the javascript is combined at the bottom of the html page it will contain a javascript error because of the missing semicolon.

PostHeaderIcon SQL Server Alter Statements

It is not very common to create a database via scripts or modify a database via scripts, most often the changes are done via a designer which does make the job a lot easier.

In some cases it is worthwhile to do it via a script one of the instances where I use scripts a lot is when I make changes to a development database and I know that I have to make same changes to a production database at some point.

Below is a list of the most common alter statements to modify the structure of  a database

  • Add a column to a database
      
      ALTER TABLE table_name
      ADD column_name datatype
      
      • Add a primary key to a database
      
      ALTER TABLE table_name
      ADD CONSTAINT constraint_name
      PRIMARY KEY (field_name)
      
      • Add an index to a table
      
      CREATE NONCLUSTERED INDEX index_name
      ON table_name (field_name ASC)
      
      • Add a foreign constraint to a table
      
      ALTER TABLE table_name
      WITH CHECK ADD CONSTRAINT contraint_name
      FOREIGN KEY (field_name)
      REFERENCES table_name (field_name)
      
      ALTER TABLE table_name
      CHECK CONSTRAINT constraint_name
      

      Note that there are some very good tools out there as well to determine the structural changes between a development and production database but you may not always have access to these tools.

PostHeaderIcon Computer Automation

Over the last few weeks have been working a lot with computer automation. The challenge with automation is to keep it running even when exceptions are happening.

The project I have been working on moves data on a regular interval between MySQL database and a SQL Server database. The MySQL database lives on a linux machine and is accessed via a SSH tunnel.

The following three rules will ensure that the automation runs quite reliable.

  1. Be able to restart the process at any point.
  2. Be able to process the same data without causing issues E.g. duplication
  3. Be able to chunk the data. E.g. if the process has not run in a long time only process a limited number of records at a time.

PostHeaderIcon XML – Binary – JSon Serialization

It appears that there are more than one way to skin a cat and sometimes it tough to decide on how to skin a cat. With serialization there are three common ways to serialize an object each has there advantages and disadvantages.

XML Serialization

XML serialization allows you to exchange data between disjoint systems e.g. C# and PHP. The downside of XML serialization is that it is very verbose and out of the box C# does not support serialization of a dicationary.

Example of XML Serialization


using System.Xml.Serialization;
StreamWriter _XMLOut = new StreamWriter(_filepath);
XmlSerializer _ProjectSerializer = new XmlSerializer(typeof(MyObject));
_ProjectSerializer.Serialize(_XMLOut, _MyObject);
_XMLOut.Close();

Example of XML Deserialization


using System.Xml.Serialization;
StreamReader _XMLIn = new StreamReader(_filepath);
XmlSerializer _ProjectSerializer = new XmlSerializer(typeof(MyObject));
_MyObject = (MyObject)_ProjectSerializer.Deserialize(_XMLIn);
_XMLOut.Close();

Binary Serialization

Binary serialization is less verbose and does support serialization of dicationaries however you can not exchange binary serialized messages between disjoint systems.

Example of Binary Serialization


using System.Runtime.Serialization.Formatters.Binary;
FileStream _stream = new FileStream(_filepath, FileMode.CreateNew);
BinaryFormatter _Formatter = new BinaryFormatter();
_Formatter.Serialize(_stream, _MyObject);
stream.Close();

Example of Binary Deserialization


using System.Runtime.Serialization.Formatters.Binary;
FileStream _stream = new FileStream(_filepath, FileMode.Open);
BinaryFormatter _Formatter = new BinaryFormatter();
_MyObject = (MyObject)objFormatter.Deserialize(_stream);

JSon Serialization

JSon serialization is less verbose allows for seriaization of dictionaries and can be used to exchange messages between disjoint systems the downfall of JSon is that the serialized information contains no type definitions.

Example of JSON Serialization


using System.Runtime.Serialization.Json;
DataContractJsonSerializer _Serializer = new DataContractJsonSerializer(_MyObject.GetType());
MemoryStream _DataMemoryStream = new MemoryStream();
_Serializer.WriteObject(_DataMemoryStream , _MyObject);

Example of JSON Deserialization


using System.Runtime.Serialization.Json;
MemoryStream _MemoryStream = new MemoryStream(_DataStream);
DataContractJsonSerializer _Deserializer = new DataContractJsonSerializer(_MyObject.GetType());
return (MyObject)_Deserializer.ReadObject(_MemoryStream);

One major negative aspect JSON serialization is that you need the same version of the object to serialize and deserialize it. The deserializer will fail if the newer version of the object as an additional property.

PostHeaderIcon Dicationary Serialization

Back to serialization again this is a beast that just wont die every time I think I know everything about serialization that there is to know about I get whacked over the head with it. One of the big issues with xml serialization is that it will not serialize a dictionary if you try you get an error like this

Cannot serialize member SSD.SourceControl.Modules.Files.Monitors.Business.MonitorSettings.Accesses of type System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], because it implements IDictionary.

You have four ways to deal with this

Personally my preference is to either user binary serialization or JSON serialization if that is an option.

PostHeaderIcon Updating MS Access Linked Tables With c#

For one of my contracts I decided to split the MS Access into two database one to collect the data and one for reporting. The reporting database would linked in all the tables from the data database. This way I can modify the reports email the client the report database and do not need to port any data over to the newer database or provide scripts to update the database.

The problem is that the linked table source database is full hardcoded path. To keep it easy for the clients I needed a way to have the program updated the linked table source database path from the program.

I googled around but was not able to find a solution but did find some leads which led to the following code snippet. The code snippet reset the linked tables source database path.


ADODB.Connection Con = new ADODB.Connection();
ADOX.Catalog Cat = new ADOX.Catalog();

Con.Open(connectionString, null, null, 0);
Cat.ActiveConnection = Con;
Cat.Tables[LinkedTableName].Properties["Jet OLEDB:Link Datasource"].Value = LinkedDatabaseLocation;
Con.Close();

The same code can also be used to create a new linked table as well.

PostHeaderIcon HTML Layout – CSS or Tables

The css versus tables for layout is a holy war, there are two camps.

  • Only use css
  • Use a mixture

    When I started developing websites in 1998 everything was done in tables and having 3-4 nested tables was a nightware. I must say that I got really good at using the rowspan and colspan attribute to reduce the numbers of nested tables. Now-a-day we see people that only use css since it abstracts the presentation from the content.

    Here are my thoughts

  • css does not deal well with height. Anything height is very complicated in css, e.g. equal height column.
  • css uses a lot of nested divs to get the required effect
  • placing css in a seperate file that deals with presentation (font, colour, alignment etc) makes a lot of sense
  • placing css in a seperate file that deals with layout (positioning) does not make a lot of sense. Since the layout tends to closely tied to content and it just seems silly to place css code in a file if it is only used once in the whole website.
  • css is very good at overlapping elements.

    After having gone through the pain of creating websites with pure css I am back at where I use one table to create the master layout for the page. Everything else is done with css. This method really simplefies the website a lot and removes a lot of css.

    In my opinion anything that makes it simple and reduces code is a good thing even if there are a lot of people pushing for pure css.