Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 12th, 2007, 01:55 AM
Registered User
 
Join Date: Aug 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default There is already an open datareader associated wit

Hi.. Thanks a lot..i tried with it. It says "There is already an open datareader associated with this connection which must be closed first." how to close the existing data reader? pls help. below is my code.. i need to insert values from insert sql statement into database.. am not able to..plss help.

Code:
Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click

        Dim CALLID, RequestorID, CommentsFromITD, MessageFromITD, MessageToITD, CallStatus, strSQL As String
        CALLID = Request.QueryString("CallID")
        RequestorID = Session("USER_ID")
        CommentsFromITD = lblcomments.Text
        MessageFromITD = lblmessage.Text
        MessageToITD = txt_desc.Text
        CallStatus = Request.Form(ddl_callstatus.UniqueID)

        strSQL = "SELECT * FROM MESSAGES WHERE CALL_ID=" & CALLID & ""
        'Dim Objcmd As New SqlCommand(strSQL, ObjConn)
        Dim ObjCmd As SqlCommand
        Dim ObjDR As SqlDataReader
        'ObjConn.Open()
        Try
            'ObjDR = Objcmd.ExecuteReader()
            ObjConn.Open()

            ObjCmd = New SqlCommand(strSQL, ObjConn)
            ObjDR = ObjCmd.ExecuteReader()

            'If Not ObjDR Is DBNull.Value Then
            If ObjDR.HasRows Then
                'If Not ObjDR("MESSAGE_TO_ITD") = MessageToITD Then
                If Not IsDBNull(ObjDR("MESSAGE_TO_ITD")) Then
                    'If Not ObjDR.HasRows Then
                    strSQL = "UPDATE MESSAGES SET MESSAGE_TO_ITD='" & MessageToITD & "',FLAG='1' WHERE CALL_ID=  " & CALLID & ""
                Else
                    strSQL = "UPDATE MESSAGES SET MESSAGE_TO_ITD='" & MessageToITD & "',FLAG='2' WHERE CALL_ID=  " & CALLID & ""
                End If
            Else
                    strSQL = "INSERT INTO MESSAGES (CALL_ID,MESSAGE_TO_ITD,FLAG) VALUES ( " & CALLID & " ,'" & MessageToITD & "','1')"
                End If
            Dim ObjCmd1 As New SqlCommand(strSQL, ObjConn)
            ObjCmd1.ExecuteNonQuery()
            ObjConn.Close()
            ObjDR.Close()
            ObjConn.Close()
        Catch ex As SqlException
            lblmsg.Text = ex.Message.ToString

        End Try
        Response.Redirect("UserCreateMessage.aspx")
    End Sub


 
Old August 12th, 2007, 03:37 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You can simply call Close() on the DataReader before you call ExecuteNonQuery. E.g.:

ObjDR.Close()
Dim ObjCmd1 As New SqlCommand(strSQL, ObjConn)
ObjCmd1.ExecuteNonQuery()
ObjConn.Close()

Tip: if you post code here, can you please remove "dead code". You have commented out quite a bit of code. This is easy to spot in an IDE with color coding, but more difficult to read on a forum like this. Removing irrelevant code makes it easier for us to focus on the problem.

Cheers,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem wit struts and spring DR POMPEII Struts 0 August 17th, 2006 09:16 AM
There is already an open DataReader associated wit jayanp ADO.NET 1 July 2nd, 2006 01:10 PM
Datareader NitinJoshi ADO.NET 4 January 31st, 2005 08:34 AM
There is already an open DataReader associated wit flyin ADO.NET 14 February 20th, 2004 08:41 AM
"There is already an open DataReader " error kaz VS.NET 2002/2003 4 December 17th, 2003 11:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.