1

aspx.cs file

The datatable is generated during runtime by reading values from Excel file using C# . How to pass it to javascript??

protected void btnRead_Click(object sender, EventArgs e)
{
      Upload();  //Upload File Method
      DataTable dt = ReadExcelWithStream(currFilePath); 
    //on button click it reads file path of excel and stores it in a string path 
    //calls the ReadExcelWithStream Method to read the excel File
}

  private void Upload()
        {
            HttpPostedFile file = this.fileSelect.PostedFile;
            string fileName = file.FileName;
            string tempPath = System.IO.Path.GetTempPath();   //Get Temporary File Path
            fileName = System.IO.Path.GetFileName(fileName); //Get File Name (not including path)
            this.currFileExtension = System.IO.Path.GetExtension(fileName);   //Get File Extension
            this.currFilePath = tempPath + fileName; //Get File Path after Uploading and Record to Former Declared Global Variable
            file.SaveAs(this.currFilePath);  //Upload
        }
    private DataTable ReadExcelWithStream(string path)
    {
       bool isDtHasColumn = false;   //Mark if DataTable Generates Column
       StreamReader reader = new StreamReader(path, System.Text.Encoding.Default);  //Data Stream
       while (!reader.EndOfStream)
       {
       string message = reader.ReadLine();
       string[] splitResult = message.Split(new char[] { ',' }, StringSplitOptions.None);  //Read One Row and Separate by Comma, Save to Array
       DataRow row = dt.NewRow();
       for (int i = 0; i < splitResult.Length; i++)
            {
                if (!isDtHasColumn) //If not Generate Column
                {
                    dt.Columns.Add("column" + i, typeof(string));
                }
                row[i] = splitResult[i];
            }
       dt.Rows.Add(row); 
       isDtHasColumn = true;  //Mark the Existed Column after Read the First Row, Not Generate Column after Reading Later Rows
        }
        //method(here coding to read the excel file and stores it in a dt is written)
        ForJs(dt);
        return dt;
    }

[ScriptMethod, WebMethod]
public static DataTable ForJs(DataTable dt)
{
  return dt;
}

aspx

<script type="text/javascript">
    function InsertLabelData() {
        PageMethods.ForJs(onSuccess, onFailure);
    }
    function onSuccess(dt) {
     //attach the table to dhtmlx grid
    }

But the datatable is not passed to the javascript. How to pass the datatable to javascript from c#

Ram
  • 330
  • 1
  • 3
  • 10
  • How is `ReadExcelWithStream` being called? and `ForJs` takes a `DataTable`, from where? – Christian Phillips May 25 '14 at 11:14
  • I called ReadExcelWithStream method on btnRead_Click. and the datatable is generated in ReadExcelWithStream method which I didn't mention as might take lot of space. @christiandev – Ram May 25 '14 at 11:26
  • The more information you provide (within reason), the more chance others have of helping you out. That piece of *information* is key here. – Christian Phillips May 25 '14 at 11:29
  • plz have a review now @christiandev – Ram May 25 '14 at 11:39

1 Answers1

0

You can not simply pass an(y) object to JavaScript. It has to be in a usable format. Like an array or serialized to XML or JSON. Also "DataTables contain lots of additional information which JSON cannot store".

Take a look at how to pass c# datatable to a javascript function or What's the best way to jSON serialize a .NET DataTable in WCF? for more information on this subject.

Community
  • 1
  • 1
Doku-so
  • 346
  • 3
  • 9