Click here to Skip to main content
15,917,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am facing the time out and object reference not to set an instance of an object when running an C# exe from the job . The application runs the querries and saves the results as CSV . This issue is happening only some times and not always.

-- Few other things I noticed:
Command.commandtimeout=600
Using SQLreader to store the resultant query.


I have ran the profiler and to find out any deadlock or long running querries but I didnt find any. Tried to run the each and every query to check the timings and are running below 5 secs and command time out is 10 S.

Following is the error which we got when running the exe :

'
Message : Timeout expired.  The timeout period elapsed prior to completion of th
e operation or the server is not responding.
Inner   :
Source  : .Net SqlClient Data Provider
Stack   :    at System.Data.SqlClient.SqlConnection.OnError(SqlException excepti
on, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception
, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cm
dHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, Tds
ParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, Run
Behavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBe
havior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehav
ior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult
result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehav
ior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, S
tring method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()
   at ABC_Ad_Hoc_Emailer.Program.Run_Report() in F:\ABC_code\Ad Hoc Emailer\ABC
Ad Hoc Emailer\ABC Ad Hoc Emailer\Program.cs:line 209
Message : Object reference not set to an instance of an object.
Inner   :
Source  : ABC XYZ Emailer
Stack   :    at ABC_Emailer.Program.Send_Mail(String contents) in F:\ABC_
code\Ad Hoc Emailer\ABC Ad Hoc Emailer\ABC Ad Hoc Emailer\Program.cs:line 455
   at ABC_XYZEmailer.Program.Main(String[] Args) in F:\ABC_code\XYZ Email
er\ABC XYZ Emailer\ABC XYZ Emailer\Program.cs:line 153
'

Following is the code at 209 line(highlighted with bold letters):

SqlConnection db_Conn = new SqlConnection(connstring); 
				db_Conn.Open();
<pre>SqlCommand command = new SqlCommand(Query, db_Conn);	
				command.CommandTimeout = 600;
				<big>SqlDataReader Results = command.ExecuteReader();</big>


Can any one please help me in finding out the root cause.

Regards
Prashant
Posted
Updated 1-Oct-15 19:59pm
v3

The first thing to solve is the error: "Object reference not set to an instance of an object."

Based on the error stack investigate the code in
F:\ABC_code\Ad Hoc Emailer\ABC Ad Hoc Emailer\ABC Ad Hoc Emailer\Program.cs: 

on line 209
There you use some object but the instance is not set. Without seeing all relevant code it's impossible to say what the reason is for the object not being set.
 
Share this answer
 
Comments
Prashant Bangaluru 2-Oct-15 1:59am    
Hi Mika, I have updated the details now. Can you please check once.
Wendelius 2-Oct-15 2:13am    
CAn you post the whol call sequence:
F:\ABC_code\Ad Hoc Emailer\ABC Ad Hoc Emailer\ABC Ad Hoc Emailer\Program.cs:line 455
at ABC_XYZEmailer.Program.Main(String[] Args) in F:\ABC_code\XYZ Email
er\ABC XYZ Emailer\ABC XYZ Emailer\Program.cs:line 153
Prashant Bangaluru 2-Oct-15 4:33am    
Hi Mika,

at 153: Send_Mail("Running of an emai query report has failed on " +DateTime.Now.ToString("ddd dd MMM yyyy HH:mm") );



Line 455: which is in send mail function: using (SmtpClient email_Client = new SmtpClient(Smtp_Server))
{
email_Client.Send(Email);
email_Client.Dispose();
Line : 455 attach.Dispose();
}

Hope this code snippet helps to help me
C#
SqlConnection db_Conn = new SqlConnection(connstring); 
//try increasing connection timeout
db_Conn.ConnectTimeout = 600;
 
Share this answer
 
Comments
Prashant Bangaluru 2-Oct-15 4:30am    
But all the querries are not taking time. Every query is running before 5 secs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900