title: Needs to retrieve data first and then update the new entry into DB
body: <p>I need to retrieve data from the database first and then update the table with the new entry, following is my code but I am having an error:</p>
  
  <blockquote>
    <p>"Invalid attempt to call Read when reader is closed."</p>
  </blockquote>
  
  <p>I know I need to open the datareader by commenting <code>dr1.close</code>, but as soon as I did that I face an another exception:</p>
  
  <blockquote>
    <p>"there is already an open datareader associated with this command which must be closed first. vb.net"</p>
  </blockquote>
  
  <pre><code>Imports System.IO
  Imports System.Data.Sql
  Imports System.Data.SqlClient
  Imports System.Data
  
  Partial Class Officer_Module_GST_id_password
  Inherits System.Web.UI.Page
  
      Dim sscript As String
      Dim sms As New smsgw
      Dim mail As New MailSender
      Dim cmd As New SqlCommand
      Dim ds As New DataSet
      Dim dr As SqlDataReader
      Dim objconn As New connectioncls
      Dim name As String
      Dim pid As String
      Dim pwd As String
      Dim email_sent As Integer
      Dim email_status As String
      Dim mobile As String
      Dim message As String
      Dim subject As String
      Dim email As String
      Dim mtext As String
  
      Protected Sub validatedeal_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles validatedeal.Click
          containertwo.Visible = True
          txt_subject.Text = "Communication of the Provisional Identification Number and Password"
          txt_mail.Text = "Instance mail"
      End Sub
  
      Protected Sub btnsendmail_Click(ByVal sender As Object, ByVal e As System.EventArgs)
          objconn.openconn()
          cmd.Connection = objconn.conn
          cmd.CommandText = "Select Trade_name,provissional_id,access_token,reg_mobile_no,reg_email_id,email_status,isnull(no_of_email_sent,0) from Provisional_details"
          Dim dr1 As SqlDataReader = cmd.ExecuteReader()
          While (dr1.Read())
  
              name = dr1(0).ToString()
              pid = dr1(1).ToString()
              pwd = dr1(2).ToString()
              mobile = dr1(3).ToString()
              email = dr1(4).ToString()
              email_status = dr1(5).ToString()
              email_sent = dr1(6).ToString()
              subject = "subject to instance"
              mtext = "new instance email"
              message = "new instance message"
              Try
                  MailSender.SendEmail("riteshbhatt93" + "@gmail.com", "rock_on", email, subject, mtext, System.Web.Mail.MailFormat.Text, "")
                  Try
                      Call sms.SendSMSUsingNICGW(mobile, message)
                  Catch
                      sscript = "&lt;script language=javascript&gt;alert('Message not sent!!')&lt;/script&gt;"
                      Page.ClientScript.RegisterStartupScript(Me.GetType(), "Empty", sscript)
                      sscript = Nothing
                      Exit Try
                  Finally
                  End Try
  
                  Try
                      Call sms.SendSMSUsingMGOVGW(mobile, message)
                  Catch
                      sscript = "&lt;script language=javascript&gt;alert('Message not sent!!')&lt;/script&gt;"
                      Page.ClientScript.RegisterStartupScript(Me.GetType(), "Empty", sscript)
                      sscript = Nothing
                      Exit Try
                 Finally
                 End Try
              Catch
  
                  Dim cmd1 As New SqlCommand
                  cmd1.Connection = objconn.conn
                  cmd1.Parameters.AddWithValue("@mobile", mobile)
                  cmd1.Parameters.AddWithValue("@Email_status", "NO")
                  cmd1.CommandText = "Update Provisional_details set Email_sent =  @Email_status where reg_mob_no = @mobile"
                  cmd1.ExecuteNonQuery()
                  cmd1.Parameters.Clear()
                  Exit Sub
              Finally
              End Try
  
              dr1.Close()
              Dim cmd2 As New SqlCommand
              cmd2.Connection = objconn.conn
              cmd2.Parameters.AddWithValue("@mobile", mobile)
              cmd2.Parameters.AddWithValue("@Email_status", "YES")
              cmd2.Parameters.AddWithValue("@emailsent", email_sent + 1)
              cmd2.CommandText = "Update Provisional_details set email_status =  @Email_status,no_of_email_sent = @emailsent  where reg_mobile_no = @mobile"
  
              cmd2.ExecuteNonQuery()
              cmd2.Parameters.Clear()
          End While
  
          sscript = "&lt;script language=javascript&gt;alert('Your Mail has been sent to all applied dealers!!')&lt;/script&gt;"
          Page.ClientScript.RegisterStartupScript(Me.GetType(), "Empty", sscript)
          sscript = Nothing
  
      End Sub
  
  End Class
  </code></pre>