Tuesday, November 19, 2013

System.BadImageFormatException: Could not load file or assembly 'xxx.dll'or one of its dependencies. An attempt was made to load a program with an incorrect format.

If you encounter below error when running Visual Studio .NET but compilation is successful.
System.BadImageFormatException: Could not load file or assembly 'xxx.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Resolution:
1) Ensure that all projects have the same .net framework version you can check this from each project properties.
2) Ensure that the target platform is Any CPU on solution properties and each project properties (for .NET it's under Compile section, for C# it's under Build section).

Reference: here

Tuesday, October 8, 2013

Windows 7 upgrade error - External table is not in the expected format.

Before the upgrade it's working fine, after upgrade, my user experienced that error:
External table is not in the expected format.

After debugging, found out that there's 2 files being processed, even though my user only puts 1 file into the upload folder.
Turned out to be the second file is Thumbs.db, usually used for thumbnails.
But I cannot see this file in the folder itself.
So what do we need to do?
Simple, just ask your user to go to Windows Explorer, go to Tools, Folder Options

On View tab, tick the first one, Always show icons, never thumbnails then click ok.
Go to that upload folder, remove any Thumbs.db if there's is any.
It should solve the problem.

Reference: here

Thursday, September 26, 2013

Cleaning COM Objects when coding using Interop.Excel

Proper coding with releasing of the COM objects must be done when coding to create excel reports using Microsoft.Office.Interop.Excel in order to avoid memory leak.

Please refer to this example to automate excel with proper releasing of COM objects in VS2010.
In the example, Solution1.AutomateExcel demonstrates automating Microsoft Excel application by using Microsoft Excel Primary Interop Assembly (PIA) and explicitly assigning each COM accessor object to a new variable that you would explicitly call Marshal.FinalReleaseComObject to release it at the end. When you use this solution, it is important to avoid calls that tunnel into the object model because they will orphan Runtime Callable Wrapper (RCW) on the heap that you will not be able to access in order to call Marshal.ReleaseComObject. You need to be very careful. For example,

  Excel.Workbook oWB = oXL.Workbooks.Add(missing);

Calling oXL.Workbooks.Add creates an RCW for the Workbooks object. If you invoke these accessors via tunneling as this code does, the RCW for Workbooks is created on the GC heap, but the reference is created under the hood on the stack and are then discarded. As such, there is no way to call MarshalFinalReleaseComObject on this RCW. To get such kind of RCWs released, you would either need to force a garbage collection as soon as the calling function is off the stack (see Solution2.AutomateExcel), or you would need to explicitly assign each accessor object to a variable and free it.

  Excel.Workbooks oWBs = oXL.Workbooks;
  Excel.Workbook oWB = oWBs.Add(missing);

In the example, Solution2.AutomateExcel demonstrates automating Microsoft Excel application by using Microsoft Excel Primary Interop Assembly (PIA) and forcing a garbage collection as soon as the automation function is off the stack (at which point the Runtime Callable Wrapper (RCW) objects are no longer rooted) to clean up RCWs and release COM objects.

