I need a quick help. I am getting an error invalid attempt to call read when reader is closed when trying to add my databagridview from the reader.
the databases is a class that calls the database connection string. and the databaseColumn is a class that has all columns names.
error for column Time_Completed
what is the issues please help
Here is the code:
//datagridview, bindingsource, data_apapter global objects variables
private DataGridView dataGridView = new DataGridView();
private BindingSource bindingSource = new BindingSource();
private SqlDataAdapter dataAdapter = new SqlDataAdapter();
//class objects
Databases lemars = new Databases();
Databases schuyler = new Databases();
Databases detroitlakeskc = new Databases();
public Form1()
{
InitializeComponent();
}
private void btn_Exit_Click(object sender, EventArgs e)
{
this.Close();
}
private void comboBox_Database_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox_Database.SelectedItem.ToString() == "LeMars21St")
{
GetDataToDataGridView();
}
}
private void GetDataToDataGridView()
{
//prgBar_DataGridViewLoading
DatabaseColumns Obj = new DatabaseColumns();
String SqlcmdString = "Select * from dbo.AllInvoicesInReadyStatus";
SqlDataReader reader;
int i = 1;
try
{
using (SqlConnection conn = new SqlConnection(lemars._LeMarsConnectionString))
{
reader = null;
SqlCommand Sqlcmd = new SqlCommand(SqlcmdString, conn);
conn.Open();
reader = Sqlcmd.ExecuteReader();
if (reader.HasRows)
{
try
{
while (reader.Read())
{
Obj.Invoice = reader["invoice"].ToString();
Obj.Shipment = reader["shipment"].ToString();
Obj.Project = reader["Project"].ToString();
Obj.InvoiceDateTB = Convert.ToDateTime(reader["invoiceDateTB"]);
Obj.CreatedDate = Convert.ToDateTime(reader["CreatedDate"]);
Obj.TypeName = reader["typeName"].ToString();
Obj.ExportedDate = Convert.ToDateTime(reader["exportedDate"]);
Obj.StatusName = reader["statusName"].ToString();
Obj.Total = Convert.ToDecimal(reader["total"]);
Obj.ImportStatus = reader["import_status"].ToString();
//DateTime dateFacturation;
int colIndex = reader.GetOrdinal("Time_Completed");
if (!reader.IsDBNull(colIndex))
Obj.TimeCompleted = reader.GetDateTime(colIndex);
Obj.ErrorDescription = reader["ERROR_DESCRIPTION"].ToString();
//bindingSource.DataSource = reader;
DataTable dt = new DataTable();
dt.Load(reader);
dataGridView.DataSource = dt;
i++;
}
}
finally
{
reader.Close();
}
conn.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}