rahul mekala

rahul mekala

  • 1.6k
  • 112
  • 7.5k

Cannot find table 0. Description: An unhandled exception

Oct 3 2015 11:18 PM

Cannot find table 0.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: Cannot find table 0.

Source Error:
 
My Code=
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Collections.Specialized;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.ObjectBuilder2;
using Microsoft.VisualBasic;
namespace excel_test
{
public partial class _Default : System.Web.UI.Page
{
private string NoWell;
private System.Data.DataTable dt = new System.Data.DataTable();
private System.Data.DataTable dt96 = new System.Data.DataTable();
private System.Data.DataTable dtt = new System.Data.DataTable();
protected void Page_Load(object sender, EventArgs e)
{
gvOne.Visible = true;
GetDataTableFor96Well();
GetPlateInGrid();
}
protected void Button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("Excel is not properly installed!!");
return;
}
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 6] = "Sheet 1 content";
xlWorkBook.SaveAs("d:\\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file d:\\csharp-Excel.xls");
}
// 96 well position view..
private void GetDataTableFor96Well()
{
this.dt96.Columns.Add("SNo");
for (int i = 1; i <= 12; i++)
{
dt96.Columns.Add(i.ToString());
}
char ch = 'A';
for (int j = 1; j <= 8; j++)
{
this.dt96.Rows.Add(new object[] { ch });
ch = (char)(ch + '\x0001');
}
this.dt96.Columns[0].ReadOnly = true;
this.gvOne.DataSource = this.dt96;
this.gvOne.DataBind();
this.ViewState["Datatabledt96"] = this.dt96;
}
//384 well positions
private void GetDataTableFor384Well()
{
this.dt.Columns.Add("SNo");
for (int i = 1; i <= 0x18; i++)
{
this.dt.Columns.Add(i.ToString());
}
char ch = 'A';
for (int j = 1; j <= 0x10; j++)
{
this.dt.Rows.Add(new object[] { ch });
ch = (char)(ch + '\x0001');
}
this.dt.Columns[0].ReadOnly = true;
this.gvOne.DataSource = this.dt;
this.gvOne.DataBind();
this.ViewState["Datatable"] = this.dt;
}
// ***********************the view of the compounds ******************************/
public DataSet GetCompoundCode(string strPlateId)
{
DataSet set = new DataSet();
NameValueCollection objNVC = new NameValueCollection();
objNVC.Add("@v_PlateID", strPlateId);
try
{
set = this.Getdata(objNVC, "NCBS_GETCOMPOUNDCODE");
}
catch (Exception exception)
{
//ExceptionLogger.WriteLog(exception);
}
return set;
}
public DataSet Getdata(NameValueCollection objNVC,string spName)
{
Database database = null;
DbCommand storedProcCommand = null;
DataSet set2;
try
{
spName = "NCBS_GETCOMPOUNDCODE";
database = DatabaseFactory.CreateDatabase();
storedProcCommand = database.GetStoredProcCommand(spName);
storedProcCommand.CommandTimeout = 0;
if ((objNVC != null) && (objNVC.Count > 0))
{
for (int i = 0; i < objNVC.Count; i++)
{
database.AddInParameter(storedProcCommand, objNVC.GetKey(i), DbType.AnsiString, Convert.ToString(objNVC[i]));
}
}
set2 = database.ExecuteDataSet(storedProcCommand);
}
catch (Exception)
{
throw;
}
finally
{
storedProcCommand.Dispose();
database = null;
}
return set2;
}
protected void btnExport_Click(object sender, EventArgs e)
{
if (this.gvOne.Rows.Count > 0)
{
this.GetPlateInGrid();
// Exporter.ExcelExporter(this.gvOne, "PlateDetailsView.xls", base.Response);
}
else
{
//this.lblExportMsg.Text = Messages_Resources.NoRecordExportMsg;
}
}
protected void GetPlateInGrid()
{
string strPlateId = base.Request.QueryString["Id"];
this.NoWell = base.Request.QueryString["NoWell"];
System.Data.DataTable dtt = new System.Data.DataTable();
DataSet compoundCode = new DataSet();
dtt.Columns.Add("CompoundCode");
dtt.Columns.Add("WellPosition");
strPlateId = "CB0001";
compoundCode = this.GetCompoundCode(strPlateId);
this.dtt = compoundCode.Tables[0];
this.Session["Plate"] = this.dtt;
dtt = this.dtt;
if (this.NoWell == "96")
{
this.dt = (System.Data.DataTable)this.ViewState["Datatabledt96"];
}
else
{
this.dt = (System.Data.DataTable)this.ViewState["Datatable"];
}
int num2 = 0;
ArrayList list = new ArrayList();
ArrayList list2 = new ArrayList();
foreach (DataRow row in this.dtt.Rows)
{
string str3 = Convert.ToString(row[0]);
string str = str3.Substring(0, 1);
int num = Convert.ToInt32(str3.Substring(1));
int num3 = Convert.ToChar(str) - 'A';
list.Add(str);
list2.Add(num);
this.dt.Rows[num3][num] = dtt.Rows[num2]["COMPOUND_CODE"].ToString();
num2++;
this.gvOne.DataSource = this.dt;
this.gvOne.DataBind();
}
if (this.NoWell == "96")
{
for (int i = 0; i <= 7; i++)
{
if (i <= 3)
{
this.gvOne.Rows[i].Cells[1].BackColor = Color.LightGreen;
this.gvOne.Rows[i].Cells[12].BackColor = Color.LightGreen;
}
else
{
this.gvOne.Rows[i].Cells[1].BackColor = Color.Red;
this.gvOne.Rows[i].Cells[12].BackColor = Color.Red;
}
}
}
else
{
for (int j = 0; j <= 15; j++)
{
if (j <= 7)
{
this.gvOne.Rows[j].Cells[1].BackColor = Color.LightGreen;
this.gvOne.Rows[j].Cells[2].BackColor = Color.LightGreen;
this.gvOne.Rows[j].Cells[0x17].BackColor = Color.LightGreen;
this.gvOne.Rows[j].Cells[0x18].BackColor = Color.LightGreen;
}
else
{
this.gvOne.Rows[j].Cells[1].BackColor = Color.Red;
this.gvOne.Rows[j].Cells[2].BackColor = Color.Red;
this.gvOne.Rows[j].Cells[0x17].BackColor = Color.Red;
this.gvOne.Rows[j].Cells[0x18].BackColor = Color.Red;
}
}
}
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}
 

Answers (2)