public static void AutomateExcel()
        {
            AutomateExcelImpl();


            // Clean up the unmanaged Excel COM resources by forcing a garbage
            // collection as soon as the calling function is off the stack (at
            // which point these objects are no longer rooted).

            GC.Collect();
            GC.WaitForPendingFinalizers();
            // GC needs to be called twice in order to get the Finalizers called
            // - the first time in, it simply makes a list of what is to be
            // finalized, the second time in, it actually is finalizing. Only
            // then will the object do its automatic ReleaseComObject.
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

Read more on the Marshal.ReleaseCOMObjects is dangerous to be used here.

Wednesday, September 18, 2013

Sharepoint WS - List - Delete file

here is the code to delete a file.
Note that FileRef cannot use encoded URL.
For the GetDocID method can refer to this post.

 private static void DeleteFile(string FilePath)  
     {  
       try  
       {  
         using (ListWS.Lists listService = new ListWS.Lists())  
         {  
           listService.Url = ListURL;  
           listService.Credentials = System.Net.CredentialCache.DefaultCredentials;  
           listService.Credentials = new System.Net.NetworkCredential(username, password);  
           //Get the file id  
           string fileName = FilePath.Substring(FilePath.LastIndexOf("/") + 1);  
           string DocID = GetDocID(fileName);  
           //get guid  
           string lListName = "EDMS";  
           XmlNode lSharePointListName = listService.GetList(lListName);  
           string lListID = lSharePointListName.Attributes["ID"].Value;   
           XmlDocument doc = new XmlDocument();  
           //Perform deletion  
           string xmlCommand;  
           doc = new XmlDocument();  
           //ID is the ID field value of MyFolder element  
           xmlCommand = "<Method ID='1' Cmd='Delete'><Field Name='ID'>" + DocID + "</Field><Field Name='FileRef'>" +   
             FilePath.Replace("%20"," ") + "</Field></Method>";  
           XmlElement ele = doc.CreateElement("Batch");  
           ele.SetAttribute("OnError", "Continue");  
           ele.SetAttribute("ListVersion", "1");  
           //ele.SetAttribute("RootFolder", FilePath.Substring(0, FilePath.LastIndexOf("/")));  
           ele.InnerXml = xmlCommand;  
           XmlNode node1 = listService.UpdateListItems(lListID, ele);  
           if ((node1 != null) && (node1.InnerText == SUCCESS))  
           {  
             Console.WriteLine("SUCCESS");  
           }  
           else  
           {  
             Console.WriteLine("There is an error : " + node1.InnerText);  
           }  
         }  
       }  
       catch (Exception ex)  
       {  
         throw ex;  
       }  
     }  

here is the error that you may get:
1) 0x81020030Invalid file name.\n\nThe file name you specified could not be used. It may be the name of an existing file or directory, or you may not have permission to access the file.
This is because FileRef must be included in the query for deleting the item.

