c# - Windows Service not starting due to DataAdapter.Fill() -


i wrote windows service takes data local database , performs function. connect database wrote following code

sqlconnection connstring1;             string conn1 = configurationmanager.connectionstrings["nameofconnectionstring"].connectionstring.tostring();             connstring1 = new sqlconnection(conn1);             connstring1.open();             string cmd = "select [orderid], [vendorid], [txtpaymentmethod] , [txtpaymentstatus], [updated_on] [postmate_ shopping].[dbo].[tbl_order_master]  [txtpaymentmethod] = 'online' , [txtpaymentstatus]= 'pending'";             datatable dt = new datatable();             dataset ds = new dataset();             sqldataadapter da1 = new sqldataadapter(cmd, connstring1);             da1.fill(ds);             da1.dispose();             connstring1.close(); 

the code before da1.fill(ds); working , service gets installed when da1.fill(ds); included in code, service not start. tried same code in web application , working fine. when da1.fill(ds); included:

the "scheduledservice" service on local computer started , stopped.some services stop automatically if not in use other services or programs.

to check if service working removed above code , replaced code:

filestream fs = new filestream(@"d:\scheduledservice.txt", filemode.openorcreate, fileaccess.write); //set streamwriter adding text streamwriter sw = new streamwriter(fs);  //find end of underlying filestream sw.basestream.seek(0, seekorigin.end);  //add text sw.writeline(content); //add text underlying filestream sw.flush(); //close writer sw.close(); 

the service worked fine. tried searching problem no relevant solution found. please help. in advance.

ask clarity if needed.

edit:any thoughts on using datareader?

it because da1.fill(ds) failing exception. check event log if there details of exception. looking @ code, 1) no need open , close connection adaptor you, 2) dispose method should after close. 3) use "using" scope db objects take care of dispose implicitly.

lastly, not in service start. service start initialization , should return possible. spawn thread perform other activities.


Comments