Saturday, November 16, 2019

Get records from MongoDB using Asp.net.

To get the records from MongoDB using Asp.net.

using MongoDB.Bson;
using MongoDB.Driver;         
                              
static MongoClient client = new MongoClient("mongodb://localhost:27017"); //Set the connection string

static IMongoDatabase db = client.GetDatabase("test"); //Database name
     
 public void List()
  {

    IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user"); //table name

    var filter = Builders<BsonDocument>.Filter.Empty; //It behaves like select * from user

    var res = collection.Find(filter).ToList();
              
 }

Insert a record into MongoDB using Asp.net.


Below code shows how to insert a record into MongoDB using Asp.net.

First install the MongoDB driver from the nuget package manager.



using MongoDB.Bson;

using MongoDB.Driver; 

static MongoClient client = new MongoClient("mongodb://localhost:27017");//Set the connection string

        
static IMongoDatabase db = client.GetDatabase("test"); //Database name 


        public void InsertCollection()
        {
            BsonDocument doc = new BsonDocument  //Create BSON document object
            {
            { "name" , "me" },
            {"age" , "99"},
            {"addr" , new BsonDocument { {"pin" , "1234"} , {"state","unknown"} }}
            };

          IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user"); //Table name

            collection.InsertOne(doc);

        }

Request format is unrecognized for URL unexpectedly ending in '/'


Add the below code in the Web.config inside the system.web.


              <webServices>
                     <protocols>
                           <add name="HttpGet"/>
                           <add name="HttpPost"/>
                     </protocols>
              </webServices>


Saturday, January 26, 2019

Paging, search & sorting using the datatable.js library


For implementing paging,search & sorting using the datatable.js library.

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">

<table id="table_id" class="display">
    <thead>
        <tr>
            <th>Name</th>
            <th>DOB</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Amar</td>
            <td>10-10-2010</td>
        </tr>
        <tr>
            <td>Akbar</td>
            <td>20-11-2010</td>
        </tr>
    </tbody>
</table>

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>

<script>
    $(document).ready(function () {
        $('#table_id').DataTable();
    });
</script>

Jquery UI Multiple Search MVC



For creating autocomplete multiple select using the jquery UI plugin.

<link rel="stylesheet" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<label>Search fruits:</label>
<input type="text" id="txtFruits" name="txtFruits" />

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script type="text/javascript">
    $(function () {

        function split(val) {
            return val.split(/,\s*/);
        }
        function extractLast(term) {
            return split(term).pop();
        }

        $("#txtFruits")
      .on("keydown", function (event) {
          if (event.keyCode === $.ui.keyCode.TAB &&
              $(this).autocomplete("instance").menu.active) {
              event.preventDefault();
          }
      })
        .autocomplete({
            source: function (request, response) {
                $.getJSON('@Url.Action("data")', {
                    term: extractLast(request.term)
                }, response);
            },
            focus: function () {
                // prevent value inserted on focus
                return false;
            },
            select: function (event, ui) {
                var terms = split(this.value);
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push(ui.item.value);
                // add placeholder to get the comma-and-space at the end
                terms.push("");
                this.value = terms.join(", ");
                return false;
            }
        });
    })
</script>


        /// <summary>
        /// Inside the controller
        /// Don't change the parameter name (term) it's default by jquery ui
        /// </summary>


public ActionResult data(string term)
        {
            var items = new[] { "Apple", "Pear", "Banana", "Pineapple", "Peach" };

            var filteredItems = items.Where(item => item.IndexOf(term, StringComparison.InvariantCultureIgnoreCase) >= 0 );

            return Json(filteredItems, JsonRequestBehavior.AllowGet);
        }

Git Commands

Git Version   To check the git version Git -v       Git Clone To clone the repository, use the following command: Git clone [u...