|
You have closed your connection in the body of the try block, and then you attempt to close it again in the finally block. You only need to do this once - the simple fix is to remove the Dispose /Close pair from inside the try part.
|
|
|
|
|
The way I see it, he actually has 2 different connections in there an MS-SQL and a MySQL one, and in the try he closes the MySQL one, not the MS-SQL one. I had posted the same answer immediately after you, but deleted it. Then I took another look, and spotted it
EDIT: I did upvote your solution before that though, and I'll leave it as is
Full-fledged Java/.NET lover, full-fledged PHP hater.
Full-fledged Google/Microsoft lover, full-fledged Apple hater.
Full-fledged Skype lover, full-fledged YM hater.
|
|
|
|
|
What Andrei says is true, I had thought what Pete said (closing the connection twice) but then I saw they're different connections.
I think I've seen this problem when the command's connection is not closed right away after use; since you already have the data in the reader, maybe you could close the SQLConnection before the while loop and see if it helps?.
And just my point of view: the catch block should either give an informative message of the problem (or at least just log the message somewhere) or it should be done away with.
|
|
|
|
|
Hi guys,
no he's right
sorry..
i missed it..
the connection was already closed here:
mark_migrated(Convert.ToInt32(MSSQLreader["file_no"]));
this is the code which closes the connection:
private void mark_migrated(int file_no)
{
MSSQLconnection_Migrated = new SqlConnection(ConfigurationManager.ConnectionStrings["MSSQL"].ConnectionString);
MSSQLconnection_Migrated.Open();
MSSQLcommand_Migrated = new SqlCommand("update persons set is_migrated = 1 where file_no = " + file_no, MSSQLconnection_Migrated);
int MSSQLreader_Migrated = MSSQLcommand_Migrated.ExecuteNonQuery();
MSSQLcommand_Migrated.Dispose();
MSSQLconnection_Migrated.Close();
}
I will try checking connection state before closing and revert back if not zsolved.
thanks guys
|
|
|
|
|
jrahma wrote: MSSQLconnection_Migrated = new SqlConnection(ConfigurationManager.ConnectionStrings["MSSQL"].ConnectionString);
Isn't that a whole different connection though?
|
|
|
|
|
oh sorry!
you are right..
then where is the problem?!!
|
|
|
|
|
Scoping issues. Try moving your MSSQL connection before the try - catch block. Also, I've only now noticed it. Just out of curiosity: does your code even compile?
Full-fledged Java/.NET lover, full-fledged PHP hater.
Full-fledged Google/Microsoft lover, full-fledged Apple hater.
Full-fledged Skype lover, full-fledged YM hater.
|
|
|
|
|
I don't think that's it either.
|
|
|
|
|
I dunno.
Does your mark_migrated try to use the same Connection reference that the above code uses? Or does it replace it?
Unrelated to the error:
Please don't keep instantiating and disposing the Connection, Command, and Parameters within the while loop.
0) Instantiate the Connection
1) Instantiate the Command
2) Instantiate and Add the Parmeters
3) while (MSSQLreader.Read())
3.1) Assign the Parameter values
3.2) ExecuteNonQuery
4) Close and Dispose
|
|
|
|
|
In general a bad idea all around.
If an exception occurs you do NOT know why it occurred.
It is possible that almost anything afer that point will cause another exception.
Thus your code should be structured like this.
try
{
...
command.Dispose();
connect.Close();
}
catch(Exception e)
{
try
{
command.Dispose();
}
catch(Exception)
{
}
try
{
connection.Close();
}
catch(Exception)
{
}
}
|
|
|
|
|
Now that it's been pointed out that they are different connections (I completely missed that), I would refactor this. Rather than the pattern you are using here, I would look to use the using command. Also, if you pass in CommandBehavior.CloseConnection on your call to ExecuteDataReader , you will automatically close the ocnnection when you finish iterating over the data reader.
|
|
|
|
|
but how can I use the CommandBehavior.CloseConnection with ExecuteNonQuery()?
|
|
|
|
|
Well, you are using ExecuteReader in your code, not ExecuteNonQuery. ExecuteReader can take CommandBehavior.CloseConnection as a parameter
|
|
|
|
|
I have a standalone HTML page used for data entry by a third party application. The form is opened in an IE window directly from the directory that it is stored. What I need to do is send information from a C# Class Library that is being called by a different system and send the information to this html page. ideas?
basically I want to do something like have javascript poll a tcp port until I send something but not sure how that works.
Also considered writing a C# COM object and using the object tag to make that part of the page and do all communication through C# but not sure if this is the best route.
|
|
|
|
|
How about ASP.NET? Seems to me it was created for this kind of stuff.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
A non C# solution - you might want to try and use Comet notifications[^].
|
|
|
|
|
Hi all, I am trying to create a method that allows users to convert a Word document to other kinds of files like PDFs, text files, etc., and that means using Word library and Word components. These need the Office Interop Word Assembly to work and I heard that hosting companies won't install it for you because of the licensing fee and they won't let you install it on their web server unless you sign up for a dedicated server. Are there open source alternatives out there that I can use? Thanks in advance.
modified 17-Sep-12 11:17am.
|
|
|
|
|
Yes, you can use the OpenOffice API for .NET to manipulate Office docs programmatically. I've used it before, and it works.
However, if you do decide to use it, be warned: it's not easy, and it's very poorly documented, not to mention intuitive. But if you do get it working, it's a wonder.
Good luck!
Full-fledged Java/.NET lover, full-fledged PHP hater.
Full-fledged Google/Microsoft lover, full-fledged Apple hater.
Full-fledged Skype lover, full-fledged YM hater.
|
|
|
|
|
Sounds like something that would be well worth reading as an article.
|
|
|
|
|
I would actually post it, if I'd remember how to do it. I may be able to dig the source though. Nice idea though, I was actually thinking about an article on automating some stuff, although not Office files. Thanks for the idea!
Full-fledged Java/.NET lover, full-fledged PHP hater.
Full-fledged Google/Microsoft lover, full-fledged Apple hater.
Full-fledged Skype lover, full-fledged YM hater.
|
|
|
|
|
Hello friends
I am practicing C# by solving the Problems in projecteuler.net. Now I am working on 20th one.
I create a project and here it is my code:
const int number = 100;
double fact = Library.Math.Fact(number);
BigInteger bigInteger = new BigInteger(fact);
int sum = 0;
while (bigInteger > 0)
{
sum += (int)(bigInteger % 10);
bigInteger /= 10;
}
Console.WriteLine("Sum of the digits int the number {0}! is {1}", number, sum);
static double Fact(int number)
{
return number == 1 ? 1 : number * Fact(number - 1);
}
It is absolutely fine, isn't it?
I have tried the small number that I can calculate manually and the result given was correct. But when I give to the site the rsult of my code it doesn't accept the answer.
What's wrong with my code?
modified 18-Sep-12 9:40am.
|
|
|
|
|
Meysam Tolouee wrote: It is absolutely fine, isn't it?
Depends on how you define "fine".
Well first, your recursive alogorithms could be an iterative one, that would be even finer.
Next: for large number you round, because the precision of double may be not good enough. So there are rounding error for large numbers.
By the way, I didn't downvote you and I wonder why someone did.
------------------------------
Author of Primary ROleplaying SysTem
How do I take my coffee? Black as midnight on a moonless night.
War doesn't determine who's right. War determines who's left.
|
|
|
|
|
I just changed the output of method Fact() from double to BigInteger.
Now it really works fine.
Fine means it gives me true result.
|
|
|
|
|
Meysam Tolouee wrote: Now it really works fine.
Fine means it gives me true result.
Ok, if it's fine for you, it's fine for me, too
------------------------------
Author of Primary ROleplaying SysTem
How do I take my coffee? Black as midnight on a moonless night.
War doesn't determine who's right. War determines who's left.
|
|
|
|
|
ihoecken wrote: By the way, I didn't downvote you and I wonder why someone did.
I've just counter-voted, but I have a suspicion as to why he was univoted.
|
|
|
|