title: How to pass json from c# to mongodb stored js
body: <p>We are trying to post json string from c# to mongodb stored javascript using driver ver 1.8</p>
  
  <p>Here is my js function :</p>
  
  <pre><code>function addCorporate (json) {
      db.Corporate.save(json);
  
     /* obj = JSON.parse(json);
      if(obj.corporateName) {        
          db.Corporate.save(obj);
      }
      */
  }
  </code></pre>
  
  <p>Note: We tried to run it with commented code as well </p>
  
  <p>C# code :</p>
  
  <pre><code>var CorporateData="{\"corporateName\":\"a\"}";
  
  MongoClient client = new MongoClient(hostedWebConnectionString);
              MongoServer server = client.GetServer();
              MongoDatabase db = server.GetDatabase("myDb");
  
              BsonValue bv = db.Eval("addCorporate", CorporateData);
              BsonValue bv1 = db.Eval(bv.AsBsonJavaScript.Code, CorporateData);
  </code></pre>
  
  <p>The above code gives error as follows:</p>
  
  <pre><code>Command '$eval' failed: exception: Error: can't save a null
  at Error (&lt;anonymous&gt;)
  
  at DBCollection.save (src/mongo/shell/collection.js:486:15)
  
  at _funcs1 (_funcs1:2:18) at src/mongo/shell/collection.js:486 (response: { "errmsg" : "exception: Error: can't save a null\n    at Error (&lt;anonymous&gt;)\n    at DBCollection.save (src/mongo/shell/collection.js:486:15)\n    at _funcs1 (_funcs1:2:18) at src/mongo/shell/collection.js:486", "code" : 16722, "ok" : 0.0 })
  </code></pre>
  
  <p>If we uncommnet the lines in js function and comment the 1st line then we get following error:</p>
  
  <pre><code>"Command '$eval' failed: exception: SyntaxError: Unexpected token u
  at Object.parse (native)
  
  at _funcs1 (_funcs1:4:16) (response: { "errmsg" : "exception: SyntaxError: Unexpected token    at Object.parse (native)    at _funcs1 (_funcs1:4:16)", "code" : 16722, "ok" : 0.0 })            
  </code></pre>
  
  <p>Note: We also tried to parse it as BsonValue i.e </p>
  
  <pre><code>var bsonString = (BsonValue)CorporateData;
  </code></pre>
  
  <p>but still it gives error.</p>
  
  <p>Please suggest any solutions.</p>
  
  <p>Thanks in advance.</p>