|
I have an app that connects to a db in the Data Directory for Website Project. I am able to connect to the database fine locally. However, after I uploaded my project to my hosting provider's server and try to process a form that does a sql insert, I receive the following error:
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure my be caused by the fact that under the default settings SQL server does not allow remote connections. (provider: Sql Network Interfaces, error 26 - Error Locating Server/Instance Specified.
My connection string to the datasource is as such in my webconfig file:
connectionString="DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|DefinaBackEnd.mdf;IntegratedSecurity=True;ConnectTimeout=30;User Instance=True"
and the call to the connecting string is called as such:
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefinaBackEndConnectionString"].ConnectionString);
conn.Open();
Can someone please assist with how to resolve this as this site just looks for a sql db file as opposed to an instance of sql server itself. Thanks to all in advance.
|
|
|
|
|
Hi, let's take this piece of code:
Engine newEngine = new Engine(200, "PS");
Engine oldEngine = null;
oldEngine = Car.Engine;
Car.Engine = newEngine;
...
Car.Engine = oldEngine;
If Engine is a class (reference type) , shouldn't oldEngine share the same value like newEngine ? Right now they have different values.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
No... Basically, the variables are pointers to objects (ie, addresses). Therefore, your code does the following:
Engine newEngine = new Engine(200, "PS");
Engine oldEngine = null;
oldEngine = Car.Engine;
Car.Engine = newEngine;
...
Car.Engine = oldEngine;
Therefore, newEngine IS NOT equal to oldEngine or Car.Engine, but the other two are equal. Hope this helps,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
Let's say I have an assembly whatever.dll. I have an app where I want to load that assembly dynamically at runtime (it implements an interface that I know about already) and all I have to go on is the filename. If I do this:
<br />
Assembly assembly = Assembly.LoadFrom(@"C:\somecrazypath\bin\Debug\whatever.dll");<br />
foreach (Type type in assembly.GetTypes())<br />
{<br />
if (type.GetInterface("IMyInterface") == null)<br />
{<br />
continue;<br />
}<br />
<br />
object ibaseObject = Activator.CreateInstance(type);<br />
LoadedModules.Add((IMyInterface)ibaseObject);<br />
}<br />
Then it works great. I can handle events from the object in whatever.dll, and I have a reference to it that works as expected. However, that doesn't allow whatever.dll to have it's own config file, which is also a requirement. So in doing some research, it seems that if you load an object in it's own appdomain, then it can have it's own app.config (read here). So that seemed perfect, but the problem is that I just can't get the code to load right. If I do this:
<br />
AppDomainSetup ads = new AppDomainSetup();<br />
ads.PrivateBinPath += @"C:\somecrazypath\bin\Debug\";<br />
ads.ApplicationBase = @"C:\somecrazypath\bin\Debug\";<br />
ads.ConfigurationFile = @"C:\somecrazypath\bin\Debug\whatever.dll.config";<br />
AppDomain ad = AppDomain.CreateDomain("Whatever", null, ads);<br />
byte[] _assembly = loadFile(@"C:\somecrazypath\bin\Debug\whatever.dll");<br />
ad.Load(_assembly);
I get a filenotfound exception stating that it could not find whatever.dll or one of its dependencies. For the purposes of testing, I made sure that whatever.dll does not have any dependencies (no references other than System, System.Configuration, System.Data, and System.Xml) and I had it inherit MarshalByRefObject instead of my base class to eliminate that as a possible problem. I'm really at a loss as to why it can't seem to find the file. From reading online, it seems that the problem is because it is in a different path than the currently running appdomain, but I would have expected that setting the privatebinpath and the applicationbase on the new appdomain should have fixed that. I did find another report of the same problem here but the answer is incorrect. Passing the full path\filename to createinstanceandunwrap doesn't really fix it.
Has anyone run into this problem before?
|
|
|
|
|
I did just check, and I can load the assembly to the new appdomain just fine if I also place a copy of it in the running application's applicationbase directory. This works even if I don't change the absolute path I use to load whatever.dll. So it seems that if the dll is available to the current appdomain, then I can instance it in a new appdomain as well, but not if it is in a different directory.
|
|
|
|
|
Hello there,
I have made a small console application in C# which I have built and deployed to my local machine, I call it using a batch file to retrieve back any error codes it happens to throw back. It works no worries on my local machine, but I have placed it on a network share I get a load of permissions errors. I have searched round now for days looking for an answer and mainly, I think got it down to a permissions problem. I have increased the security on the local intranet, my computer and every other zone but still I get errors.
I have tried signing the application using visual studio properties of the application but still no joy. I have tried making its own Runtime Security Policy but still no joy. I’m using .net 2 with likes the signed file but when I use the .net 1.1 configuration wizard to trust an application it says it has no strong name despite giving the application one in Visual Studio. I have even tried installing the certificate that visual studio generates on the remote machine but to no joy.
The error I get when I run the program on the remote machine I get this error if I take away all the signing and ClickOnce manifests signing and disable ClickOnce Security Settings is :
Unhandled Exception: System.Security.SecurityException: System.Security.Permissions.SecurityPermission at PDF_to_TIFF.Program.Main(String[] args) The type of the first permission that failed was: System.Security.Permissions.SecurityPermission The demand was for: <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode"/>
And the error I get if I have all the signing switched on is:
Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'PDF to TIFF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d010d2ef76c6f036' or one of its dependencies. Failed to grant minimum permission requests. (Exception from HRESULT: 0x80131417) File name: 'PDF to TIFF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d010d2ef76c6f036' ---> System.Security.Policy.PolicyException: Required permissions cannot be acquired. at System.Security.SecurityManager.ResolvePolicy(Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, PermissionSet& denied, Boolean checkExecutionPermission) at System.Security.SecurityManager.ResolvePolicy(Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, PermissionSet& denied, Int32& securitySpecialFlags, Boolean checkExecutionPermission)
Please Help, this has almost driven me to point of insanity
|
|
|
|
|
We have a .NET console app that runs on our Win2003 server and processes a lot of network traffic.
After several days of running, we've discovered the app eating up all the memory on the machine -- nearly 2GB of memory. It brings the machine to a crawl, we start getting OutOfMemoryExceptions, all fun stuff.
Here's the kicker: I wrote some code so that I could type "garbage collect" in the console, and it would programmatically force a garbage collection (it calls GC.Collect() ). I went to the console app this morning, see it throwing OutOfMemoryExceptions and eating 2GB of memory - I type "garbage collect" in the console, and the memory goes down to a reasonable size and the app magically starts working again, no OutOfMemoryExceptions.
Has anyone ever seen anything like this? Any ideas why the garbage collector isn't working?
|
|
|
|
|
Hi Judah,
I have no experience with Win2003 Server nor do I run programs that long. Anyway:
AFAIK there are only two ways to either run out or leak memory:
1. keeping references to managed objects that are no longer required;
2. not releasing unmanaged resources (i.e. forgetting to implement and/or call Dispose).
If your program runs out of memory, it must be violating 1 and/or 2. If it then recovers after
executing a particular piece of code, then it seems to me that piece of code is remedying 1, 2
or both. So I would suggest you inspect that piece to discover what is happening.
FWIW: If you want to implement some caching, make sure to use WeakReference objects.
BTW: 2GB in a couple of days isn't much, it is around 1MB/minute.
As an alternative idea, the gc probably uses some lower priority threads, so if your server is
heavily loaded, it just might fail to retrieve old generation objects in time. I suggest you
check its CPU load overall and in particular when the problems show.
Hope this helps.
|
|
|
|
|
Luc,
Thanks for the reply.
#1 doesn't seem right, since it wouldn't explain the garbage collection. If I was violating #1, for example, memory would still be high after I force a GC.
#2 doesn't seem right either, since forcing a garbage collection wouldn't magically free unmanaged resources, right? In other words, memory would still be high if I was forgetting to release unmanaged resources, since the unmanaged resources would never get freed, even if I force a GC.
What do you think?
|
|
|
|
|
Hi Judah,
if all it does is GC.Collect() then you are right of course, but since you did not show any
code my best guess was to assume something else also happens when you type "garbage collect"
on your console.
As for unmanaged resources, it is still not clear to me; my advice is to call Dispose() or Close()
[if it exists] as soon as on object is no longer needed; others claim a class with Dispose() also
has a finalizer that will take care of things, but I am not convinced of this.
As always: when the whole thing does not behave the way you expect it to, everything must be
suspected, al prior assumptions must be scrutinized.
|
|
|
|
|
Luc Pattyn wrote: if all it does is GC.Collect() then you are right of course
Yep, that's all it does.
Luc Pattyn wrote: As for unmanaged resources, it is still not clear to me; my advice is to call Dispose() or Close()
Yes, right. To the best of our knowledge, we're calling dispose everywhere possible. I guess I'm gonna have to do some profiling of the app over several weeks. Blech. Thanks anyways, Luc.
|
|
|
|
|
You might want to consider using PerfMon as well, and monitor the garbage collections.
|
|
|
|
|
You're welcome.
Good luck with the profiling, and don't hesitate to report your progress.
|
|
|
|
|
In MFC the Windows Message can be handled using ON_MESSAGE macro. How to do the same in C#?
|
|
|
|
|
I think you may want WndProc (or to override it anyway):
protected override void WndProc(Message m)
{
base.WndProc(ref m);
}
I could be wrong, i'm uncertain of what ON_MESSAGE is.
My current favourite word is: Bauble!
-SK Genius
|
|
|
|
|
Yes this what I wanted. I am surprised it's so simple. Thank you!
|
|
|
|
|
Hi
the below code is throwing up this error, Im stumped any ideas?thanks
string maxquery3 = "Select max(activity_order) from roomactivitylk where room_code = '" + room_code + "'";
//declare variable max
int max;
//sets max as the result from the sql command
SqlCommand cmd15 = new SqlCommand();
cmd15.Connection = con2;
cmd15.CommandText = maxquery3;
max = Convert.ToInt32(cmd15.ExecuteScalar());
//adds 1 to the value max
max = max;
++max;
|
|
|
|
|
My guess is the object in one of the fields is Null, and therefor cannot be made into an Int32?
Also, why the max = max; ++max; why not just max++; or ++max; ?
My current favourite word is: Bauble!
-SK Genius
|
|
|
|
|
If the room code doesn't have anything associated with it, the select max would return null (technically it returns DBNull, but at the database end it's null). What you need to do is save the value to an intermediate variable and then convert it after testing for DBNull.Value.
object value = cmd15.ExecuteScalar();
if (value != DBNull.Value)
{
max = Convert.ToInt32(value);
}
max++; This is done off the top of my head so the syntax may be slightly wrong, but you should get the idea from it. More importantly though, don't use direct SQL like this. You leave yourself wide open to a SQL Injection Attack. Consider using parameterized queries instead.
|
|
|
|
|
Hi,
I'm trying to communicate with an unmanaged c++ dll from c# that takes two 2d
float arrays, one as input with data in it and the other will have the
result of the operation in it as follows:
bool Evaluate(float** input,float** output,int nFrames, int
featureWidth);
note: nFrames and featureWidth will determine the sizes of input and
output arrays in the c++ code.
Using this method in c# requires you to use float multidimensional
arrays. However I keep on getting exceptions thrown by the dll while
the same code works fine in a normal console application!
Trying to debug the dll as much as i can i figured out that the arrays
are not being passed correctly. can anybody help me out here, how
would I pass a 2 dimentional array to unmanaged c++ function?
Thanks
Fahd
|
|
|
|
|
Hi
how did you define the Call in c# (any MarshalAs usage or something..?)
what kind of exceptions do you get? access violations or others..?
f4hd wrote: dll while
the same code works fine in a normal console application!
do you mean you have a c# console application that successfully handles the dll?
greets
m@u
|
|
|
|
|
Hi M@u
thanks for the reply.
here is my definition
[DllImportAttribute(@".\xyz_MFC.dll")]
public static extern bool Evaluate(float [,] input, float[,] output, int nFrames,int featureWidth);
in unmanaged c++ it is
bool xyz_MFCApp::Evaluate(float** input, float **output,int nFrames,int featureWidth);
The exception that I get is System.NullReferenceException. Which is access violation I assume.
>>do you mean you have a c# console application that successfully handles the dll?
I have a legacy c++ code that im making into a dll in order to use it with c#
wen accessed using a c++ console app the code works fine. but wen i convert the code into a dll and use the exposed function in c# it throws the exception i just mentioned. on further investigating i figured out its the problem with the passing of 2d float arrays.
so if i pass the input array and access the first row i.e input[0][0] in the dll its fine but when i access input[1][0] the exception is thrown!
Thus i think i need to find out the correct way to pass the 2d array such that the memory it holds isnt affected.
hope I havent confused things further
Fahd
|
|
|
|
|
f4hd wrote: The exception that I get is System.NullReferenceException. Which is access violation I assume
No the Null Reference exception only says that you're passing a parameter that is null and is not supposed to be null
f4hd wrote: [DllImportAttribute(@".\xyz_MFC.dll")]
public static extern bool Evaluate(float [,] input, float[,] output, int nFrames,int featureWidth);
that with the multi-dimensional arrays might be a big problem.. i read something about it once and i think the central statement there was, that passing multi-dimensional arrays is not supported / only possible by using dirty tricks..
what you could try would be to mark the output array as ref or out.
[DllImportAttribute(@".\xyz_MFC.dll")]
public static extern bool Evaluate(float [,] input, [In,Out] ref float[,] output, int nFrames, int featureWidth);
or
[DllImportAttribute(@".\xyz_MFC.dll")]
public static extern bool Evaluate(float [,] input, [Out] out float[,] output, int nFrames, int featureWidth);
i find it a bit strange that nFrames is not a pointer, do you decide how many entrys per dimension the dll will return?
|
|
|
|
|
>No the Null Reference exception only says that you're passing a parameter that is null and is not >supposed to be null
well data is passed but incorrectly. see I can access the first row of the 2d array but not any other.
mebe you are right its not supported so one way, not quite elegant, is to convert the 2d arrays into 1d arrays during passing and receiving... Can there be any better way I wonder?
the In and out dont help much because of the same issue i mentioned above.
>>i find it a bit strange that nFrames is not a pointer, do you decide how many entrys per dimension >>the dll will return?
Yes I know it from before and is fixed for this Dll call and using it I do know the dimensions being returned
|
|
|
|
|
f4hd wrote: not quite elegant, is to convert the 2d arrays into 1d arrays during passing and receiving... Can there be any better way I wonder?
well if you can make it work that way i think that's a good way. an other way you could try would be to pass an array of intptrs that point each to an array of float.
it's ugly and i don't really know if it'll work at all but it *might* work
that it would look like this:
DllImportAttribute(@".\xyz_MFC.dll")]
public static extern bool Evaluate(IntPtr [] input, IntPtr[] output, int nFrames,int featureWidth);
private float[,] foo()
{
int nFrames = 3;
int featurWidth = 5;
float[,] myArray = new float[2,nFrames];
IntPtr[] input = writeArray(myArray);
IntPtr[] output = allocArray(featurWidth);
bool ok = Evaluate(input, output, nFrames, featurWidth);
freeArray(input);
return readArray(output,featurWidth);
}
private float[,] readArray(IntPtr[] Items, int featureWidth)
{
float[,] RetVal = new float[2,featureWidth];
readMemArray(Items[0],RetVal,0,featureWidth);
readMemArray(Items[1],RetVal,1,featureWidth)
freeArray(Items);
return RetVal;
}
private void readMemArray(IntPtr Pointer, Float[,] Target, int Dimension, int featureWidth)
{
float[] Dummy = new float[featureWidth];
Marshal.Copy(Pointer,Dummy,0,featureWidth);
copyArray(Dummy, Target, Dimension);
}
private void freeArray(IntPtr[] Items)
{
Marshal.FreeHGlobal(Items[0]);
Marshal.FreeHGlobal(Items[1]);
}
private IntPtr[] allocArray(int ItemCount)
{
IntPtr RetVal = new IntPtr[2];
int Count = Marshal.SizeOf(typeof(float)) * ItemCount;
RetVal[0] = Marshal.AllocHGlobal(Count);
RetVal[1] = Marshal.AllocHGlobal(Count);
return RetVal;
}
private IntPtr[] writeArray(float[,] Array)
{
IntPtr[] RetVal = allocArray(Array.GetLength(1));
float[] Dummy = new float[Array.GetLength(1)];
copyArray(Array, Dummy, 0);
Marshal.Copy(Dummy,0,RetVal[0],Dummy.Length);
copyArray(Array, Dummy, 1);
Marshal.Copy(Dummy,0,RetVal[1],Dummy.Length);
}
private void copyArray(float[] Source, float[,] Destination, int zDimIndex)
{
for(int i = 0; i< Source.Length; i++)
{
Destination[zDimIndex,i] = Source[i];
}
}
private void copyArray(float[,] Source, float[] Destination, int zDimIndex)
{
for(int i = 0; i < Destination.Length; i++)
{
Destination[i] = Source[zDimIndex,i];
}
}
the code is untested so maybe you'll have to change something but generally it could work.
however.. if you can make it work with an 1-dimensional array do it that way
greets
m@u
|
|
|
|
|