Saturday, February 4, 2017

Pass an Variant Array from VB6 to VB.net

There is no variant type in vb.net.
If you have an Variant array declared in VB6 and you are trying to pass this to VB.net, you would need to pass variant as object .net and declare an array and loop thru each item from the passed object and add it to array.

Example:

My VB6 code:
 Dim variantArray As Variant
 variantArray = Empty  
 ReDim variantArray(1)
 variantArray(0) = "C:\pic1.jpg"
 variantArray(1) = "C:\pic2.jpg"

So now in VB.net, you can either pass this parameter to a function or a property of an object of .net class. See code below as property:
 Private _stringArray As String()  
 Public Property StringArray As Object
     Get  
         Return _stringArray  
     End Get  
     Set(value As Object)  
       If Not value Is Nothing Then  
           ReDim _stringArray(value.length - 1)  
           For i As Int16 = 0 To value.length - 1  
               _stringArray(i) = value(i)  
           Next  
       End If  
     End Set  
 End Property  

Monday, January 16, 2017

Return object from VB.net to VB6 (Object not defined)

Hi,

I was working on a project where there was a change needed in VB6 application.
This change was implemented in VB.net but there were few things involved in returning values from .net to VB6 which caused little challenge.
Without realizing, I thought that the class object I m creating in .net would be understood by VB6 but, I was wrong.

I was keep getting error when I was calling .net function in VB6 that was returning object in vb.net.
The error was "object not defined".

To fix this problem, what I did, I decomposed my single class into 2 classes; say ClassA and ClassB.
Then I created objects of each class in VB6 and then used properties to pass values from ClassA object to ClassB.

In the constructor of ClassA, I initialized the property which was ReadOnly property in my class.
Then I passed this property by assigning to ClassB object's property.

What I was doing wrong returning ClassB object from .net function to VB6 which wasn't working. So  I guess the correct way to create separate classes and objects and then pass values between them using properties.

HTH.

Thursday, December 29, 2016

Access SQL Server in ODBC in different domain

Hi,

Recently, in my company, we added a new domain with sql servers. Ofcourse, a trust was setup between existing and new domain.
There was a need to access these sql servers from prev domain.
The way you access it would be as:

<sqlservername>.domainName\instanceName

Example: If sqlserver name is: DevServer 
and domain is: company.internal.com 
and instance name is: PostTesting

Then I will access it as: devserver.company.internal.com\posttesting

For default instance, it would be just: devserver.company.internal.com

HTH

Tuesday, November 22, 2016

Encrypt / Decrypt password in Sql Server Database

I know this solution is not a recommended solution by many DBAs but it is available to use. Sometimes you have to get things going and implementing each step with detail can take weeks to finish but as a quick solution, this method has helped me to store password in the database with encryption and decryption methods.

I will not suggest this implementation but it is a fast way to store passwords in database.
CREATE TABLE LoginDetails (
 UserName VARCHAR(50)
      , [Password] VARBINARY(100)
)

INSERT INTO LoginDetails VALUES ('John', ENCRYPTBYPASSPHRASE('secretkey', 'johnPassword'))
INSERT INTO LoginDetails VALUES ('Shaw', ENCRYPTBYPASSPHRASE('secretkey', 'shawPassword'))
INSERT INTO LoginDetails VALUES ('Mike', ENCRYPTBYPASSPHRASE('secretkey', 'mikePassword'))

When I select from table, I see binary data in Password column


Now to read password, you need to decrypt it with the same key along convert function to read binary data as readable text.

SELECT username, CONVERT(VARCHAR(100), DECRYPTBYPASSPHRASE('secretkey', [password])) AS Password FROM LoginDetails

Here is the result

Thursday, November 10, 2016

How to find start date of month in SQL Server?


SELECT DATEADD(month, DATEDIFF(month, 0, @date), 0) AS StartOfMonth

0 in date functions means 1/1/1900.
The above function first find the difference of total months from 1/1/1900.
It then adds the difference in 0 (1/1/1900) again which will land to the beginning date of month.



Thursday, April 7, 2016

Why I dont see Network drives inside Open Dialog box?


If you like me that u didn't see network drives then you have to right place.
You need to visit the drive by typing the drive letter in the FileName field.
Once you visit, next time you open dialog box, you will see the drive listed in the dialog box.

HTH

Thursday, January 21, 2016

SET vs SELECT SQL Server

Many times we wonder how set and select are different and mostly we think that select is used to return data set back to user and set is used to set the value of variables.

This is exactly right but sometimes people also use to set variable value using select statement.

Lets try some statements and learn some behaviors as well:

-- create couple temp tables
create table #Test (BusinessDate datetime)
create table #TestNoRows (BusinessDate datetime)

-- insert null row in #test table
insert into #test values (null)

-- declare variable
declare @businessDate as datetime

select @businessDate = businessdate from #Test
select @businessDate

You will get Null.  A row was returned from table with Null. To make it more clear, we will now set some default value to variable so that variable has some value.

-- Now set some default value.
set @businessDate = '01/01/1970'

select @businessDate =  isnull(businessdate, '01/01/2000') from #Test
select @businessDate

You will get '01/01/2000'. Again a row was returned and isnull function was able to work on this value and replaced the default value.

Now we will test how select behave with empty table.

-- declare variable
declare @businessDateNoRow as datetime

select @businessDateNoRow = businessdate from #TestNoRows
select @businessDateNoRow

No row returned. You will get Null as default value of variable.

-- Now set some default value.
set @businessDateNoRow = '01/01/1970'
select @businessDateNoRow =  isnull(businessdate, '01/01/2000') from #TestNoRows
select @businessDateNoRow

You will get '01/01/1970' because no rows returned that would be checked for null or not null. So same default value stored in variable is returned.

Now we will test how set/select works.
--using set
set @businessDateNoRow = (select  isnull(businessdate, '01/01/2000') from #TestNoRows )
select @businessDateNoRow

You will get null.

--using select
select @businessDateNoRow = (select  isnull(businessdate, '01/01/2000') from #TestNoRows )
select @businessDateNoRow

You will get null too.

When no rows are returned and if set is used, you would write subquery to set value. Nothing to do with set or select.