Click here to Skip to main content
15,903,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Dataset to Excel sheets using interop.excel generation in C#.net

i got error Object reference not set to an instance of an object. how to solve this error



protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

getquery();

}

}
void getquery()
{
con = new SqlConnection(strcon);
con.Open();
string query = " Select top 100 * from getezee; ";
cmd = new SqlCommand(query,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
con.Close();
}


protected void excel_generation_Click(object sender, EventArgs e)
{
try
{
Export_Excel(ds);

}
catch (Exception ex)
{
throw ex;
}
}

public void Export_Excel(DataSet ds)
{

Microsoft.Office.Interop.Excel.Application objXL = null;
Microsoft.Office.Interop.Excel.Workbook objWB = null;
try
{

objXL = new Microsoft.Office.Interop.Excel.Application();
objWB = objXL.Workbooks.Add(ds.Tables.Count);
int sheetcount = 1;

foreach (System.Data.DataTable dt in ds.Tables)
{

Microsoft.Office.Interop.Excel.Worksheet objSHT = (Microsoft.Office.Interop.Excel.Worksheet)objWB.Sheets.Add();

objSHT.Name = "SheetData" + sheetcount.ToString();
for (int j = 0; j < dt.Rows.Count; j++)
{
for (int i = 0; i < dt.Columns.Count; i++)
{

if (j == 0)
{
objSHT.Cells[1, i + 1] = dt.Columns[i].ColumnName.ToString();
}

objSHT.Cells[j + 2, i + 1] = dt.Rows[j][i].ToString();
}
}

sheetcount++;
}

objWB.Saved = true;
objWB.SaveCopyAs("C:\\" + Guid.NewGuid().ToString() + ".xlsx");

objWB.Close();

objXL.Quit();

}
catch (Exception ex)
{
objWB.Saved = true;
objWB.Close();
objXL.Quit();
Response.Write("Illegal permission");
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
Posted

Don't use interop in ASP.net websites it is not meant for that, try this component instead : http://epplus.codeplex.com/[^]
 
Share this answer
 
Comments
Member 10918596 17-Feb-15 2:49am    
any other way for multiple Dataset to multiple Excel sheets using interop.excel generation in C#.net
I would guess that ds in the code bellow is nothing

C#
protected void excel_generation_Click(object sender, EventArgs e)
{
try
{
Export_Excel(ds);

}
catch (Exception ex)
{
throw ex;
}
}


You're using ds without instantiating it in the method.

The question is badly posed, you're not saying which line throws the error and besides, this is one of the easiest bug to detect and resolve. Put a breakpoint into your code and go line by line until you find which object is not instantiated.

If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
Comments
Member 10918596 17-Feb-15 2:49am    
any other way for multiple Dataset to multiple Excel sheets using interop.excel generation in C#.net
Sinisa Hajnal 17-Feb-15 4:23am    
You don't need another way, you only need to give this export function valid dataset. So, change getquery to function returnin dataset and call it in button click event like DataSet ds = getquery(); Export(ds); There is no need to call getquery in page load since you're not using it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900