Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: VB.NET
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
Edited 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 at 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 at 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 at 11-Jun-12 3:47am
i alredy check that all datareader is closed
Mehdi Gholam at 11-Jun-12 3:52am
You will have to trust the computer when it says it is not closed :)
gauravrank at 11-Jun-12 3:56am
pls when you free then i share my pc in teamviewer pls chek
Mehdi Gholam at 11-Jun-12 4:02am
Sorry that is not possible.
gauravrank at 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
0 Sergey Alexandrovich Kryukov 195
1 Peter Leow 130
2 Zoltán Zörgő 120
3 BillWoodruff 85
4 bling 70
0 Sergey Alexandrovich Kryukov 9,568
1 OriginalGriff 6,901
2 Peter Leow 4,737
3 Zoltán Zörgő 4,404
4 CHill60 2,932

Advertise | Privacy | Mobile
Web03 | 2.8.150129.1 | Last Updated 11 Jun 2012
Copyright © CodeProject, 1999-2015
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