Thursday, July 27, 2006

Is HashCode Unique?

I found in many sites and artilce saying Hashcode is unique. But thats not true always.
just consider this example. As u know HashCode is int32, that means at a given point of time u can have max 2^32 different values. At that point of time u can have more than 2^32 different objects. That means hashcode must be duplicated.

There is another claim saying at least for string objects hascode is unique, that is also not true, the reason is. As we have 26 alphabets total no of differe string u can have is 26!(26 factorial) (I am just considering 26 char length single word only), which is bigger number than 2^32. That means two different string can have same hashcode is it not!.
Based on the above discussion we can say Hashcode is not unique.

*Please not that this is just my understanding, Correct me if i am wrong.

Tuesday, July 18, 2006

Sending a DataTable as parameter to Stored Procedure(SQL Sever 2000)

Quite often i faced a cese where i need to send a DataTable or array to the procedure as parameter. As we all know SQL 2000 takes paramteres as varchar,char,int,text etc. But there are occation where we required to send Full DataTable to DB. In those case we usually pump data to DB, record by record. Here is a work around for that. Lets Assume we have to send a table(see below) as parameter to Stored Procedure.

Person Discovery
EinsteinTheory of relativity
NewtonLaws of Cooling
Niels BhoreAtomic Model
Now convert the above table as string using a (C#)class.The resultant string looks like this
'''Person'',''Discovery'',~''Einstein'',''Theory of relativity'',~''Newton'',''Laws of Cooling'',~',~''Niels Bhore'',''Atomic Model'',~'
We can send this string to SP as a parameter directly. So the SP that take this string as input parameter looks as below.
Create Procedure usp_update_peopleTable
@inputTable varchar(8000)

But there is constraint as varchar can just hold(8000 chars) so we have to declare a variable type which can hold more data. So we have to use Text/Image instead of Varchar. so the SP looks as below.
Create Procedure usp_update_peopleTable
@inputTable text
I just want to repat the the stuff what we done still now.In App server we have a table that we need to send to DB. we converted the table as a string, now we sent the string to SP.
Now we have to chage input string to SP as Table again so that we can manipulate that as we require.So next task is to covert the String to Table in DB. Below SP address the same task. This SP takes 2 input parameters and results a Table, 1st param the strig that u have to convert to a table, 2nd param number of columns of the table. Teh SP is as below.

alter procedure usp_get_table_from_text
@inputtxt text,
@no_cols int
declare @charposition int,@row varchar(8000),@strsql varchar(8000)
create table #data_holder
rowid int,
textvalue text

set @strsql = 'create table output_table ('
while(@no_cols != 0)
set @strsql = @strsql+ ' col' + cast(@no_cols as varchar) + ' varchar(2000),'
set @no_cols = @no_cols -1
set @strsql = substring(@strsql,1,len(@strsql)-1)+ ')'


set @charposition = charindex('~',@inputtxt)
insert into #data_holder values(1,@inputtxt)

while(@charposition > 1)
select @row = substring(textvalue,1,@charposition-1) from #data_holder where rowid = 1

set @strsql = 'insert into output_table values(' + substring(@row,1,len(@row)-1) + ')'

update #data_holder set textvalue= substring(textvalue,@charposition+1,datalength(textvalue))

select @charposition = charindex('~',textvalue) from #data_holder where rowid = 1

drop table #data_holder
select * from output_table
drop table output_table

Just for understanding create SP in u r DB and execute below Query. SO that you can get some understanding what its doing.

exec usp_update_peopleTable '''Person'',''Discovery'',~''Einstein'',''Theory of relativity'',~''Newton'',''Laws of Cooling'',~',~''Niels Bhore'',''Atomic Model'',~',2

I am so sorry to say that there were no cooments in above SP. i'll add comments as and when i get soem time.
Steps to use the stuff
Objective: when vere u need to send a Table directly to DB folow these steps.
1. Covert the Table to String in App Server.
2. send teh converted string to your SP that consumes the Table.
3. In your SP use sp 'usp_get_table_from_text' to convert the string to table
4. Manipulate the table as you want.
Hope this might help you all.

External Tool for VS(Visual Studio)2003 to change the file attributes

Hi All: This Article describes adding your own tool to VS 2003
If you intigrate VS 2003 with VSS, as and when u get the latest version the respective file(s) become "Read Only". So when ever u want to edit the file, u have to check out the file, else if u want to make changes locally, u have to dig till that file using Win Explorer and have to uncheck the Read only attribute. Imagine if the project is having many subdirectories, then its bit difficult to change the read only attribute of the file(s) by navigating thru explorer.
If there is any way that address the above task, directly from VS IDE that will difinitely help us. Here is a way out. I just added a external tool, that appears under Tools menu.

U have to write a class that acceppts a file_path as argument. And u have to change teh file attributes thru code.Here i wrote a class that exactly do the same stuff.

using System;
using System.IO;
using System.Text;

namespace Surendra.ExternalTools.SetFileAttributes
public class ChangeFileAccessAttributes
public static void Main(string[] args)
string strFilePath = args[0].ToString();

File.SetAttributes(strFilePath, FileAttributes.Archive);

Creation of EXE:
Now u have to create an EXE for the above class. u can achieve teh same through CMD as below.

"C:\Surendra\PersonalProjects\ExternalTools>csc ChangeFileAccessAttributes.cs"

Integration of the tool to VS IDE:

step1: Open MS VS. Click on Tools Main Menu, then click on External tool..
A pop up will open for u.

Step 2:Click on Add button.
just fill out the text boxes as
Title: Remove Read Only
Command: Path of the ChangeFileAccessAttributes.exe
Arguements: "$(ItemPath)"
And finally press OK button..
Thats it.. your new tool will get ready and that appears under Tools menu as shown below.