none
Getting a failed to read data when no data is present error while trying to read from SQL Server RRS feed

  • Question

  • Hello Everyone,

    I am working on a data extraction project to get data from a vendor, while I was writing my code, I came across the issue that it would error with System.InvalidOperationException: Invalid attempt to read when no data is present. I am unsure how to fix the issue. The code attached is only part of the larger block due to the script being over 700 lines long. The error is occuring on the line "if(CommentInfo["AttachmentIDs"] != null)". 

    Thanks in advance,

    Nate

              try
                {
                    String IDs = null;
                    foreach (RequestField RF in TI.Fields)
                        IDs += RF._id + ";";
    
                    SqlCommand CheckID = new SqlCommand("SELECT * FROM Tickets WHERE TicketID='" + TI.Ticket_ID + "'", DatabaseConnection);
                    SqlDataReader IDCheck = CheckID.ExecuteReader();
    
                    if (IDCheck.HasRows)
                    {
                        IDCheck.Read();
                        if (TI.UpdatedAt != IDCheck["UpdatedAt"].ToString())
                        {
                            String[] ids = IDCheck["FieldIds"].ToString().Split(';');
                            String[] commentIDs = IDCheck["CommentIDs"].ToString().Split(';');
                            String[] attachmentIDs = IDCheck["AttachmentIDs"].ToString().Split(';');
                            String[] changeNodeIDs = IDCheck["ChangeNodeIDs"].ToString().Split(';');
                            IDCheck.Close();
    
                            if (attachmentIDs.Length > 0)
                            {
                                foreach (String attachmentID in attachmentIDs)
                                {
                                    SqlCommand DeleteAttachment = new SqlCommand("DELETE FROM Attachments WHERE ID='" + attachmentID + "'", DatabaseConnection);
                                    DeleteAttachment.ExecuteNonQuery();
                                }
                            }
                            if (commentIDs.Length > 0)
                            {
                                foreach (String commentID in commentIDs)
                                {
                                    SqlCommand GetCommentInfo = new SqlCommand("SELECT * FROM Comments WHERE ID='" + commentID + "'", DatabaseConnection);
                                    SqlDataReader CommentInfo = GetCommentInfo.ExecuteReader();
                                    CommentInfo.Read();
                                    String[] attachmentIDList = null;
                                    if(CommentInfo["AttachmentIDs"] != null)
                                        attachmentIDList = CommentInfo["AttachmentIDs"].ToString().Split(';');
                                    CommentInfo.Close();
                                    if (attachmentIDList.Length > 0)
                                    {
                                        foreach (String attachmentID in attachmentIDList)
                                        {
                                            SqlCommand DeleteAttachment = new SqlCommand("DELETE FROM Attachments WHERE ID='" + attachmentID + "'", DatabaseConnection);
                                            DeleteAttachment.ExecuteNonQuery();
                                        }
                                    }
    
                                    SqlCommand DeleteComment = new SqlCommand("DELETE FROM Comments WHERE ID='" + commentID + "'", DatabaseConnection);
                                    DeleteComment.ExecuteNonQuery();
                                }
                            }


    • Edited by NHastings25 Tuesday, October 22, 2019 8:37 PM Changed Info
    Tuesday, October 22, 2019 8:35 PM

Answers

  • You need to be using a while loop. 

    while (reader.Read())

    {

      // do other stuff, becuase there is data in the reader. If no data in reader the code in the while statment  is bypassed.

    }

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-data-using-a-datareader

    • Marked as answer by NHastings25 Tuesday, October 22, 2019 8:58 PM
    Tuesday, October 22, 2019 8:48 PM