We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Server side event ExcelExport.Export

Hi, I am implemented a simple Grid that call Data from WCF, that was a normal JSON data.
I can show that data to grid every function on grid seem working well add, edit, delete, excepted the Export function

could you help me please..~~

##here is my screen##
<ej:grid runat="server" id="usersGrid" allowfiltering="true" allowpaging="true" allowresizing="true"
            allowsorting="true" font-names="Phetsarath OT" isresponsive="true" allowsearching="true"
            enableresponsiverow="true" onserveraddrow="usersGrid_ServerAddRow" onservereditrow="usersGrid_ServerEditRow"
            onserverdeleterow="usersGrid_ServerDeleteRow" onserverexcelexporting="usersGrid_ServerExcelExporting">
            <FilterSettings FilterType="Excel" />
            <EditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true" ShowDeleteConfirmDialog="true" EditMode="Dialog"></EditSettings>
            <ToolbarSettings ShowToolbar="true" ToolbarItems="add,edit,delete,update,cancel,search,excelExport,printGrid"></ToolbarSettings>
            <Columns>
                <ej:Column Field="id" HeaderText="ລດ" IsPrimaryKey="true" IsIdentity="true" AllowEditing="true" />
                <ej:Column Field="username" HeaderText="ຊື່ຜູ້ໃຊ້" EditType="StringEdit" AllowEditing="true">
                    <ValidationRule>
                        <ej:KeyValue Key="required" Value="true" />
                    </ValidationRule>
                </ej:Column>
                <ej:Column Field="passwd" HeaderText="ລະຫັດຜ່ານ" EditType="StringEdit" AllowEditing="true">
                    <ValidationRule>
                        <ej:KeyValue Key="required" Value="true" />
                    </ValidationRule>
                </ej:Column>
                <ej:Column Field="rules" HeaderText="ສິດທິ (rw:ຜູ້ບໍລິຫານ, r:ຜູ້ໃຊ້)" EditType="DropdownEdit" AllowEditing="true">
                    <ValidationRule>
                        <ej:KeyValue Key="required" Value="true" />
                    </ValidationRule>
                </ej:Column>
            </Columns>
        </ej:grid>

##and here is my code behide##
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                usersGrid.DataSource = GetgridData();
                usersGrid.DataBind();
            }
        }

        public List<GetUsers> GetgridData()
        {
            List<@GetUsers> gridData = new List<@GetUsers>();
            wcf.parti.Service1 _parti = new wcf.parti.Service1();
            partiDB.RootObject rootObject = new partiDB.RootObject();

            string json_str = _parti.GetUsers("");
            if (json_str == "e0")//code error
            {
                MessageBox.Show(this.Page, "en:code error.\lo: ລະບົບຂັດຂ້ອງຕິດຕໍ່ຜູ້ເບີ່ງແຍ່ງດ່ວນ.");
            }
            else if (json_str == "e1")//no data found
            {
                MessageBox.Show(this.Page, "en:no data found.\lo: ບໍ່ມີຂໍ້ມູນທີ່ຈະສະແດງ.");
            }
            else if (json_str == "e2")//can't connect databbase
            {
                MessageBox.Show(this.Page, "en:can't connect to database.\lo:ບໍ່ສາມາດເຊື່ອມຕໍ່ຖານຂໍ້ມູນໄດ້.");
            }
            else
            {
                rootObject = JsonConvert.DeserializeObject<partiDB.RootObject>(json_str);
                foreach (var vl in rootObject.GetUsers)
                {
                    gridData.Add(new GetUsers(vl.id, vl.username, enCoding.GetDecryptData(vl.passwd), vl.rules));
                }
            }
            return gridData;
        }

        public class @GetUsers
        {
            public int id;
            public string username;
            public string passwd;
            public string rules;

            public GetUsers(int _id, string _username, string _passwd, string _rules)
            {
                this.id = _id;
                this.username = _username;
                this.passwd = _passwd;
                this.rules = _rules;
            }
        }
      
       protected void usersGrid_ServerExcelExporting(object sender, GridEventArgs e)
        {
            try
            {
                ExcelExport exp = new ExcelExport();
                GridExcelExport excelExport = new GridExcelExport();
                excelExport.FileName = "Export.xlsx";
                excelExport.IsTemplateColumnIncluded = true;
                excelExport.IsHideColumnIncude = true;
                excelExport.Excelversion = ExcelVersion.Excel2010;
                excelExport.Theme = "flat-lime";
                exp.Export(usersGrid.Model, (IEnumerable)usersGrid.DataSource, excelExport); << ERROR on this row
            }
            catch (Exception ex)
            {
                MessageBox.Show(this.Page, "en:internal error.\(" + ex.Message + ")");
            }
        }


ERROR MESSAGE

NameValueType
$exception{"Object reference not set to an instance of an object."}System.NullReferenceException

4 Replies

GU Guadalupe February 27, 2019 07:04 PM UTC

I have this problem too.



VN Vignesh Natarajan Syncfusion Team February 28, 2019 08:16 AM UTC

Hi Sengdala, 
 
Thanks for using Syncfusion support.  
 
Query: “Exception thrown while exporting the grid.” 
 
From your query, we understand that you are facing issue while exporting the ejGrid to excel format. We have prepared a sample using your code and we are able to reproduce the reported issue at our end. This is because in the export method Grid dataSource will be null.which is the cause of the issue. While exporting Postback will be sent to server and dataSource must be re binded to Grid. So while exporting Grid dataSource will not empty as per your code example. To overcome the reported issue, kindly rebind the dataSource to grid during postback. 
 
Refer the below modified code example 
 
protected void Page_Load(object sender, EventArgs e) 
        { 
            usersGrid.DataSource = GetgridData(); 
            usersGrid.DataBind(); 
        } 
 
 
Note: Ensure to bind the dataSource to Grid in the postback 
 
Reported issue may occur when dataSource for the columns bound are not defined. We have already discussed this topic in our knowledge base document.    
  
 
 
Please get back to us if you have further queries. 
 
Regards, 
Vignesh Natarajan. 



SE sengdala February 28, 2019 09:17 AM UTC

Hi, thanks for your support
I am edit code to rebind datasource on postback, but still error with the same

Attachment: Screen_Shot_20190228_at_4.08.57_PM.png_905d815a.zip


VN Vignesh Natarajan Syncfusion Team March 1, 2019 08:46 AM UTC

Hi sengdala, 

Thanks for the update. 

We are not able to reproduce the reported issue after re bounding the dataSource in postback while exporting. So, please share the following details so that we may try to replicate the issue on our side and provide the solution as soon as possible. 

  1. Share the code example of grid in both the client side and server side after applying the suggested solution.
  2. Share the stack trace of the exception that you are getting while exporting.
  3. If possible please try to reproduce the issue in the attached sample

We have prepared a sample which can be downloaded from below link 


Regards, 
Vignesh Natarajan 
 


Loader.
Live Chat Icon For mobile
Up arrow icon