Thursday, July 7, 2016

How to use file upload control in asp.net with update panel

In this article, I am going to explain how to use file upload control inside update panel in asp.net c#. Create a folder with name "Upload" for saving your file at solution explorer.

aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadFileInsideUpdatePanel.aspx.cs" Inherits="UploadFileInsideUpdatePanel" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="smain" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="upmain" runat="server">
    <ContentTemplate>
    <fieldset>
    <legend>Upload File Inside Update Panel</legend>
    <asp:FileUpload ID="flupload" runat="server" />
    <asp:Button ID="bthUplaod" runat="server" Text="Save" onclick="bthUplaod_Click" />
    <asp:Label ID="lblmsg" runat="server" ForeColor="Green"></asp:Label>
    </fieldset>
    </ContentTemplate>
    <Triggers>
    <asp:PostBackTrigger ControlID="bthUplaod" />
    </Triggers>
    </asp:UpdatePanel>
    </form>
</body>
</html>

aspx.cs code

using System;
using System.IO;
 
public partial class UploadFileInsideUpdatePanel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void bthUplaod_Click(object sender, EventArgs e)
    {
        try
        {
            lblmsg.Text = string.Empty;
            if (flupload.HasFile)
            {
                string physicalDirPath = Server.MapPath("~/Upload");
                string filedata = Path.GetFileNameWithoutExtension(flupload.FileName) + Guid.NewGuid() + Path.GetExtension(flupload.FileName);
                string filename = physicalDirPath + "\\" + filedata;
                flupload.SaveAs(filename);
                lblmsg.Text = "Saved Successfully";
            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = ex.Message;
        }
    }
}

Output:




Monday, July 4, 2016

Difference between Asp.Net Web Services and WCF


                 Web service
                         WCF
 It is hosted only in IIS
It is hosted in IIS, WAS(Windows activation service), Windows Service, Self-hosting
WebService attributes are used to define a web service
ServiceContract attributes are used to define a WCF service.
It supports only HTTP, HTTPS protocols.
It support various protocols like HTTP, HTTPS, TCP, Named Pipes etc.
It is slower than WCF
It is faster than web services
Hash Table cannot be serialized
Hash table cab be serialized
It support XML and MTOM message encoding
It support XML, MTOM, Binary message encoding
It does not support multi-threading.
It support multi-threading
It support one-way and request-response service operations.
It supports one-way, request-response and Duplex service operations.
It support XML serializer
It support DataContract serializer

Sunday, June 26, 2016

dot net interview questions for experienced professionals

Last Friday I got call from "India Bulls" for telephonic interview as technical round, so in this article I am sharing question of that interview. Firstly interviewer introduce him self and then asked some question related to c#, ASP. net and SQL. The questions are:

Question 1 What is Managed and Unmanaged code?

Question 2 What is Encapsulation and abstraction and difference between them ?

Question 3 What is Interface and abstract class and difference between them ?

Question 4  What is difference between array and array list ?

Question 5  What is Generic and collection ?

Question 6  How to implement interface when both interface have same method name
For Example

Interface I1{ void show();}
Interface I2{ void show();}
Class Test, I1,I2{}    ?

Question 7  What is http handler?

Question 8  What is difference between Response.Write() and Response.OutPut.Write()?

Question 9  What is difference between ViewState and Session?

Question 10  What is Transaction Management?

Question 11  Type of Temporary Table in SQL?

Question 12 Difference between truncate and delete?

Question 13 Difference between Stuff() and Replace() function in SQL?

Question 14 Which one query executed fast and Why?

Query 1 select count(*)  from table name

Query 2 select Count(Id) from table name

Query 3 select Count(1) from table name


Question 15  How to insert into table though xml?

Question 16  What is static class in c#?

Question 17  Explain constraints in SQL?

Friday, June 24, 2016

Sql combine multiple rows into one string

In this article I am going to explain how to show multi row column value into single row in sql.


Output

Thursday, June 23, 2016

Running sum in sql server

In this article, I am going to explain how to calculate running or cumulative sum in SQL



Output

Calculate hours minutes between two dates in SQL

In this article, I am going to create logic how to calculate hours and minute between two dates in SQL.

DECLARE @date1 DATETIME = '2016-06-23 12:18:03.133';
DECLARE @date2 DATETIME = getdate();
SELECT CAST((DATEDIFF(Minute, @date1, @date2)) / 60 AS VARCHAR(5)) + ' Hrs' + ' ' + RIGHT('0' + CAST((DATEDIFF(Minute, @date1, @date2)) % 60 AS VARCHAR(2)), 2) + ' Min' AS 'TotalTime'


Output

Sunday, June 19, 2016

Image width and height in asp.net c#

In this article, I am going to explain how to find out uploaded image dimension in asp. net c#.

Code of aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:FileUpload ID="fupload" runat="server" />
    <asp:Button ID="btnCheckHeightWidth" runat="server" Text="Check"
            onclick="btnCheckHeightWidth_Click" /><asp:Label ID="lblmsg" runat="server" ForeColor="Green"></asp:Label>
    </div>
    </form>
</body>
</html>

Code of cs page

using System;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnCheckHeightWidth_Click(object sender, EventArgs e)
    {
        System.IO.Stream stream = fupload.PostedFile.InputStream;
        System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
        lblmsg.Text = "Image size is " + image.Width + "x" + image.Height + " px";
    }
}

Image


Output