Click here to Skip to main content
15,887,886 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi guys

I have a query that retrieves data from the database through a Storedproceduer. The data is then stored into a DataTable and then to a session and afterwards export to excel through a button click.
My issue is that my table has dozens of NUll values and I want them removed before exporting them to excel.

This is how my code looks like with everything:
DB methode:
public void GetData()
       {
        try
        {
            DataTable dt = new DataTable();
            SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            connection.Open();
            using (SqlCommand sqlCmd = new SqlCommand("DAtabase.dbo.SP_EXPORT", connection)) //Extract data: no cons first and cons after
            {
                sqlCmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd))
                {
                    sqlDa.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        Session["TaskTable"] = dt;
                    }
                }
            }
        }
        catch (Exception)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('No data found, try again');", true);
        }
    }

My Export function:
C#
<pre>protected void Button_Export_DS_Click(object sender, EventArgs e)
    {
        if (dtNew.Rows.Count > 0)
            {
                MemoryStream ms = new MemoryStream();
                int i = 1;
                using (ExcelPackage package = new ExcelPackage(ms))
                {
                    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet" + i++);
                    worksheet.Cells["A1"].LoadFromDataTable(dtNew, true);
                    worksheet.Cells.AutoFitColumns();

                    Response.Clear();
                    package.SaveAs(Response.OutputStream);
                    Response.AddHeader("content-disposition", "attachchment; filename=DS_Export.xls");
                    Response.Charset = "";
                    Response.ContentType = "application/vnd.xls";
                    Response.End();
                }
            }

    }


What I have tried:

I have looked at various solutions through the internet without success, I hope someone can help with this.
It's not possible to change the SQL code, since it's working with a pvt function which is based on dates!
Posted
Updated 4-Apr-18 1:37am
v2

Change your stored procedure to replace nulls with reasonable default values.
 
Share this answer
 
Comments
Mish1337 4-Apr-18 7:39am    
It's not possible to change the sql code, since it's based on a pivot function that retrieves data based on time and date
To replace the NULL values in your resultset, you could use the ISNULL() function. For example:
SQL
SELECT [ID], [Name] FROM [People]
would return something like
1 Chris
2 Null
3 Nancy
You could easily change the Null values to the value 'unknown' using the following:
SQL
SELECT [ID], ISNULL([Name], 'unknown') FROM [People]
The result would be
1 Chris
2 unknown
3 Nancy
More on this here[^].
 
Share this answer
 
Comments
Mish1337 4-Apr-18 7:38am    
It's not possible to change the sql code, since it's based on a pivot function that retrieves data based on time and date

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