Create SQL database from RhinoCommon SDK

I wrote a tiny code just to create an SQL database inside a Rhinocommon plugin.

It works quite fine with windows forms Application, but for the same code and the same process, it show null reference exception when it is a RhinoCommon SDK.
This line below give null reference exception.
I have attached the zip file as a small/tiny project in visual Studio 2010 c# dot net

String connectionString = null;
connectionString = ConfigurationManager.ConnectionStrings[“myconnectionn”].ConnectionString;

AppConfig file is as below

<?xml version="1.0" encoding="utf-8" ?> SQL_TEST_02.zip (1.1 MB)
1 Like

If you are getting a NullReferenceException, it is probably the call to ConfigurationManager.ConnectionStrings. Try directly embedding that string in your code to see if that works.

Dear Steve,
Thanks for the reply.
I tried in several ways. nothing works.

Is there any setting I need to do when it is Rhino plugin ?
Because it kind of work in different ways , when it is just a Windows Forms Application.
This string below shows syntax error, when I have put it directly.
“Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True”

or if I put it in this way below, still the plugin hang in Rhino. But it shows compile succeeded.

.\SQLEXPRESS;AttachDbFilename=“C:\Users\John\documents\visual studio 2010\Projects\SQL_TEST_02\SQL_TEST_02\Database1.mdf”;Integrated Security=True;User Instance=True

There is no difference when between a stand-alone application and a plug-in in this context. It is probably your connection string. I can’t remember exactly how to read those, but it looks like there are some paths involved in there. Like, what is DataDirectory.

So I tried full path, in that case Rhino Hang.
.\SQLEXPRESS;AttachDbFilename=“C:\Users\John\documents\visual studio
2010\Projects\SQL_TEST_02\SQL_TEST_02\Database1.mdf”;Integrated
Security=True;User Instance=True

DataDirectory is "C:\Users\John\documents\visual studio 2010\Projects\SQL_TEST_02\SQL_TEST_02\Database1.mdf

eventually the whole string is as below
.\SQLEXPRESS;AttachDbFilename=“C:\Users\John\documents\visual studio
2010\Projects\SQL_TEST_02\SQL_TEST_02\Database1.mdf”;Integrated
Security=True;User Instance=True

but it hang Rhino

I don’t know enough about SQL Server to give a good answer to why that would be happening. Sorry.

OK. :worried: That means it is not Rhino problem, SQL problem.

Does the debugger give you any information about what is going on?

No. just it stuck with a null reference exception.
Do I need to change any Configuration information for Rhino Common and SQL.
WebConfig or AppConfig settings I mean.

I don’t understand what that means. If you get a NullReferenceException, the debugger should be telling you where the problem is.

Are you running your plug-in in the debugger?

Yes. It stuck in this line below and give the null reference exception. I wrote it ebove.

connectionString = ConfigurationManager.ConnectionStrings[“myconnectionn”].ConnectionString;

it was stuck in this line.
this value is not retrieved.

Yes, I doubt that will work when run in a plug-in which is why I suggested directly embedding a connection string in your code instead of using ConfigurationManger.

This is the direct connection string I wrote above and it also did not work. because the string is not in right format.

Are you sure you are referencing everything you should in the plugin project? Imported everything? Right .NET versions and all? Initializing everything after declaring?

Maybe something from the forms application has fallen on the way to the plugin…

OK … I am looking into it. I guess some configuration difference exist. I am looking into it. Also can be some fault writing my strings syntax.

Another day of struggle beginning :worried:

it worked, when I put it like this :
double back slash instead of single back slash in connection string…

So problem is solved by putting direct string with double back slash where single back slash needed !

1 Like

Ah yes, the infamous backslash… Instead of using double backslash, you can create a so-called literal string in C# using the @ symbol:

String conn = @".\SQLEXPRESS;AttachDbFilename="C:\Users\John\documents\visual studio 
2010\Projects\SQL_TEST_02\SQL_TEST_02\Database1.mdf";Integrated 
Security=True;User Instance=True";

String conn2 = ".\\SQLEXPRESS;AttachDbFilename="C:\\Users\\John\\documents\\visual studio 
2010\\Projects\\SQL_TEST_02\\SQL_TEST_02\\Database1.mdf";Integrated 
Security=True;User Instance=True";

Using a literal string will prevent the backslash being used as escaped sequence. Both strings will be the same.

2 Likes

Hi tsiddikee,
working on something similar. would you mind sharing your final solution?