0

I'm having hard times importing a C# Dll into another simple test application (console, winform, you name it).

Basically it consist in a huge library which consumes the European Community Vat checking service (which URL I believe is Vat webservice.

The service makes you download a library, then the implementation of the code is up to you.

What I did is starting a new C# class library in Visual studio 2010 and then adding this class which is automatically downloaded from the webservice for your own use, and then wrote this snippet I found in the web.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Xml; using System.IO; namespace EuroVAT { public class EuroVAT { public static Array check(string country, string vatNum) { bool valid; string name; string address; checkVatService vatchecker = new checkVatService(); vatchecker.checkVat(ref country, ref vatNum, out valid, out name, out address); return new object[] {country, vatNum, valid, name, address }; } } } 

Well, when I create a new Console application, then add a reference (right click on project name, add reference, browse to the dll) to my DLL compiled from a Class Library project which included the 2 classes I linked/quoted up here.

Here's some code I tried to use my dll:

using EuroVAT; namespace DLLTEST { class Program { static void Main(string[] args) { EuroVat test = new EuroVat(); } } } 

Basically I am not able to call them from the program. All I get when I try to call EuroVAT object is

Error 1 The type or namespace name 'EuroVAT' could not be found (are you missing a using directive or an assembly reference?)

I tried to build, close, reopen project. Tried to put it in Winforms and so on to no avail. I always get that error.

What am I doing wrong? What can I do to solve it?

One more question:

What exactly this code is doing?

 return new object[] {country, vatNum, valid, name, address }; 

It's like returning an array of objects? Because when I run the program all the output is thrown out all togheter. Usually I have to cycle throught the output with some sort of loop...

Last question, if someone can help:

I'll have to include this DLL in a SQL server (2008 I think)

I have read about the subject and discovered that for passing values to SQL Server from a C# dll you have to include various libraries and declare your methods as SqlProcedures, but also passing/retrieving your values as SqlTypes (SqlInt32, SqlString, etc etc).

Here's what I came up with:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Xml; using System.IO; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; namespace WebServiceVatEuropa { class Program { [SqlProcedure] public static Array check(string country, string vatNum) { bool valid; string name; string address; checkVatService vatchecker = new checkVatService(); vatchecker.checkVat(ref country, ref vatNum, out valid, out name, out address); return new object[] {country, vatNum, valid, name, address }; } } } 

I hope it's correct. What I'm missing is how I can cast all those object to SqlTypes like SqlInt32, SqlString and so on. I tried but I got tons of conversion errors and stuff like that. It just won't allow me to declare those objects ad SqlTypes and I can't work out the last to make the last return statement to output those SqlTypes.

Can anyone show an example if possible?

Thank you all.

1 Answer 1

2
  1. your class is declared as EuroVAT. Whereas you initialize object of type EuroVat. Check the register Other thing is, try giving your namespace and class different names

  2. This: 

    return new object[] {country, vatNum, valid, name, address }; 

    creates an array of object type, and populates it with (object)country, (object)vatNum, (object)valid, etc

  3. If I get it right how you intend your library to interact with SQL Server, the problem is with this array. Those SQL types, like SqlInt32, SqlString, SqlBinary, etc. do have implicit conversion operators for corresponding types, like int, string. But since array holds object instances, implicit conversion can not be applied. That is the one side. On the other side, SQL Server gets result of object[], and it cannot just handle it. It doesn't know how. I think you should consider using a data acess framework to handle the database, something like plain ADO.NET, or Entity Framework, or whatever. I actually cannot figure out your intentions with these stored procedures you provided.

EDIT:

Try passing result to SQL Server through SqlContext like this:

[SqlProcedure] public static void UserService(string country, stringvatNum) { object[] serverResponse = CheckService(country, vatNum); // Variables. SqlMetaData column1Info; SqlDataRecord record; // Create the column metadata. column1Info = new SqlMetaData("Column1", SqlDbType.Variant); // Create a new record with the column metadata. record = new SqlDataRecord(new SqlMetaData[]{column1Info}); // Mark the begining of the result-set. SqlContext.Pipe.SendResultsStart(record); foreach (var o in serverResponse) { record.SetValue(0, o); SqlContext.Pipe.SendResultsRow(record); } // Mark the end of the result-set. SqlContext.Pipe.SendResultsEnd(); } public static object[] CheckService(string country, string vatNum) { bool valid; string name; string address; checkVatService vatchecker = new checkVatService(); vatchecker.checkVat(ref country, ref vatNum, out valid, out name, out address); return new object[] {country, vatNum, valid, name, address }; } } 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer. Well I managed to use the DLL but it wasn't a uppercase/lowercase problem, but a compilation problem. Solved. Regarding the object array, I guess I had the right intuition. Thanks. The SqlTypes hints you gave me are still a bit obscure, so I will wait to mark your answers as solved because I would like to see someone else point of view about this particular subject. Thank you so much for the help.
Edit: I think I found a way to do the SqlTypes conversion doing like this: return new object[] {(SqlString) country, (SqlString)vatNum, (SqlBoolean) valid, (SqlString) name, (SqlString) address } ; But I didn't tested it.
i thing the problem is that method check() can not have return type other than void or int. link
i have updated my previous answer. i hope it will be helpful. But imho, calling web service from inside of sql server... it seems a little bit odd to me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.