Error Prompted at RunTime
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 8 of 8

Thread: Error Prompted at RunTime

Threaded View

  1. #1
    Join Date
    Feb 2009

    Error Prompted at RunTime


    I have the following code;

    Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
            Dim strConn As String
            strConn = ConfigurationManager.ConnectionStrings("CCMSConnectionString").ConnectionString
            Dim conn As New SqlConnection(strConn)
            'Dim nextconn As New SqlConnection(strConn)
            Dim strSql As String
            strSql = "INSERT INTO dbo.TblChangeControlDet (RequestBy, CreatedBy, ChangeType, TechnicalPerson, Req, BusImp,ChangeRaisedDate, PeerRvw) VALUES (@RequestBy, @CreatedBy, @ChangeType, @TechnicalPerson, @Req, @BusImp,@ChangeRaisedDate, @PeerRvw )"
            Dim cmd As New SqlCommand(strSql, conn)
            With cmd.Parameters
                .AddWithValue("@RequestBy", Me.cmbchngreq.SelectedItem.ToString)
                .AddWithValue("@CreatedBy", Me.cmbchngcrt.SelectedItem.ToString)
                .AddWithValue("@ChangeType", Me.cmbchngtyp.SelectedItem.ToString)
                .AddWithValue("@TechnicalPerson", Me.cmbTechname.SelectedItem.ToString)
                .AddWithValue("@Req", txtreq.Text)
                .AddWithValue("@BusImp", txtbusimp.Text)
                .AddWithValue("@ChangeRaisedDate", DateTime.Now.ToString("dd\/MM\/yyyy HH:mm:ss"))
                .AddWithValue("@PeerRvw", Me.cmbpeername.SelectedItem.ToString)
            End With
            Dim query As String
            Dim RefID As New System.Data.SqlClient.SqlCommand(("Select RefNo From dbo.QryRefNo"), conn)
                Using Autoreader As System.Data.SqlClient.SqlDataReader = RefID.ExecuteReader()
                    While Autoreader.Read()
                        Dim RefNo As String = (Autoreader.GetValue(0))
                        query = "UPDATE dbo.TblChangeControlDet SET RefNo = @RefNo WHERE  ChangeRaisedDate = @ChangeRaisedDate"
                        cmd = New SqlCommand(query, conn)
                        cmd.Parameters.AddWithValue("@RefNo", RefNo)
                        cmd.Parameters.AddWithValue("@ChangeRaisedDate", DateTime.Now.ToString("dd\/MM\/yyyy HH:mm:ss"))
                    End While
                End Using
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Information, "Spare Elements")
            End Try
        End Sub
    At run time when I click the button I receive the error -

    'There is already an open DataReader associated with this Command which must be closed first".

    The INSERT works fine the problem us with the UPDATE statement.

    Last edited by dr223; October 19th, 2012 at 07:02 AM.

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a survey!

HTML5 Development Center