SqlCommand.CommandTimeout not working

C

cmhavoc

Can someone please look at my code and tell me what I might be doing
wrong? I'm getting the following error:

Exception Details: System.NullReferenceException: Object reference
not set to an instance of an object.

for the line with "cmdCat.CommandTimeout = 300"

How can I set the commandTimeout?


See my code below

------------------------------------------------

sub page_load(sender as Object, e as EventArgs)



Dim sqlcon As New
System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("CONNECTION_STRING"))
Dim cmdCat As System.Data.SqlClient.SqlCommand
cmdCat.CommandTimeout = 300
Dim daCat As System.Data.SqlClient.SqlDataAdapter
Dim dsCat1 As New System.Data.DataSet

Dim strCat as String = "SELECT GOES HERE"

daCat = New System.Data.SqlClient.SqlDataAdapter(strCat, sqlcon)
sqlcon.Open()
daCat.Fill(dsCat1)
sqlcon.Close()

rptEmails.DataSource = dsCat1
rptEmails.DataBind()

Page.DataBind()
end sub
 
P

paulhux174

Dim cmdCat As System.Data.SqlClient.SqlCommand

At this stage you haven't set cmdCat to a New
System.Data.SqlClient.SqlCommand(cmdText)
So there is no Object to reference.
 
J

Jack Jackson

Can someone please look at my code and tell me what I might be doing
wrong? I'm getting the following error:

Exception Details: System.NullReferenceException: Object reference
not set to an instance of an object.

for the line with "cmdCat.CommandTimeout = 300"

How can I set the commandTimeout?

Dim sqlcon As New
System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("CONNECTION_STRING"))
Dim cmdCat As System.Data.SqlClient.SqlCommand
cmdCat.CommandTimeout = 300

At this point cmdCat is a variable of type SqlCommand that is set to
Nothing. You need to allocate an object:

Dim cmdCat = New System.Data.SqlClient.SqlCommand
 
C

cmhavoc

Thank you ... that fixed that error but now I'm back to getting a
timeout error. The weird thing is that it times out after only 30
seconds even though I have this and the IIS and the web.config set to
300 seconds. Any thoughts? Below is the error.

--------------------------------------

Timeout expired. The timeout period elapsed prior to completion of
the operation or the server is not responding.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Timeout
expired. The timeout period elapsed prior to completion of the
operation or the server is not responding.

Source Error:

Line 16: daCat = New System.Data.SqlClient.SqlDataAdapter(strCat,
sqlcon)
Line 17: sqlcon.Open()
Line 18: daCat.Fill(dsCat1)
Line 19: sqlcon.Close()
Line 20:


Source File: C:\Inetpub\wwwroot\mergetools.com\campaign\index-
test.aspx Line: 18

Stack Trace:

[SqlException: Timeout expired. The timeout period elapsed prior to
completion of the operation or the server is not responding.]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +742
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior
behavior) +45

System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior
behavior) +5
System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32
startRecord, Int32 maxRecords, String srcTable, IDbCommand command,
CommandBehavior behavior) +304
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32
startRecord, Int32 maxRecords, String srcTable, IDbCommand command,
CommandBehavior behavior) +77
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +38
ASP.index_test_aspx.page_load(Object sender, EventArgs e) in C:
\Inetpub\wwwroot\mergetools.com\campaign\index-test.aspx:18
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +772
 
C

cmhavoc

Thank you ... that fixed that error. However, now I'm back to my
original timeout issue. It times out after only 30 seconds, even
though I have this timeout set to 300 seconds and likewise for my
web.config and IIS. Any thoughts?


-----------------

Timeout expired. The timeout period elapsed prior to completion of
the operation or the server is not responding.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Timeout
expired. The timeout period elapsed prior to completion of the
operation or the server is not responding.

Source Error:

Line 16: daCat = New System.Data.SqlClient.SqlDataAdapter(strCat,
sqlcon)
Line 17: sqlcon.Open()
Line 18: daCat.Fill(dsCat1)
Line 19: sqlcon.Close()
Line 20:


Source File: index-test.aspx Line: 18

Stack Trace:

[SqlException: Timeout expired. The timeout period elapsed prior to
completion of the operation or the server is not responding.]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +742
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior
behavior) +45

System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior
behavior) +5
System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32
startRecord, Int32 maxRecords, String srcTable, IDbCommand command,
CommandBehavior behavior) +304
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32
startRecord, Int32 maxRecords, String srcTable, IDbCommand command,
CommandBehavior behavior) +77
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +38
ASP.index_test_aspx.page_load(Object sender, EventArgs e) in index-
test.aspx:18
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +772
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top