Posts

Showing posts from January, 2016

Asp.Net MVC: With Product example 1

Web.config  - Add the connection string <connectionStrings>     <add name="ProductDBConnectionString" connectionString="Data Source=LK-PRK\SQL2012DEV;Initial Catalog=ProductDB;Integrated Security=True"         providerName="System.Data.SqlClient" />   </connectionStrings> Connections.cs - in Model section using System.Configuration; using System.Data.SqlClient; namespace TestStock.Models {     public class Connection     {         public static SqlConnection GetConnection()         {             var conn = ConfigurationManager.ConnectionStrings["ProductDBConnectionString"].ConnectionString;             return new SqlConnection(conn);         }     } } Products.cs  - in Models section namespace TestStock.Models {     public class Product     {         public int Id { get; set; }         public string Name { get; set; }         public double Price { get; set; }     } } ProductHelper.cs - in M

.Net Better structure : With Product sample - Insert Data from JavaScript (3)

In this post we are going to call C# function using jQuery Ajax. jQuery Ajax help to send client side request without doing a post back. This allow to trigger an asynchronous (you can do synchronous as well, but what is the point ?) Add the jQuery libry to your page header. Find this from Google or Microsoft CDN. I'll take from Google CDN. https://developers.google.com/speed/libraries/#jquery Create a JavaScript file under folder called Scripts and name that file as Script.js. Add the following script to that file. Using this script file we are going to call a server side method which is written in C#. Script.js function AddProduct() {     var name = document.getElementById('txtProductName').value;     var price = document.getElementById('txtProductPrice').value;     if (name == "") {         alert("Enter product name !");     }else if (price == "") {         alert("Enter product price !");     }else if

.Net Better structure : With Product sample - Insert Data from C# (2)

Hi all, this is the 2nd post of this series. In this post I'll try to explain how to insert data to a database while keeping a better structure in your Visual Studio ASP.Net application. First lets create this (or similar to this) Stored Procedure in your database to add products. AddProducts Stored Procedure SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE AddProducts @name varchar(50), @price decimal(8,2) AS BEGIN SET NOCOUNT ON; INSERT INTO Products VALUES (@name, @price); END GO Inside the Domain folder of your project, create the following class. Product.cs namespace TestWeb.Domains {     public class Product     {         public int Id { get; set; }         public string Name { get; set; }         public double Price { get; set; }     } } Under the DomainHelpers folder, inside ProductHelper class add this code. ProductHelper.cs public string AddProduct(Product product)         {             string message;        

.Net Better structure : With Product sample - Get Data (1)

Hi all, in this series of blog posts I'll explain how to create ASP.Net web applications using a better structure. This is the part 1 of this blog series, so in this post I'll try to explain how to retrieve data from database. First go tot the Visual Studio and create a ASP.Net Empty Web Application project. Give a suitable name (I'll be using TestWeb). Create database and tables with some sample data. Now lets connect to that database using a connection string. Web.config <?xml version="1.0"?> <!--   For more information on how to configure your ASP.NET application, please visit   http://go.microsoft.com/fwlink/?LinkId=169433   --> <configuration>     <connectionStrings>         <add name="ProductDBConnectionString" connectionString="Data Source=LK-PRK\SQL2012DEV;Initial Catalog=ProductDB;Integrated Security=True"             providerName="System.Data.SqlClient" />     </connection