Wednesday, August 27, 2008

ADO.NET C# common error dealing with inserting NULL value

If you are using c# and you are trying to insert null value into a table by using a shorthand something like below

param.Add("@abc", strAbc == "" ? null : strAbc);

it will not work. And you will got this error such as below:
Parameterized Query '(@Type nvarchar(2),@GroupType nvarchar(1),@Keyword' expects parameter @Type, which was not supplied.

To solve this problem you need to do a trick something like this
param.Add("@abc", strAbc == "" ? (object)DBNull.Value : strAbc);


change from null to (object)DBNull.Value

However, in VB.NET you do not need to cast.

Wednesday, August 20, 2008

Query data using sql and return results as datatable. Super fast method for ADO.NET!

You need to an easy way to query data from the database and fill into the datatable. Here is quick, dirty and secured trick

The method:
public static DataTable GetTable(string sql, Dictionary parameters)
{
DataTable dt = new DataTable("DataTable");
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString))
{
myConnection.Open();
SqlCommand mySqlCommand = new SqlCommand(sql, myConnection);

if (parameters != null)
{
foreach (KeyValuePair p in parameters)
{
mySqlCommand.Parameters.AddWithValue(p.Key, p.Value);
}
}

using (SqlDataReader datareader = mySqlCommand.ExecuteReader())
{
dt.Load(datareader);
}
}
return dt;
}

The query:
Dictionary sqlParams = new Dictionary();
sqlParams.Add("@id", 123);
devReview = DataHelper.GetTable("select * from sometable where id = @id", sqlParams).DefaultView;


Sunday, August 3, 2008

Windows Vista Ultimate - bootmgr is compressed

Today i've tried to install the windows vista ultimate 64bit to build up a 64bit workstation so that i could test to program that runs 64bit. Guess what? A Mesaage shown to me after i finish my vista installation. It just

bootmgr is compressed

prompt a dummy message asking me to press ctrl+alt+del. This intruction does help because it just restarts my pc and appear the message next and over again.

Luckly, i found a solution to this problem. If you encounter this, please use the Vista DVD to boot up your pc and wait until you will see a option that says "Repair your computer" Just click on that.

Later you will see a button "Load Driver", click click on that and go to "My Computer". After all, just right click => Properties. And just make sure the drive is not compressed. Unchecked the "Compress drive to save more space" something like (counld't remember exactly). for just in case the drive is compressed. And you need to do it for each drive. This process take ages, which cause your pc seem to be hang for a long period of time but wait don't restart it until it finishes. It will return you to the main menu. Trust me!

Anyway, I am going to test out my new PC running AMD 5000+ 2.6Ghz dual core. Don't be surprise to see more updates on the tips to make a program that utilizing the multi core processor using PLINQ!