Click here to Skip to main content
13,346,397 members (63,214 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Private Sub tool_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tool_save.Click
        'On Error Resume Next

        Dim cmd As New SqlCommand
        cmd.Connection = _OpenConnect()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "INSERT INTO Customer_Info (Cust_Name,Address,City,Phone,Cust_Invoice) Values (@Cust_Name,@Address,@City,@Phone,@Cust_Invoice)"
        With cmd.Parameters
            .AddWithValue("@Cust_Name", txt_custName.Text)
            .AddWithValue("@Address", txt_address.Text)
            .AddWithValue("@City", txt_city.Text)
            .AddWithValue("@Phone", txt_phone.Text)
            .AddWithValue("@Cust_Invoice", txt_cust_Invoice.Text)
        End With


        Message("Inserted Successfully")
    End Sub

Same Problem
This type error are given

There is already an open DataReader associated with this Command which must be closed first.
Posted 10-Jun-12 22:28pm
Updated 10-Jun-12 22:39pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Like the error says there is an open Datareader on your connection. Check your code before this routine and close the DataReader first then call this sub.
gauravrank 11-Jun-12 3:41am
Public Function _OpenConnect() As SqlConnection
If _cn.ConnectionString = "" Then
_cn.ConnectionString = _GlobalConString
End If
If _cn.State = ConnectionState.Closed Then
End If
Catch ex As Exception
End Try
Return _cn
End Function

This Function is my connection function
Mehdi Gholam 11-Jun-12 3:45am
A part of your code is using this connection with a datareader. Search for "datareader" in your project and work from there to see where you are not closing the datareader.
gauravrank 11-Jun-12 3:47am
i alredy check that all datareader is closed
Mehdi Gholam 11-Jun-12 3:52am
You will have to trust the computer when it says it is not closed :)
gauravrank 11-Jun-12 3:56am
pls when you free then i share my pc in teamviewer pls chek
Mehdi Gholam 11-Jun-12 4:02am
Sorry that is not possible.
gauravrank 11-Jun-12 4:10am
ok no problem and thank for your help
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Look at that line:
cmd.Connection = _OpenConnect()
and then to a line in that function
You use always the same SqlConnection object!
That's the reason for your problems. Create a new SqlConnection object when required.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180111.1 | Last Updated 11 Jun 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100