2) You got success code (0x00000000) however the file just stays there in the Document Library.
This is because the FileRef path is using encoded URL
(eg. http://url/a/b/c/cc%20c.pdf -> must change to -> http://url/a/b/c/cc c.pdf)
After changing to non-encoded URL, wala.. it's working like magic!

Hope it helps!

Sharepoint WS References

Good reference websites:
http://sarangasl.blogspot.sg/2009/12/addupdate-list-item-using-sharepoint.html
http://sarangasl.blogspot.sg/2009/12/sharepoint-list-web-service.html
http://sarangasl.blogspot.sg/2009/10/caml-spquery-in-sharepoint.html

CAML Query Helper to help you create the query, can just download, it's quite easy to use.
http://spcamlqueryhelper.codeplex.com/

Sharepoint WS - Copy - Move the file from one server to another

Here's the method
faxObj is just an object if u wan to include for the field information purpose.
EDMSPath is the source
EDMSRPath is the destination

 private static bool MoveEDMSToEDMSR(ISDAFaxDBO faxObj, string EDMSPath, string EDMSRPath, string msg ="")  
     {  
       using (CopyWS.Copy copyService = new CopyWS.Copy())  
       {  
         copyService.Url = CopyURL;  
         copyService.Credentials = System.Net.CredentialCache.DefaultCredentials;  
         copyService.Credentials = new System.Net.NetworkCredential(username, password);  
         CopyWS.FieldInformation fiEDMS = new CopyWS.FieldInformation();  
         CopyWS.FieldInformation[] fi = { fiEDMS };  
         byte[] byteArr;  
         uint getFile = copyService.GetItem(EDMSPath, out fi, out byteArr);  
         string[] destination = { EDMSRPath };  
         CopyWS.CopyResult cResult1 = new CopyWS.CopyResult();  
         CopyWS.CopyResult cResult2 = new CopyWS.CopyResult();  
         CopyWS.CopyResult[] cResultArray = { cResult1, cResult2 };  
         CopyWS.FieldInformation fiRecordDate = new CopyWS.FieldInformation();  
         fiRecordDate.DisplayName = "Record Date";  
         fiRecordDate.Type = CopyWS.FieldType.DateTime;  
         fiRecordDate.Value = faxObj.MaturityDate.ToString("yyyy/MM/ddT00:00:00Z");  
         CopyWS.FieldInformation fiSector = new CopyWS.FieldInformation();  
         fiSector.DisplayName = "Sector";  
         fiSector.Type = CopyWS.FieldType.Choice;  
         fiSector.Value = "Fax";  
         CopyWS.FieldInformation[] fis = { fiRecordDate, fiSector };  
         uint copyresult = copyService.CopyIntoItems(EDMSPath, destination, fis, byteArr, out cResultArray);  
         //Array.Resize<CopyWS.FieldInformation>(ref fi, fi.Length + 2);  
         //fi[fi.Length - 2] = fiRecordDate;  
         //fi[fi.Length - 1] = fiSector;  
         //uint copyresult = copyService.CopyIntoItems(EDMSPath, destination, fi, byteArr, out cResultArray);  
         if (copyresult == 0)  
         {  
           int idx = 0;  
           foreach (CopyWS.CopyResult myCopyResult in cResultArray)  
           {  
             string opString = (idx + 1).ToString();  
             if (cResultArray[idx].ErrorMessage == null)  
             {  
               msg += "Copy operation " + opString +  
                 "completed.\r\n" + "Destination: " +  
                 cResultArray[idx].DestinationUrl;  
             }  
             else  
             {  
               msg += "Copy operation " + opString +  
                 " failed.\r\n" + "Error: " +  
                 cResultArray[idx].ErrorMessage + "\r\n" +  
                 "Code: " + cResultArray[idx].ErrorCode;  
             }  
             idx++;  
           }  
           return true;  
         }  
         else  
         {  
           return false;  
         }  
       }  
     }  

Sharepoint WS - Lists - Check if folder exists and create folders

Here's the code to check if the folder exists using Sharepoint Web Service
EDMSRPath is the full path of the file (eg. http://url/ListName/blabla/be/bo/x.pdf)
WSPath is the url of the document library (eg. http://url)
 char[] separator = ("/").ToCharArray();  
       string[] folder = EDMSRPath.Substring(EDMSRPath.IndexOf("ListName") + ("ListName").Length + 1).Split(separator);  
 //check first folder  
       // This method is separated to avoid getting the subfolders for all top folders, as it will have too much recurrence.  
       getFolders(string.Empty, ref dt);  
       string firstFolder = WSPath + "/ListName";  
       if (!checkFolderExist(dt, firstFolder + "/" + folder[0]))  
       {  
         CreateFolder(firstFolder, folder[0]);  
       }  
       firstFolder += "/" + folder[0];  
       //check the subfolders required  
       dt = new DataTable();  
       if (folder.Length > 1)  
       {  
         getFolders(folder[0], ref dt, true);  
         string subfolder = firstFolder;  
         for (int i = 1; i < folder.Length; i++)  
         {  
           if (!checkFolderExist(dt, subfolder + "/" + folder[i]))  
           {  
             CreateFolder(subfolder, folder[i]);  
           }  
           subfolder += "/" + folder[i];  
         }  
       }  
Now the actual functions
CheckFolderExist - to check if the folder exists

     private static bool checkFolderExist(DataTable dt, string folderpath)  
     {  
       if (dt.Rows.Count > 0)  
       {  
         for (int i = 0; i < dt.Rows.Count; i++)  
         {  
           if (dt.Rows[i][0].ToString() == folderpath || dt.Rows[i][0].ToString() == folderpath.Replace(" ","%20"))  
           {  
             return true;  
           }  
         }  
       }  
       return false;  
     }  
And function to create the folder

     private static string CreateFolder(string path, string foldername)  
     {  
       string res = string.Empty;  
       try  
       {  
         using (ListWS.Lists listService = new ListWS.Lists())  
         {  
           listService.Url = ListURL;  
           listService.Credentials = System.Net.CredentialCache.DefaultCredentials;  
           listService.Credentials = new System.Net.NetworkCredential(username, password);  
           XmlDocument doc = new XmlDocument();  
           string xmlCommand = "<Batch OnError='Continue' RootFolder='" + path +  
               "'><Method ID='1' Cmd='New'><Field Name='ID'>New</Field><Field Name='FSObjType'>1</Field><Field Name='BaseName'>" +  
               foldername + "</Field></Method></Batch>";  
           doc.LoadXml(xmlCommand);  
           XmlNode batchNode = doc.SelectSingleNode("//Batch");  
           XmlNode node1 = listService.UpdateListItems("ListName", batchNode);  
           if ((node1 != null) && (node1.FirstChild.FirstChild.InnerText == FOLDER_EXISTS) || (node1.FirstChild.FirstChild.InnerText == SUCCESS))  
           {  
             // success  
             res = "";  
           }  
           else  
           {  
             res = "Create new folder failed for: " + foldername + ". Error Details: " + node1.InnerText;  
           }  
             
         }  
       }  
       catch (Exception ex)  
       {  
         if (ex.Message != "Column name 'ID' is defined for different mapping types.")  
           throw ex;  
       }  
       return res;  
     }  

Sharepoint WS - Copy - Save local file to EDMS

Here is the method to save the local file in your computer and put it to the document library, my list name is EDMS.

 private static bool saveFileInEDMS(ISDAFaxDBO faxObj, string localPath, string EDMSPath)  
     {  
       using (CopyWS.Copy copyService = new CopyWS.Copy())  
       {  
         copyService.Url = CopyURL;  
         copyService.Credentials = System.Net.CredentialCache.DefaultCredentials;  
         copyService.Credentials = new System.Net.NetworkCredential(username, password);  
         string[] destination = { EDMSPath };  
         CopyWS.CopyResult cResult1 = new CopyWS.CopyResult();  
         CopyWS.CopyResult cResult2 = new CopyWS.CopyResult();  
         CopyWS.CopyResult[] cResultArray = { cResult1, cResult2 };  
         CopyWS.FieldInformation fiRecordDate = new CopyWS.FieldInformation();  
         fiRecordDate.DisplayName = "Date";  
         fiRecordDate.Type = CopyWS.FieldType.DateTime;  
         fiRecordDate.Value = faxObj.MaturityDate.ToString("yyyy/MM/ddT00:00:00Z");  
         CopyWS.FieldInformation[] fis = { fiRecordDate };  
         FileStream strm = new FileStream(localPath, FileMode.Open, FileAccess.Read);  
         Byte[] fileContents = new Byte[strm.Length];  
         int ia = strm.Read(fileContents, 0, Convert.ToInt32(strm.Length));  
         strm.Close();  
         uint copyresult = copyService.CopyIntoItems(localPath, destination, fis, fileContents, out cResultArray);  
         if (copyresult == 0)  
         {  
           return true;  
         }  
         else  
         {  
           return false;  
         }  
       }  
     }  

Sharepoint WS - Lists - Get Document ID

Here is the function to get the document data and it will return the document ID in result string.

 private static string GetDocID(string docname)  
     {  
       try  
       {  
         string result="";  
         using (ListWS.Lists listService = new ListWS.Lists())  
         {  
           listService.Url = ListURL;  
           listService.Credentials = System.Net.CredentialCache.DefaultCredentials;  
           listService.Credentials = new System.Net.NetworkCredential(username, password);  
           XmlDocument doc = new XmlDocument();  
           XmlElement query = doc.CreateElement("Query");  
           XmlElement viewFields = doc.CreateElement("ViewFields");  
           XmlElement queryOptions = doc.CreateElement("QueryOptions");  
           //query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" />" +  
           //     "<Value Type=\"Counter\">0</Value></Gt></Where>";  
           //docname = "STAR.BE_CIS_B_My%20%20test%20123.pdf";  
           query.InnerXml = "<Where><Eq><FieldRef Name=\"LinkFilename\" />" +  
                "<Value Type=\"Text\">"+ docname.Replace("%20"," ") + "</Value></Eq></Where>";  
           //query.InnerXml = "<Where><BeginsWith><FieldRef Name=\"LinkFilename\" />" +  
           //     "<Value Type=\"Text\">S</Value></BeginsWith></Where>";  
           viewFields.InnerXml = "";  
           queryOptions.InnerXml = "<ViewAttributes Scope='Recursive'/>";  
           XmlNode nodes = listService.GetListItems("ListName", String.Empty, query, viewFields, string.Empty, queryOptions, null);  
           foreach (System.Xml.XmlNode node in nodes)  
           {  
             if (node.Name == "rs:data")  
             {  
               for (int i = 0; i < node.ChildNodes.Count; i++)  
               {  
                 if (node.ChildNodes[i].Name == "z:row")  
                 {  
                   for (int j = 0; j < node.ChildNodes[i].Attributes.Count; j++)  
                   {  
                     Console.WriteLine(j + "|" + node.ChildNodes[i].Attributes[j].Name + "|" + node.ChildNodes[i].Attributes[j].Value);  
                   }  
                   result = node.ChildNodes[i].Attributes["ows_ID"].Value;  
                 }  
               }  
             }  
           }  
         }  
         return result;  
       }  
       catch (Exception ex)  
       {  
         throw ex;  
       }  
     }  

Monday, July 8, 2013

CSS Vertical text

Try this:

 <style>.test {  
      writing-mode:tb-rl;  
      -webkit-transform:rotate(90deg);  
      -moz-transform:rotate(90deg);  
      -o-transform: rotate(90deg);  
 }  
 </style>  
 <p class="test">testing 12123123</p>  
Result:
testing 12123123
Reference here

Friday, June 14, 2013

SQL to get the tables and its row count

Below is the SQL we can use to get the tables and its row count.
CREATE TABLE #counts
(
    table_name varchar(255),
    row_count int
)

EXEC sp_MSForEachTable @command1='INSERT #counts (table_name, row_count) SELECT ''?'', COUNT(*) FROM ?'
SELECT table_name, row_count FROM #counts ORDER BY table_name, row_count DESC

drop table #counts



Alternatively, can use this SQL, it will give the same result:
DECLARE @sql nvarchar(MAX)

SELECT
    @sql = COALESCE(@sql + ' UNION ALL ', '') +
        'SELECT
            ''' + s.name + '.' + t.name + ''' AS ''Table'',
            COUNT(*) AS Count
            FROM ' + QUOTENAME(s.name) + '.' + QUOTENAME(t.name)
    FROM sys.schemas s
    INNER JOIN sys.tables t ON t.schema_id = s.schema_id
    ORDER BY
        s.name,
        t.name

EXEC(@sql)


Reference: here

Thursday, June 13, 2013

[System.Web.HttpException]: {"File does not exist."}

Did you encounter this error? Dunno where exactly the error is??
Same me too..

Resolution is check your css file.
There could be an image reference from the css file that is not valid anymore.
That's the case for me.
So after I remove the css that refer to the invalid file path, it didn't give such error anymore.

Reference: here

Tuesday, May 28, 2013

Sharepoint webservice - create and get list of subfolders

To create folder
            Dim webUrl as string = "http://localhost/sites/test1/"
            Dim listWebService As New ListService.Lists()
            listWebService.Url = webUrl + "/_vti_bin/lists.asmx"
            listWebService.Credentials = System.Net.CredentialCache.DefaultCredentials 'New System.Net.NetworkCredential("user", "pwd")
            Dim doc As New XmlDocument
            Dim xmlCommand As String
            Dim node1 As XmlNode
            xmlCommand = "<Batch OnError='Continue' RootFolder='" & webUrl & "/Main'><Method ID='1' Cmd='New'><Field Name='ID'>New</Field><Field Name='FSObjType'>1</Field><Field Name='BaseName'>" & foldername & "</Field></Method></Batch>"
            doc.LoadXml(xmlCommand)
            Dim batchNode As XmlNode = doc.SelectSingleNode("//Batch")
            node1 = listWebService.UpdateListItems("Main", batchNode)
ListService is the web service http://localhost/sites/test1/_vti_bin/lists.asmx added as reference.

If you want to add subfolder, just need to change the rootfolder on the xmlCommand.

To get the list of subfolders (this can be used to check if subfolder exists)
   Sub getSubFolders(parent As String, ByRef retTable As DataTable)
        Dim query As String = "<mylistitemrequest><Query><Where><Eq><FieldRef Name=""FSObjType"" /><Value Type=""Lookup"">1</Value></Eq></Where></Query><ViewFields><FieldRef Name=""EncodedAbsUrl""/><FieldRef Name=""ID"" /><FieldRef Name=""Title"" /></ViewFields><QueryOptions><Folder>" & parent & "</Folder></QueryOptions></mylistitemrequest>"
        Dim dt As DataTable = Nothing
        Console.WriteLine("Parent is " & parent)
        Using listProxy As ListService.Lists = New ListService.Lists()
            listProxy.Url = webUrl + "/_vti_bin/lists.asmx"
            listProxy.UseDefaultCredentials = True

            Dim doc As XmlDocument = New XmlDocument()
            doc.LoadXml(query)

            Dim queryNode As XmlNode = doc.SelectSingleNode("//Query")
            Dim viewNode As XmlNode = doc.SelectSingleNode("//ViewFields")
            Dim optionNode As XmlNode = doc.SelectSingleNode("//QueryOptions")

            Dim retNode As XmlNode = listProxy.GetListItems("Main", String.Empty, queryNode, viewNode, String.Empty, optionNode, Nothing)

            Dim ds As DataSet = New DataSet()
            Using sr As StringReader = New StringReader(retNode.OuterXml)
                ds.ReadXml(sr)
            End Using



            If Not IsNothing(ds.Tables("Row")) Then
                If ds.Tables("Row").Rows.Count > 0 Then
                    Dim folderUrls = From f In ds.Tables("Row").AsEnumerable() Select f("ows_EncodedAbsUrl")
                    dt = ds.Tables("Row").Copy()

                    For Each folderUrl As String In folderUrls
                        getSubFolders(folderUrl, dt)
                    Next

                    retTable.Merge(dt)
                End If
            End If

        End Using

    End Sub

Comment the getSubFolders(folderUrl, dt) if you do not want to drill down the subfolders.

Subfolders references: here and here

Tuesday, May 7, 2013

ODP.NET oracleexception with no error message :S

My oracle version is 11.2.0.2.

I tried to use ODAC 11.2.0.3 to connect to the database from my .net program.
And it threw this error ORA-1017: invalid username/password; logon denied when my password expires and I tried to change my password.

So I installed ODAC 11.2.0.2 and try to connect to the database in VB.Net however I got error OracleException with no error message -> Oracle.DataAccess.Client.OracleException: {""}

But when I code my program in C# it gave the error OracleException {"ORA-28001: the password has expired"}

Solution to this is to uninstall all the ODAC versions in my PC.
Then install ODAC 11.2.0.2 and now it should work fine.

Btw in order to change password when the password expired, u can use this method OpenWithNewPassword.

See my thread at Oracle forum here.

 

Monday, April 29, 2013

Configuration system failed to initialize

Got this error?

Check your web.config or app.config, make sure that comes after

So it should be like this:

<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <appSettings>
    <add key="ConfigXMLPath" value="D:\Config.xml" />
  </appSettings>
...
</configuration>

If your configSections does not come right after , it will throw such error.

Example of configuration that will throw the error:

<configuration>
  <appSettings>
    <add key="ConfigXMLPath" value="D:\Config.xml" />
  </appSettings>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
...
</configuration>

Thursday, April 18, 2013

Check SQL version from Query

Just type this

SELECT @@VERSION VersionInfo
GO

:D
source: here