Wednesday, January 30, 2019

Display image from Database



Client Side code


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PhotoGridview.aspx.cs"
Inherits="WebApplication3.PhotoGridview" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 150px;
}
.auto-style3 {
width: 220px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style2">
<asp:Label ID="lblimage" runat="server" Text="Please Select an
Image"></asp:Label>
</td>
<td class="auto-style3">
<asp:FileUpload ID="ImageFileUpload" runat="server" />
</td>
<td rowspan="7">
<asp:GridView ID="GridImage" runat="server" AutoGenerateColumns="False"
style="margin-left: 0px">
<Columns>
<asp:TemplateField HeaderText="Images">
<EditItemTemplate>
Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("photo")
%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="70" Width="70"
ImageUrl='<%# Eval("photo") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td class="auto-style3">
<asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click"
Text="Insert" />
</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td class="auto-style3">&nbsp;</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td class="auto-style3">&nbsp;</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td class="auto-style3">&nbsp;</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td class="auto-style3">&nbsp;</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td class="auto-style3">&nbsp;</td>
</tr>
</table>
</div>
</form>
</body>
</html>
}



Server Side code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApplication3
{
public partial class PhotoGridview : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("data source=MDASIFPC\\SQLEXPRESS;database=test;user id=sa;password=sa123");
private void FillPhoto()

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)

{
SqlDataAdapter da = new SqlDataAdapter("select * from photo", con);
DataSet ds = new DataSet();
da.Fill(ds, "photo");
GridImage.DataSource = ds;
GridImage.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
FillPhoto();
GridImage.Visible = false;
}
}
protected void btnInsert_Click(object sender, EventArgs e)
{
if(ImageFileUpload.HasFile!=false)
{
string fname = ImageFileUpload.FileName;
string fpath = "~//images//" + fname;
ImageFileUpload.SaveAs(Server.MapPath(fpath));
con.Open();
string query = "insert into photo values('" + fpath + "')";
SqlCommand cmd = new SqlCommand(query, con);
int i = cmd.ExecuteNonQuery();
con.Close();
if (i == 1)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert",
"alert('Image is inserted successfully in database')", true);
GridImage.Visible = true;
FillPhoto();
}
}
else
{
GridImage.Visible = false;
ClientScript.RegisterStartupScript(this.GetType(), "alert",
"alert('Please Select any image from source')", true);
}
}
}
}


Output will be like this



Thankyou any doubts please put a comment Thankyou

Tuesday, January 29, 2019

Web API vs Asp.Net MVC API

Web API: It is a part of ASP.NET Framework to build and deploy the REST services on HTTP protocol and accessible to wide range of devices.

ASP.NET MVC API: When I am talking about ASP.NET MVC API, It means, it is also an API but creating with ASP.NET MVC application. I mean to say both view and data in the same project.


Web API supports one of the most important features in API world and that is Content Negotiation. Web API decides and return best response format data that is acceptable by client very easily. The data format can be XML, JSON, ATOM or any other format data. But with ASP.NET MVC API, if we want the data in JSON, you need to cast your data.

Web API also supports self-hosting with any ASP.NET application. So, if you think that your client application is not in ASP.NET MVC then you should always prefer to create your API in Web API. 



ASP.NET Web API
public class EmployeeController: ApiController  
{  
    // GET: /Api/Employees/  
    public List < Employee > Get()  
    {  
        return EmployeeMaster.GetEmployees();  
    }  
}  
ASP.NET MVC API
public class EmployeeController: Controller  
{  
    // GET: /Employees/  
    [HttpGet]  
    public ActionResult Index()  
    {  
        return Json(EmployeeMaster.GetEmployees(), JsonRequestBehavior.AllowGet);  
    }  
}  
Standalone Service Layer

As we all know that ASP.NET Web API is part of ASP.NET framework. But the features of the Web API like filters, routing, model binding, etc. are totally different from ASP.NET MVC API because of ASP.NET Web API in System.Web.Http namespace but ASP.NET MVC API resides in System.Web.MVC. So, we can say that Web API is standalone and used with any other application which supports HTTP protocol. 


Which one is best choice?

So, finally as per my opinion the best thing is that Web API is a good choice when you are going to create standalone fully REST Service, but it is your project requirement to return data and represent it on View in same application. Then you should go with ASP.NET MVC API. 

One more reason to choose Web API is that it provides us high performance in low cost as compared to ASP.NET MVC API.

So, finally as per my opinion ASP.NET Web API is the best instead of ASP.NET MVC API. But I suggest you need to try and see which one is better and why.

JWT Token in WebAPI

using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Net; using Syst...