|
My apologies if this sounds trivial but I don't know what it means to write debug messages to eventlog.
All I'm doing currently by brute force is putting eventLog1.WriteEntry at several stages of the code to check where the code stops working, using try and catch block and write exception to eventlog as in my code snippet. Now I've put all my creating excel file code into try and catch like below.
private void createXLS(string dir)
{
eventLog1.WriteEntry("In createXLS");
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
eventLog1.WriteEntry("createXLS declaration");
try
{
xlApp = new Excel.Application();
xlApp.DisplayAlerts = false;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
eventLog1.WriteEntry("Xls worksheet created.");
xlWorkSheet.Cells[1, 2] = "Date";
xlWorkBook.SaveAs(dir, Excel.XlFileFormat.xlWorkbookDefault, misValue, misValue, true, false, Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlLocalSessionChanges, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
eventLog1.WriteEntry("Excel file is saved under " + dir);
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
catch(Exception e1)
{
eventLog1.WriteEntry("createXLS error 1: " + e1.ToString());
}
}
And this is the error I get. Line 218 is at
xlApp = new Excel.Application();
createXLS error 1: System.Runtime.InteropServices.COMException (0x80080005): Retrieving the COM class factory for component with
CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed
Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at UPNP_WS_Weekly.UpnpWeeklyMailService.createXLS(String dir) in c:\Users\SolarGy\Documents\Visual Studio 2013\Projects\UPNP_WS_Weekly\UPNP_WS_Weekly\Service1.cs:line 218
|
|
|
|
|
So what is the code on line 218?
Also write the dir to the log to make sure it is valid
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
line 218 is at
xlApp = new Excel.Application();
|
|
|
|
|
Written the dir to the log. It is a valid directory.
|
|
|
|
|
The problem sounds to me like an issue with the account that you are running the service under doesn't have access to the appropriate part of the registry. To test this out, set your service to run as a named user and then see if it works.
Note, I wouldn't recommend this as a permanent solution. Instead, I'd have the system admins create a dedicated locked down user for this service to run under.
|
|
|
|
|
This windows service may be running under a different user.
This user may not have access rights to the file.
|
|
|
|
|
My windows service is running under local system account. Well I logged in as the administrator and created the service so can I assume the service is running under the administrator account?
|
|
|
|
|
No. Take a look at your service settings. It's not the admin account - it would be incredibly dangerous if services assumed the identity of the account they were created under.
|
|
|
|
|
I right click the service and check its properties. Under Log On, 'local system account' radio button is selected and not 'this account'. I do lack the knowledge on if this means the service is running under admin account or not. Sorry about that.
So I wanted to try a different log on option. I clicked on 'This account' and configure using 'Help me configure user account log on options'. There is only one option for "From this location" which is COMPANY-SERVER. I typed my administrator name and after checking names, it appears as COMPANY-SERVER\COMPANY. I set the password and I applied the settings.
So I run the service again using This account as defined above. The problem still remains.
|
|
|
|
|
Just a quick question. Do you have Excel installed on the server? It's not a great idea to use the client version of Office applications on a server.
|
|
|
|
|
Sorry I started learning all these a few days back so I don't know if office application is installed on the server. How do I check it?
Now I check the properties of excel 2013 and here is the permission status.
Everyone - only Read & execute, Read are allowed
SYSTEM - All other permissions are allowed except Special permissions
COMPANY (COMPANY-SERVER\COMPANY) - only Special permissions is allowed!
Administrators (COMPANY-SERVER\administrators) - same as SYSTEM
Users (COMPANY-SERVER\Users)- same as Everyone
Now I'm logged in as COMPANY(COMPANY-SERVER\COMPANY). Does it mean I'm not the administrator? When I check the user account, there is no other account, only COMPANY (COMPANY-SERVER\COMPANY)account which is also named as the Administrator. Now I'm confused.
|
|
|
|
|
Running Office applications from a non-interactive session is not supported. See if any of the suggestions from this blog post[^] help.
If you still can't get it to work, you might want to look at using a tool like EPPlus[^] or ClosedXML[^] to generate the file without using Excel.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
It is quite a useful blog post have given here http://bharathkumaran.wordpress.com/2011/10/25/running-excel-from-a-windows-service/[^]
I followed it.
- I created C:\Windows\SysWOW64\config\systemprofile\Desktop.
- I tried to change DCOMConfig settings. I couldn't find Microsoft Excel Application.
- So I followed another link - http://social.technet.microsoft.com/Forums/windows/en-US/dde69147-a01a-4eb1-8ea9-31adbf874bed/microsoft-excel-application-entry-missing-in-dcomcnfg
- although we have slightly different applications, I decided to do these
Quote:Solved!
1. In DCOMCNFG, right click on the My Computer and select properties.
2. Choose the COM Securities tab
3. In Access Permissions, click "Edit Defaults" and add Network Service to it and give it "Allow local access" permission. Do the same for \Users.
4. In launch and Activation Permissions, click "Edit Defaults" and add Network Service to it and give it "Local launch" and "Local Activation" permission. Do the same for \Users
Now when I run the windows service again, I have a different error such as
createXLS error 1: System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC
at Microsoft.Office.Interop.Excel.Workbooks.Add(Object Template)
at UPNP_WS_Weekly.UpnpWeeklyMailService.createXLS(String dir) in c:\Users\Documents\Visual Studio 2013\Projects\UPNP_WS_Weekly\UPNP_WS_Weekly\Service1.cs:line 218
Line 218 is at
xlWorkBook = xlApp.Workbooks.Add(misValue);
I don't know if I should be happy getting a different error.
|
|
|
|
|
I will try to use the ClosedXML although it bugs me terribly when I can't solve the previous problem.
|
|
|
|
|
Read the license of Office; Or use an alternative way to write your Excel-file - there are a few of them.
As an easy alternative solution; you could turn it from a windows service and run it as a normal application.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
The excel workbook creation and saving is fine in normal user interactive application. But I need to automatically generate an excel report once a week and once a month so I'm using windows service. I haven't really looked into task scheduler. I don't have much luck with quartz.net (but this is still to be used in service mode). But if Microsoft Office doesn't allow non-user-interactive application (like my windows service), I wonder if task scheduler can do the job.
|
|
|
|
|
Hi,
I have written the below code to connect to the server.Also I should login to the server to fetch data from it.How to supply credentials via socket so that the server allows the client to fetch the data from server.
byte[] receivedBytes = new byte[1024];
int portno = Convert.ToInt32(txtportnum.Text);
IPHostEntry ipHost = Dns.GetHostByName("192.168.x.y");
IPAddress ipAddress = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, portno);
txtstatus.Text = "Creating socket object...";
Socket send_soc = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
send_soc.Connect(ipEndPoint);
txtstatus.AppendText("\nSuccessfully connected to:" + "\t" + send_soc.RemoteEndPoint);
txtstatus.AppendText("\nConnecting via :" + "\t" + send_soc.LocalEndPoint);
string sendingMessage = "abcde";
SendMessage(send_soc, sendingMessage);
int totalBytesReceived = send_soc.Receive(receivedBytes);
string dff = "";
string s = System.Text.UTF7Encoding.UTF7.GetString(receivedBytes, 0, totalBytesReceived);
txtRx.AppendText(s);
send_soc.Shutdown(SocketShutdown.Both);
send_soc.Close();
try
{
FileStream dr = new FileStream("E:/Demo/IpData/Call.txt", FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
StreamReader fg = new StreamReader(dr);
string df = fg.ReadToEnd();
dff = df;
fg.Dispose();
dr.Dispose();
}
catch (Exception dfjdfs)
{
throw dfjdfs;
}
try
{
File.Delete("E:/Demo/IpData/Call.txt");
}
catch (Exception jhu)
{
throw jhu;
}
try
{
FileStream cd = new FileStream("E:/Demo/IpData/Call.txt", FileMode.Create);
StreamWriter cdf = new StreamWriter(cd);
cdf.Write(dff);
cdf.Write(s);
cdf.Dispose();
cd.Dispose();
}
catch (Exception hgy)
{
throw hgy;
}
}
Any guidance on how to supply credentials will be of great help.
|
|
|
|
|
Sockets and credentials aren't really related with each other... TCP just specifies the protocol for how to the communication should work (e.g. how the packets look like etc.). If you take a look at the OSI model[^]:
TCP is layer 4 (Transport Layer[^].
Credentials etc. should be on layer 5 (Session Layer[^])
Session 5-7 are those Layers that you need to implement yourself in your application (or use functionality provided by a framework)
So you need to provide some more details what you're trying to accomplish as " I should login to the server to fetch data from it." doesn't say anything about what you need to do.
What does the server do? Which protocol does it use for communication etc.?
|
|
|
|
|
Since you're using sockets, you're using the transport layer. Credentials are at the application layer. In order to provide those to the server you would have to know exactly what it expects and exactly how it needs to be formatted, byte-for-byte.
Since we know nothing about your server, it's impossible to answer your question. In order to supply that information to us, you would also have to understand it, making your question a moot point.
|
|
|
|
|
Arjun Mourya wrote: I have written the below code to connect to the server. What kind of server?
Arjun Mourya wrote: Also I should login to the server to fetch data from it If your server happens to be a database, you'd use a database-connection - not a raw socket. You don't need to login to use a socket; the server only needs to accept the connection.
Arjun Mourya wrote: <layer>How to supply credentials via socket so that the server allows the client to fetch the data from server. Did you write the server? Does it have a routine to check any credentials supplied?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
hi
am developing c# winforms application.
my requirement is basedon tag_id(sEPC) ,i need to get the data from inventory table and populate that data(tagid,categoryid,productid,productname,gateid and remarks) on listview
next check the transaction table whether the out time is null. if out_time is null ,then update the record using tag_id.
if not null then insert the new record in transaction table.
here is the code:
string query = "SELECT tag_id,category_id,product_id,product_name,gate_id,remarks FROM inventory where tag_id Like '" + sEPC + "'";
MySqlCommand cmd;
MySqlDataReader rdr;
MySqlConnection connection = new MySqlConnection(MyConnectionString);
connection.Open();
// DataTable dt = new DataTable();
cmd = new MySqlCommand(query, connection);
rdr = cmd.ExecuteReader();
/*Received Epc matched in the Inventory Table */
if (rdr != null)
{
String s2 = DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss").Trim();
// ListView1_EPC.Items[j].Remove();
if (rdr.Read())
{
String a1;
cnt = ListView1_EPC.Items.Count;
ListViewItem item = new ListViewItem(rdr.GetString(0));
// cnt = ListView1_EPC.Items.Count;
item.SubItems.Add(rdr.GetString(1));
item.SubItems.Add(rdr.GetString(2));
item.SubItems.Add(rdr.GetString(3));
item.SubItems.Add(rdr.GetString(4));
item.SubItems.Add("");
item.SubItems.Add("");
item.SubItems.Add(rdr.GetString(5));
//item.SubItems[Remarks.Index].Text = rdr.GetString(5);
ListView1_EPC.Items.Add(item);
rdr.Close();
cnt = ListView1_EPC.Items.Count;
MessageBox.Show("cnt:"+cnt);
string query1 = "SELECT tag_id,category_id,product_id,product_name,gate_id,remarks FROM transaction where tag_id Like '" + sEPC + "' AND out_time=NULL";
MySqlCommand cmd1 = new MySqlCommand(query1, connection);
MySqlDataReader rdr1 = cmd1.ExecuteReader();
if (rdr1 == null)
{
item.SubItems[INTime.Index].Text = s2;
//String str = "insert into transaction(tag_id,category_id,product_id,product_name,gate_id,in_time,out_time,remarks)values('" + ListView1_EPC.Items[i].Text.Trim() + "','" + item.SubItems[CategoryID.Index].Text.Trim() + "','" + item.SubItems[ProductID.Index].Text.Trim() + "','" + item.SubItems[ProductName.Index].Text.Trim() + "','" + item.SubItems[GateID.Index].Text.Trim() + "','" + item.SubItems[INTime.Index].Text.Trim() + "','" + item.SubItems[OUTTime.Index].Text + "','" + item.SubItems[Remarks.Index].Text.Trim() + "')";
// MySqlCommand cmd;
String str = "insert into transaction(tag_id,category_id,product_id,product_name,gate_id,in_time,remarks)values('" + ListView1_EPC.Items[i].Text.Trim() + "','" + item.SubItems[CategoryID.Index].Text.Trim() + "','" + item.SubItems[ProductID.Index].Text.Trim() + "','" + item.SubItems[ProductName.Index].Text.Trim() + "','" + item.SubItems[GateID.Index].Text.Trim() + "','" + item.SubItems[INTime.Index].Text.Trim() + "','" + item.SubItems[Remarks.Index].Text.Trim() + "')";
MySqlConnection connection1 = new MySqlConnection(MyConnectionString);
cmd = new MySqlCommand(str, connection1);
connection1.Open();
testret = cmd.ExecuteNonQuery();
}
else
{
item.SubItems[OUTTime.Index].Text = s2;
String str = "update transaction set out_time='" + item.SubItems[OUTTime.Index].Text + "' where tag_id='" + ListView1_EPC.Items[i].Text + "' ";
// String str = "insert into transaction(tag_id,category_id,product_id,product_name,gate_id,in_time,out_time,remarks)values('" + ListView1_EPC.Items[i].Text.Trim() + "','" + item.SubItems[CategoryID.Index].Text.Trim() + "','" + item.SubItems[ProductID.Index].Text.Trim() + "','" + item.SubItems[ProductName.Index].Text.Trim() + "','" + item.SubItems[GateID.Index].Text.Trim() + "','" + item.SubItems[INTime.Index].Text.Trim() + "','" + item.SubItems[OUTTime.Index].Text + "','" + item.SubItems[Remarks.Index].Text.Trim() + "')";
// String str = "insert into transaction(tag_id,out_time)values('" + sEPC + "','" + item.SubItems[OUTTime.Index].Text + "')";
MySqlConnection connection1 = new MySqlConnection(MyConnectionString);
cmd = new MySqlCommand(str, connection1);
connection1.Open();
testret = cmd.ExecuteNonQuery();
}
}//if
while excecution.am getting the error as:
InvalidArgument=Value of '20' is not valid for 'index'.
Parameter name: index
in update query.
plz help me......
there is no index 20 in listview.
my listview is having just:
tagid,categoryid,productid,productname,gateid,intime,outtime,remarks.
|
|
|
|
|
Could you put the code in pre tags? (you can use the "code" button)
It's a little annoying to read this way.
|
|
|
|
|
I'm missing field-names in your update query. How does MySQL know which column you want to push each value into?
UPDATE [tablename]
SET col1 = value1,
col2 = value2 I can only see values in your query, not the fieldnames.
Further you'd like to make that a parameterized query, to prevent attacks on your application using sql-injection. And you should wrap those disposables in a using -block.
..and you might want to do simply a "SELECT COUNT(*)" to test whether the record exists; then you can use ExecuteScalar , which would be a bit more efficient.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
it's ok. thanq
But am getting error as ArgumentOutORange exception at
item.SubItems[INTime.Index].Text = s2;
Listview consists 0nly 7 colums.
string query = "SELECT tag_id,category_id,product_id,product_name,gate_id,remarks FROM inventory where tag_id Like '" + sEPC + "'";
MySqlCommand cmd;
MySqlDataReader rdr;
MySqlConnection connection = new MySqlConnection(MyConnectionString);
connection.Open();
cmd = new MySqlCommand(query, connection);
rdr = cmd.ExecuteReader();
if (rdr != null)
{
String s2 = DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss").Trim();
if (rdr.Read())
{
cnt = ListView1_EPC.Items.Count;
ListViewItem item = new ListViewItem(rdr.GetString(0));
cnt = ListView1_EPC.Items.Count;
item.SubItems.Add(rdr.GetString(1));
item.SubItems.Add(rdr.GetString(2));
item.SubItems.Add(rdr.GetString(3));
item.SubItems.Add(rdr.GetString(4));
item.SubItems.Add(rdr.GetString(5));
ListView1_EPC.Items.Add(item);
cnt = ListView1_EPC.Items.Count;
rdr.Close();
string query1 = "SELECT tag_id,category_id,product_id,product_name,gate_id,remarks FROM transaction where tag_id Like '" + sEPC + "' AND out_time=NULL";
MySqlCommand cmd1= new MySqlCommand(query1,connection);
MySqlDataReader rdr1 = cmd1.ExecuteReader();
if (rdr1 != null)
{
item.SubItems[INTime.Index].Text = s2;
ListView1_EPC.Items.Add(item);
String s1 = "update transaction set out_time='" + s2 + "' where tag_id='" + ListView1_EPC.Items[i].Text + "' ";
MySqlCommand cmd2 = new MySqlCommand(s1, connection);
cmd2.ExecuteNonQuery();
}
else
{
item.SubItems[Remarks.Index].Text = s2;
ListView1_EPC.Items.Add(item);
String str = "insert into transaction(tag_id,category_id,product_id,product_name,gate_id,in_time,remarks)values('" + ListView1_EPC.Items[i].Text.Trim() + "','" + item.SubItems[CategoryID.Index].Text.Trim() + "','" + item.SubItems[ProductID.Index].Text.Trim() + "','" + item.SubItems[ProductName.Index].Text.Trim() + "','" + item.SubItems[GateID.Index].Text.Trim() + "','" + item.SubItems[Remarks.Index].Text.Trim() + "','" + item.SubItems[OUTTime.Index].Text.Trim() + "')";
MySqlCommand cmd2 = new MySqlCommand(str, connection);
cmd2.ExecuteNonQuery();
}
}
|
|
|
|
|
Member 10263519 wrote: But am getting error as ArgumentOutORange exception at item.SubItems[INTime.Index].Text = s2;
Add a "Debug.WriteLine(INTime.Index)", and verify that you got the right column.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|