Gustavo

Gustavo

  • NA
  • 1.3k
  • 455.2k

How Do I: Close a SqlDataReader?

Apr 26 2010 2:17 PM

Hello:
Below is my code. I am trying to loop thur a list with a SqlDataReader and then take that list and do a different SqlDataReader to do some new checking.
 
I am getting an error at the line with "//ERROR". I am closing it but I guess not correctly.
 
The error is: {"There is already an open DataReader associated with this Command which must be closed first."}
Can someone tell me what I am missing?
 
 
public void CheckFieldGio()
{
ClearE();
E_Process =
"CheckField";
//
if (E_Run)
{
int IN = 0;
int NOTIN = 0;
int FILECOUNT = 0;
int READ = 0;
E_Table =
"";
E_Error =
"";
E_Action =
"Started..." + DateTime.Now.ToString();
Started();
SqlConnection MyConnection = new SqlConnection(Program.DBConnectionString);
MyConnection.Open();
for (int i = 1; i < TheFileCounter; i++)
{
E_Table = FormICEPack.dataGridViewGrid_Grid.Rows[i].Cells[1].Value.ToString();
Schema_SQLCommand =
"SELECT ORDINAL_POSITION, TABLE_NAME, COLUMN_NAME";
Schema_SQLCommand +=
" FROM INFORMATION_SCHEMA.COLUMNS";
Schema_SQLCommand +=
" WHERE (TABLE_NAME = '" + E_Table + "')";
SqlCommand Schema_Command = new SqlCommand(Schema_SQLCommand, MyConnection);
SqlDataReader Schema_Reader = Schema_Command.ExecuteReader();
if (Schema_Reader.HasRows)
{
FILECOUNT += 1;
while (Schema_Reader.Read())
{
ClearE_Sub();
File_Name = Schema_Reader[
"TABLE_NAME"].ToString();
Field_Name = Schema_Reader[
"COLUMN_NAME"].ToString();
FileField_SQLCommand =
"SELECT * ";
FileField_SQLCommand +=
" FROM [File_Field]";
FileField_SQLCommand +=
" WHERE (File_ID = '" + File_Name + "')";
FileField_SQLCommand +=
" AND";
FileField_SQLCommand +=
" (Field_ID = '" + Field_Name + "')";
READ += 1;
SqlCommand FileField_Command = new SqlCommand(FileField_SQLCommand, MyConnection);
SqlDataReader FileField_Reader = FileField_Command.ExecuteReader();   //ERROR
if (FileField_Reader.HasRows)
{
while (FileField_Reader.Read())
{
IN += 1;
File_Name = FileField_Reader[
"TABLE_NAME"].ToString();
Field_Name = FileField_Reader[
"COLUMN_NAME"].ToString();
//... UPDATE code goes here.
}
}
else
{
NOTIN += 1;
//... INSERT code goes here.
}
FileField_Reader.Close();
FileField_Reader.Dispose();
}
}
Schema_Reader.Close();
Schema_Reader.Dispose();
}
MessageBox.Show("FILECOUNT: " + FILECOUNT + " IN: " + IN + " NOTIN: " + NOTIN + " READ: " + READ);
MyConnection.Close();
MyConnection.Dispose();
Finished();
}
else
{
Skipped();
}

Answers (3)