Saturday, December 15, 2012

System.BadImageFormatException

I got following error when running Test project with visual studio 2010.

SetUp : System.BadImageFormatException : Could not load file or assembly 'TP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Solution:

When I checked there were difference in Solution platforms of two project.One set to x86 while other project set to AnyCPU.So I changed it from AnyCPU to x86.It works.

Wednesday, July 18, 2012

C# Password encrypting and decrypting

public static string HashPassword(string password)
{
    var provider = new SHA1CryptoServiceProvider();
    var encoding = new UnicodeEncoding();
    var hash= provider.ComputeHash(encoding.GetBytes(password));
    return Convert.ToBase64String(hash);
}

var pwdstr=HashPassword(password);
var user=(from u in context.Users
          where u.UserName.Equals(username) && u.pwd.Equals(pwdstr)  
          select u);

This is simple way to set and check password stored in database.Hope it will helpful.To store in a database use first method and to compare use second method.Until next post cheers!!!

Tuesday, April 10, 2012

MSSQL useful tips

MSSQL set null when value is not numeric in select query

SELECT CASE ISNUMERIC(column1) WHEN 1 THEN column1 ELSE NULL END FROM table1

SQL Server Error Messages - Msg 8152 - String or binary data would be truncated.  The statement has been terminated.

This error message is occurred when I tried to insert data to values from another table.It’s due to one column in the table that im inserting data have lesser length than other table column.(varchar(25) – varchar(max)).It couldn’t found previously because second table column I have used max length but when inserting data it filled with strings that contains more than 25 characters.

I found another command to skip that error and continue but its like a try catch statement without catch part(thanks yasindu bro for nice example).

SET ANSI_WARNINGS OFF

Need to use above command before inserting records.but sometimes it’s not working

Inserting multiple records to a table

This isn’t simple thing but I previously work with MySQL and that is different and we can’t use

INSERT INTO tblName (column1,column2)
VALUES
('Value1','value11'),
('Value2','value22'),
('Value3','value33')
INSERT INTO tblName (column1,column2)
select ‘Value1′,’Value11’
UNION ALL
select ‘Value2′,’Value22’
UNION ALL
select ‘Value3′,’Value33’

Monday, March 5, 2012

Starting Open Xml for Microsoft Office

Previously I have work with Aspose slides and now I’m trying to work with this one.First need to download OpenXml SDK for that used following link.This link also contains Open Xml tools setup that includes help documents etc..  image

Download Open Xml SDK 2.0 

Starting Steps

  1. Install OpenXml SDK (and tools )
  2. Open Visual Studio (currently I’m using visual studio 2010 with c#)  create new windows form project
  3. Reference DocumentFormat.OpenXml dll which is in installed path (Projects->Add Reference->browse to dll)
  4. Use following name spaces image
  5. to create new presentation image
  6. to open exist presentation image

Errors Occurred :

The type 'System.IO.Packaging.Package' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

this error can be solved adding reference to windowsBase in .Net Tab.

Thursday, January 5, 2012

How to get Twitter ID

Before I start I wish you all a very happy new year 2012.It took long time after my last since I got new job and many changes happened in recent months.

I was learning WPF(Windows Presentation Foundation) and I tried to create RSS Feed reader.I tried to get twitter feeds from my twitter account and I searched in google and I found a code but it needed twitter id for that.I found it can be also replace by username.But this code is also useful.

I open http://api.twitter.com/users/show/rasikalb.json {this is for me common url http://api.twitter.com/users/show/[replace username].json}url and it shows some useful json code that we can get many twitter account details.last line contains  twitter id.

image

We can access our twitter feed from http://twitter.com/statuses/user_timeline/[userId or username].rss it shows as follows.Using this rss file we can read tweets.

image

Hope to post about SQL, C# useful tips in future.Until next post cheers ..!!!