Results 1 to 5 of 5

Thread: Timeout error while executing delete query in SqlCommand

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    248

    Timeout error while executing delete query in SqlCommand

    Hi,

    We have a Vb.net program and we are using SqlCommand to execute some queries.This is the code block
    Code:
    If Trim(Slno) <> "" Then
                        oCmd_Local = New SqlCommand("select * from ErrorReport_analysis where (sl_no = '" & Slno & "' and Product = '" & ProductStr & "') order by id", Connum_Local)
                        rs_local = oCmd_Local.ExecuteReader
                        If rs_local.Read = True Then
    
                            If Trim(Hostname) <> "" Then
                                oCmd_Local = New SqlCommand("select count(*) from ErrorReport_analysis where (sl_no = '" & Slno & "' and Product = '" & ProductStr & "' and HOSTNAME = '" & Hostname & "')", Connum_Local2)
                                rs_local2 = Val(oCmd_Local.ExecuteScalar.ToString)
                            ElseIf Trim(CompKey) <> "" Then
                                oCmd_Local = New SqlCommand("select count(*) from ErrorReport_analysis where (sl_no = '" & Slno & "' and Product = '" & ProductStr & "' and PC_key like '%-" & CompKey & "-%')", Connum_Local2)
                                rs_local2 = Val(oCmd_Local.ExecuteScalar.ToString)
                            End If
    
                            If rs_local2 = 1 Then 'if only one row present then delete that row itself
                                oCmd_Local = New SqlCommand("Delete from ErrorReport_analysis where (id = " & rs_local.Item("id").ToString & ")", Connum_Local2)
                                oCmd_Local.CommandTimeout = TimeOutval
                                oCmd_Local.ExecuteNonQuery()
                            Else
                                Do While rs_local.Read = True
                                    If Trim(Hostname) <> "" Then
                                        If rs_local.Item("HOSTNAME").ToString = Hostname Then
                                            oCmd_Local = New SqlCommand("Delete from ErrorReport_analysis where (id = " & rs_local.Item("id").ToString & ")", Connum_Local2)
                                            oCmd_Local.CommandTimeout = TimeOutval
                                            oCmd_Local.ExecuteNonQuery()
                                            Exit Do
                                        End If
                                    ElseIf Trim(CompKey) <> "" Then
                                        If InStr(rs_local.Item("PC_key").ToString, "'%-" & CompKey & "-%'", CompareMethod.Text) > 0 Then
                                            oCmd_Local = New SqlCommand("Delete from ErrorReport_analysis where (id = " & rs_local.Item("id").ToString & ")", Connum_Local2)
                                            oCmd_Local.CommandTimeout = TimeOutval
                                            oCmd_Local.ExecuteNonQuery()
                                            Exit Do
                                        End If
                                    Else
                                        Exit Do
                                    End If
                                Loop
                            End If
                        End If
                        rs_local.Close()
                        rs_local = Nothing
                    Else
                        oCmd_Local = New SqlCommand("select * from ErrorReport_analysis where ((sl_no ='' or sl_no is null) and PC_key like '%-" & CompKey & "-%' and HOSTNAME = '" & Hostname & "' and Product = '" & ProductStr & "') order by id", Connum_Local)
                        rs_local = oCmd_Local.ExecuteReader
                        If rs_local.Read = True Then
                            oCmd_Local = New SqlCommand("Delete from ErrorReport_analysis where (id = " & rs_local.Item("id").ToString & ")", Connum_Local2)
                            oCmd_Local.CommandTimeout = TimeOutval
                            oCmd_Local.ExecuteNonQuery()
                        End If
                        rs_local.Close()
    
                        rs_local = Nothing
                    End If
    
                End If
    But while executing delete statement some this timeout error coming
    Name:  untitled.png
Views: 1159
Size:  4.1 KB
    I have Executed sp_who2 command in Management studio and find out that there are 4 sessions created by program and a session with select statement is blocking the session with delete statement.Why this is happening?what to do?

    Thank you

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Timeout error while executing delete query in SqlCommand

    It's your Reader... when you use a reader, it gets an exclusive lock... which can then tie up the tables in the query... you get the time out because it waits for the lock to be released so it can update the table... but it's inside the loop of the reader,so it never gets released... and after the designated amount of time (30seconds in the default I believe) it timesout.

    how do you get rid of it? You don't use a reader... at least not like you're doing... use the reader to fill a datatable, then loop through the datatable to perform the rest of your commands. and make sure they ALL use the SAME connection.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    248

    Re: Timeout error while executing delete query in SqlCommand

    Thanks for reply

    We used 'with (NOLOCK)' and its working fine for most of time but sometime A transport-level error has occurred when receiving results from the server error coming. Is this anything to do with 'with (NOLOCK)'? I checked network connection.It seems to be fine.Why this is happening?

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Timeout error while executing delete query in SqlCommand

    Using bad code logic that results in an unneeded lock and then using WITH (NOLOCK) to get around it - that's not what TG said to do. Is that all you got from his post? I thought he was quite clear - ONE CONNECTION only - fill a datatable from the reader - then there will be no lock on the DB and your deletes will work.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Timeout error while executing delete query in SqlCommand

    err.... yeah, what he said.... I didn't even say anyhting about using NOLOCK hint... all that would do is allow you to read uncommitted transactions... its does NOT bypass the READ LOCK that the reader has...
    Use the reader to fill a datatable... then using that SAME CONNECTION... loop through the datatable to perform the rest of your work.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width