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

  • Thread starter Thread starter NHastings25
  • Start date Start date
N

NHastings25

Guest
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();
}
}

Continue reading...
 
Back
Top