In this article, we will learn how we can insert data into MongoDB using ASP.NET. We will learn from the basics because I have written this series of articles focusing on beginners. This article gives some basic understanding of how we can insert data into the database using ASP.NET and MongoDB. In my previous articles, we learned about Insert, Delete, Update and Retrieve operations in MongoDB using a C# console application. You can check those from here.
Prerequisites
- Visual Studio
- MongoDB installed
- Robo 3T
- Basic Knowledge of C# ASP.NET
Step 1
Open Visual Studio, create a new project by going to File > New >Project >Web>select ASP.NET Web application and rename it as User Registration.


Step 2
Now, right-click on the project name and add a new Web Form. Rename it as UserRegistration.

Step 3
Now edit the source and add some Bootstrap file references to it in the Title section. Create a form and add the following lines.
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserRegistration.aspx.cs" Inherits="User_Registration.UserRegistration" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>User Registration</title>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" />
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div class="container">
- <div class="row">
- <div class="col-md-12 col-lg-12">
- <div class="col-md-3"></div>
- <div class="col-md-6">
- <h2 style="text-align:center;margin-top:10px;">User Registration</h2>
- <div class="form-group">
- <label for="email">Name:</label>
- <asp:TextBox ID="Txtname" class="form-control" placeholder="Enter Name" runat="server"></asp:TextBox>
- </div>
- <div class="form-group">
- <label for="email">City:</label>
- <asp:TextBox ID="Txtcity" class="form-control" placeholder="Enter City" runat="server"></asp:TextBox>
- </div>
- <div class="form-group">
- <label for="email">Country:</label>
- <asp:TextBox ID="Txtstate" class="form-control" placeholder="Enter State" runat="server"></asp:TextBox>
- </div>
- <div class="form-group">
- <label for="email">Email:</label>
- <asp:TextBox ID="Txtemail" class="form-control" placeholder="Enter email" runat="server"></asp:TextBox>
- </div>
- <div class="form-group">
- <label for="pwd">Password:</label>
- <asp:TextBox ID="Txtpwd" class="form-control" TextMode="Password" placeholder="Enter password" runat="server"></asp:TextBox>
- </div>
- <div class="form-group" style="text-align:center">
- <asp:Button ID="Btnreg" runat="server" CssClass="btn btn-success" Text="Registration" OnClick="Btnreg_Click" />
- </div>
- </div>
- <div class="col-md-3"></div>
- </div>
- </div>
- </div>
- </form>
- </body>
- </html>



Mohammad TabrezPosted Jan 22, 2019, 11:06 AM
Hello Sanwar, can you please tel me how to perform an insertion operation using fileupload control of asp.net i.e., inserting files into mongo using the same sort of example