|
mauricemcse wrote: create an API for a project. My first question would be: why do you believe you need to create an API? Have you actually analysed the problem you are trying to solve and determined that this is the optimum answer, or do you just think it 'sounds right'?
As to learning, you don't mention what language(s) you plan to use, but since you posted this in the C# forum I would suggest .NET Book Zero[^] as an excellent primer.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
In a C# 2008 desktiop application, I have a line of code that fails a try catch block.
The line of code is:
System.IO.FileStream fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
The wholse section of code looks like:
public Attachment AttIputDoc(String custId, String fileName)
{
Attachment att = new Attachment();
byte[] buffer = null;
try
System.IO.FileStream fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
catch (Exception e)
{
}
The line of code is trying to access a file that does not exist in a specific location.
Can you tell me how the code listed above knows where the directory path is for the file that needs to get uploaded at? The place it thinks the file needs to be loaded from is where the source code of where the application is acutally running from.
How would I override the default location so I can obtain the value from the app.config file?
|
|
|
|
|
This supposes that you have stored the value in your appSettings in a key called Path.
string path = ConfigurationManager.AppSettings["path"];
string fullPath = System.IO.Path.Combine(path, fileName); Then, use fullPath in place of fileName when you create the FileStream.
|
|
|
|
|
I obtained the code from a contract shop. You code has been setup in the app.config file yet.
|
|
|
|
|
If this method is sealed to you, then you must pass in the fully qualified file name. The code I gave you will still do.
This is the problem with contract shops; they don't always give you good code. They'll give you exactly, and only, what you ask for.
|
|
|
|
|
But even when it works the reference will immediately go out of scope.
|
|
|
|
|
Hi,
I am getting the above error on the last line:
MSSQLconnection.Close();
could you please help..
here is the code:
try
{
MSSQLconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MSSQL"].ConnectionString);
MSSQLconnection.Open();
MSSQLcommand = new SqlCommand("select * from persons where is_migrated = 0 order by file_no", MSSQLconnection);
MSSQLreader = MSSQLcommand.ExecuteReader();
while (MSSQLreader.Read())
{
lblFileNo.Text = "File# " + MSSQLreader["file_no"].ToString();
MySQL_connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["mySQL"].ConnectionString);
MySQL_connection.Open();
MySQL_command = new MySqlCommand("insert into patients (current_file_no, id_type, id_number, date_of_birth, full_name, nationality, gender, old_file_no, marital_status, blood_group, patient_is_vip, is_outside_country, address_outside_address, address_country, address_flat, address_building, address_road, address_avenue, address_block, address_po_box, contact_mobile, contact_home_tel, personal_email, created_date) values (param_current_file_no, param_id_type, param_id_number, param_date_of_birth, param_full_name, param_nationality, param_gender, param_old_file_no, param_marital_status, param_blood_group, param_patient_is_vip, param_is_outside_country, param_address_outside_address, param_address_country, param_address_flat, param_address_building, param_address_road, param_address_avenue, param_address_block, param_address_po_box, param_contact_mobile, param_contact_home_tel, param_personal_email, param_created_date)", MySQL_connection);
MySQL_command.Parameters.Add("param_current_file_no", MySqlDbType.Int32).Value = Convert.ToInt32(MSSQLreader["file_no"]);
MySQL_command.Parameters.Add("param_id_type", MySqlDbType.VarChar, 255).Value = MSSQLreader["id_type"].ToString();
MySQL_command.Parameters.Add("param_id_number", MySqlDbType.VarChar, 255).Value = MSSQLreader["id_number"].ToString();
MySQL_command.Parameters.Add("param_date_of_birth", MySqlDbType.VarChar, 255).Value = MSSQLreader["date_of_birth"];
MySQL_command.Parameters.Add("param_full_name", MySqlDbType.VarChar, 255).Value = MSSQLreader["person_name"].ToString();
MySQL_command.Parameters.Add("param_nationality", MySqlDbType.VarChar, 255).Value = MSSQLreader["nationality"].ToString();
MySQL_command.Parameters.Add("param_gender", MySqlDbType.VarChar, 255).Value = MSSQLreader["gender"].ToString();
MySQL_command.Parameters.Add("param_old_file_no", MySqlDbType.VarChar, 255).Value = MSSQLreader["old_file_no"].ToString();
MySQL_command.Parameters.Add("param_marital_status", MySqlDbType.VarChar, 255).Value = MSSQLreader["marital_status"].ToString();
MySQL_command.Parameters.Add("param_blood_group", MySqlDbType.VarChar, 255).Value = MSSQLreader["blood_group"].ToString();
MySQL_command.Parameters.Add("param_patient_is_vip", MySqlDbType.VarChar, 255).Value = MSSQLreader["person_is_vip"].ToString();
MySQL_command.Parameters.Add("param_is_outside_country", MySqlDbType.VarChar, 255).Value = MSSQLreader["is_outside_country"].ToString();
MySQL_command.Parameters.Add("param_address_outside_address", MySqlDbType.VarChar, 255).Value = MSSQLreader["address_outside_address"];
MySQL_command.Parameters.Add("param_address_country", MySqlDbType.VarChar, 255).Value = MSSQLreader["address_country"].ToString();
MySQL_command.Parameters.Add("param_address_flat", MySqlDbType.VarChar, 255).Value = MSSQLreader["address_flat"];
MySQL_command.Parameters.Add("param_address_building", MySqlDbType.VarChar, 255).Value = MSSQLreader["address_building"];
MySQL_command.Parameters.Add("param_address_road", MySqlDbType.VarChar, 255).Value = MSSQLreader["address_road"];
MySQL_command.Parameters.Add("param_address_avenue", MySqlDbType.VarChar, 255).Value = MSSQLreader["address_avenue"];
MySQL_command.Parameters.Add("param_address_block", MySqlDbType.VarChar, 255).Value = MSSQLreader["address_block"];
MySQL_command.Parameters.Add("param_address_po_box", MySqlDbType.VarChar, 255).Value = MSSQLreader["address_po_box"];
MySQL_command.Parameters.Add("param_contact_mobile", MySqlDbType.VarChar, 255).Value = MSSQLreader["contact_moblle"];
MySQL_command.Parameters.Add("param_contact_home_tel", MySqlDbType.VarChar, 255).Value = MSSQLreader["contact_home_tel"];
MySQL_command.Parameters.Add("param_personal_email", MySqlDbType.VarChar, 255).Value = MSSQLreader["email_address"];
MySQL_command.Parameters.Add("param_created_date", MySqlDbType.Timestamp).Value = Convert.ToDateTime(MSSQLreader["created_date"]);
int MySQL_reader = MySQL_command.ExecuteNonQuery();
MySQL_command.Dispose();
MySQL_connection.Close();
mark_migrated(Convert.ToInt32(MSSQLreader["file_no"]));
}
}
catch (Exception exp)
{
return;
}
finally
{
MSSQLcommand.Dispose();
MSSQLreader.Close();
MSSQLconnection.Close();
}
|
|
|
|
|
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.
|
|
|
|
|