|
yoavyoavyoav wrote: Is there a way to make my app behave differently in case Excel is not installed (for eg. detect the error and display a message ?)
It isn't an error you get, it is an Exeption. All you need to do is catch the appropriate exception.
|
|
|
|
|
You might be able to tell your app's installer that Excel is a prerequisite.
However I suggest you find a better solution than excel COM interop.
|
|
|
|
|
Thanks for your reply.
Unfortunatly , I Do want to provide a logic for my application so If excel is installed , it will do one thing , and if not - another thing (the application is a reporting tool that generates CSV files).
The only way I found to call to Excel from within my .Net app is by using the COM interop (It's my first time using COM interops). If excel is installed it works well , but if I move my application .exe to another computer and excel is not installed on it - Bam and the application crashes when I press the button that displays excel from within my app.
I've tried protecting the code block that calls the excel interops with try-catch , but it didn't work (the exception was not caught in my " Catch(Exception ex) " for some reason.
I do need a solution for this problem so I'll appreciate any reply.
(I've seen some stuff around the web with using the COM interop with late\early binding - anyone care to explain and will it solve my problem ?)
Thanks.
|
|
|
|
|
You certainly don't need Excel to produce CSV files; in fact it's probably better not to.
Just implement the "other thing" and be done with it.
|
|
|
|
|
Thanks again for replying.
I think I'm not well understood.
My application generates CSV reports (on it's own , not related to excel) , and then , in order to view the generated file I want to provide the functionality for the app to open up excel and display the generated file.
By using excel interop function like "autofit" and such , I can make the report appear nicely. - This way the end-user can also save it to xls format if he chooses to.
My other alternative is to just call System.Diagnostics.Process.Start(OutputCSV) file and let the user deal with it.
The problem in this instance - is that if for eg. Excel is associated with the CSV files , the files won't be opened well in excel and the user will need to reopen them from within excel (So a dialog will be opened up where he can specify that the file is Comma Delimited).
Is there a way perhaps to pass an argument to the excel.exe file that says that the file is CSV ? that will be extremely useful in my case.
My Last alternative is to display the CSV file in a built in datagrid in another form - though this is less functional. (It doesn't have the "power" of an excel worksheet").
Thanks.
|
|
|
|
|
Then use Process.Start to open the file, and the operating system will look for a file association for CSV files, if it finds Excel it'll use it, if it finds some other program that the user prefers, it'll use that.
|
|
|
|
|
OK - Perhaps I will settle with that.
But regardless - if I use try-catch on the Code block that calls the Excel interops - the exception that is triggered on a computer that doesn't have excel installed is not being caught.
Why is that ? (This also happens , if for eg. the DLL the application is using is not available. is there a way to detect this and display a message to the user ?
May Something like --> CanICreateObject(objectType) ?
Thanks
modified on Monday, January 14, 2008 10:56:57 AM
|
|
|
|
|
Hi!
From my experience, a failed object creation for COM objects that are not available can be caught just like any other exception.
How do you create your Excel object? I don't have the Excel PIA (and thus the namespaces and classes it provides) at hand, but something like this should work:
Microsoft.Office.Excel._Application app = null;
try
{
app = new Microsoft.Office.Excel._Application();
}
catch
{
}
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Sorry I am back so soon. For some reason when I save my database it tries to convert the Home Phone, Cell Phone, and Fax into System.Int32. I have them set on System.String.
" Failed to convert parameter value from String to a Int32. "
I have no code in there tring to convert it. Like I said in the database the textbox is set to System.String.
When I enter like: 123455 it works
When I enter like: 123-4567 it doesn't because of the dash.
|
|
|
|
|
Your post contains some confusing terminology so I am not sure what you mean by certain things.
Jacob Dixon wrote: For some reason when I save my database it tries to convert the Home Phone, Cell Phone, and Fax into System.Int32. I have them set on System.String.
By "save" did you mean "INSERT" or "UPDATE"?
Jacob Dixon wrote: Like I said in the database the textbox is set to System.String
The database knows nothing about textboxes. Nor does it use System.String. It uses char, varchar, nchar and nvarchar.
Could you post the relevant code? Both C# and SQL.
|
|
|
|
|
Sorry...
Under Data Sources I have a file called AddressesDataSet.Xsd, I right click it and go to Edit Dataset with Designer, which then shows a table of all my columns in the Addresses.mdf file (the database). Whhen I click a column in the table like HomePhone, CellPhone, or Fax, under the properties window it shows: Data Type: System.String. Here are all my "columns":
ID - primary key
FirstName
LastName
Address
City
State
Zip
Email
Picture
Notes
HomePhone
CellPhone
Fax
FirstName through State under the properties are set the Data Type to: System.String (which work and do not have any problems with)
Zip is set to System.Int32
Picture is set to System.Byte[]
The rest are System.String.
All of this I believe was automatically generated when I put the database in the project.
When I have a form open and I Drag and Drop the database to the form. I can choose to have all the columns displayed in DataGridView or all of the columns displayed on the form in TextBoxes, PictureBox, ComboBox, DateTimePicker, and more..
**wait**
I think I figured it out, I shouldn't be editing this in the DataSet. I should be doing this in the Addresses.mdf! Now I see where it is getting the INT and not varchar!
Ok now I feel stupid.. Thanks for the help though
|
|
|
|
|
I have one more question now that I got that fixed... in a foreach statement... I have a few If and else statements.. if you use return; that will exit the foreach loop won't it? For example:
if (DBNull.Value == r.Cells[7].Value) ; //if I use return; it will exit the foreachloop
else richTextBox1.Text += string.Format("Home Phone: {0}\n", r.Cells[7].Value.ToString());
Where the return; would be then it would go down here to the next If statement.
|
|
|
|
|
Jacob Dixon wrote: if you use return; that will exit the foreach loop won't it?
return exits the method. If you are in a foreach loop (or any type of loop) then that will, becuase it is inside the method, also end.
Jacob Dixon wrote: Where the return; would be then it would go down here to the next If statement
I don't understand what you mean by that. return exits the method. It won't progess to any more statements, including if statements, in the method.
|
|
|
|
|
Sorry... here I will paste the entire coding.. what I ended up doing was just leaving it blank and it seems to work....
foreach (DataGridViewRow r in theTableDataGridView.Rows)
{
richTextBox1.Text += string.Format("{0} {1}\n", r.Cells[1].Value.ToString(), r.Cells[2].Value.ToString());
if (DBNull.Value == r.Cells[3].Value);
else richTextBox1.Text += string.Format("{0}\n", r.Cells[3].Value.ToString());
if (DBNull.Value == r.Cells[4].Value && DBNull.Value == r.Cells[5].Value && DBNull.Value == r.Cells[6].Value);
else if (DBNull.Value == r.Cells[4].Value && DBNull.Value != r.Cells[5].Value && DBNull.Value != r.Cells[6].Value) richTextBox1.Text += string.Format("{0}, {1}\n", r.Cells[5].Value.ToString(), r.Cells[6].Value.ToString());
else if (DBNull.Value != r.Cells[4].Value && DBNull.Value == r.Cells[5].Value && DBNull.Value != r.Cells[6].Value) richTextBox1.Text += string.Format("{0}, {1}\n", r.Cells[4].Value.ToString(), r.Cells[6].Value.ToString());
else if (DBNull.Value != r.Cells[4].Value && DBNull.Value == r.Cells[5].Value && DBNull.Value == r.Cells[6].Value) richTextBox1.Text += string.Format("{0}\n", r.Cells[4].Value.ToString());
else if (DBNull.Value == r.Cells[4].Value && DBNull.Value != r.Cells[5].Value && DBNull.Value == r.Cells[6].Value) richTextBox1.Text += string.Format("{0}\n", r.Cells[5].Value.ToString());
else if (DBNull.Value == r.Cells[4].Value && DBNull.Value == r.Cells[5].Value && DBNull.Value != r.Cells[6].Value) richTextBox1.Text += string.Format("{0}\n", r.Cells[6].Value.ToString());
else richTextBox1.Text += string.Format("{0}, {1} {2}\n", r.Cells[4].Value.ToString(), r.Cells[5].Value.ToString(), r.Cells[6].Value.ToString());
if (DBNull.Value == r.Cells[7].Value);
else richTextBox1.Text += string.Format("Home Phone: {0}\n", r.Cells[7].Value.ToString());
if (DBNull.Value == r.Cells[8].Value);
else richTextBox1.Text += string.Format("Cell Phone: {0}\n", r.Cells[8].Value.ToString());
if (DBNull.Value == r.Cells[9].Value);
else richTextBox1.Text += string.Format("Fax Number: {0}\n", r.Cells[9].Value.ToString());
if (DBNull.Value == r.Cells[10].Value);
else richTextBox1.Text += string.Format("Email Address: {0}\n", r.Cells[10].Value.ToString());
if (DBNull.Value == r.Cells[12].Value);
else richTextBox1.Text += string.Format("Notes: {0}\n", r.Cells[12].Value.ToString());
richTextBox1.Text += "\n\n";
}
It appears to be work.. See I just left the IF statements without the RETURN. It seems to be working, I haven't ran into any problems yet..
Basically I just wanted to have it take the VALUE from the CELLS in a datagridview and display them in a certain format in a RichTextBox. Now If the cells were NULL then I did not want it to do anything.
Sorry to be so confusing, I'm trying to explain what I am trying to do.. Like I said it appears to be working and I thank you for trying to help me out.
-- Jacob Dixon
|
|
|
|
|
|
Yes! Thanks! As long as it skips the Else statement that is right after the IF statement!
|
|
|
|
|
Jacob Dixon wrote: Yes! Thanks! As long as it skips the Else statement that is right after the IF statement!
Umm... You do know how if statements work. Don't you?
if (some expression is true)
{
}
else
{
}
So, if you landed in the first block in the if statement, the code in the second block (after the else ) will not run.
Remember that if you have multiple statements you must enclose them in the braces. You can get away without the braces if you only have one statement.
|
|
|
|
|
Jacob Dixon wrote: As long as it skips the Else statement that is right after the IF statement!
I just re-read that if statement and it looks confusing
if (DBNull.Value == r.Cells[3].Value);
else richTextBox1.Text += string.Format("{0}\n", r.Cells[3].Value.ToString());
The reason it is confusing is that you have a do nothing semi-colon after the if. This is not normal practice (and if you are doing that as a matter of course then I suggest you stop)
The normal practice would be to reverse the if statement. So that the if has a != (not equal) instead of an == (equals) operator.
i.e.
if (DBNull.Value != r.Cells[3].Value)
richTextBox1.Text += string.Format("{0}\n", r.Cells[3].Value.ToString());
You might also want to consider using a StringBuilder to build your string then finally assign it to the RichTextBox . It will be faster, especially for larger sets of data.
|
|
|
|
|
Yes I understand how the If statement works and I think you are correct about reversing them. I tried using the continue statement that someone mentioned before... like:
if (DBNull.Value == r.Cells[12].Value) continue;
else richTextBox1.Text += string.Format("Notes: {0}\n", r.Cells[12].Value.ToString());
richTextBox1.Text += "\n\n";
the problem with this is if Cells[12] was actually DBNull, then for some reason it would skip the richTextBox1.Text += "\n\n"; ... now when I left continue out, it would work correctly. The program actually works like I wanted it to. I do think that you are correct and there are better ways of doing what I wanted it to do (which I'm sure you know). I'm still learning how things work exactly, and still learning how to make them more code friendly. Thanks
|
|
|
|
|
Jacob Dixon wrote: the problem with this is if Cells[12] was actually DBNull, then for some reason it would skip the richTextBox1.Text += "\n\n";
That is because continue moves to the start of the next iteration skipping any remaining statements in the current iteration.
|
|
|
|
|
Jacob Dixon wrote: When I enter like: 123-4567 it doesn't because of the dash.
You need to loop through the string and when you encounter a numeric character, add it to a temporary string, and ignore the none numeric characters. When done with the loop, you should be able to convert the temporary string to int32 with no problem.
---modified
Int32 is not always going to work if you include area codes. You might want to consider Int64 if you have store the phone numbers as integers.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
|
|
|
|
|
hi all
i work with database and i connetion to it
when aplay to connet to it the application send to me this message
Error [IM002][Microsoft][ODBC Driver Manager] DataSource name not found and no default driver specifies
thanks for any body help me
Thaer
|
|
|
|
|
There's something wrong with your connection string.
You need to provide details of this if you want help.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
Thaer Hamael wrote: DataSource name not found
Check to make sure that the data source you're connecting to has been setup properly. In WinXP go to [Start -> Settings -> Control Panel -> Administrative Tools -> Data Sources (ODBC)] to View/Modify your systems ODBC connections.
|
|
|
|
|
ODBC is soooo '80s... get with ADO.net, there's likely a provider for your database.
|
|
|
|