Saturday, October 29, 2016

HTTP POST vs HTTP GET (GET Request Vs Post Request)


HTTP stands for Hypertext Transfer Protocol. Http is the most common protocol used for the communication between a web server and a client.

For Example, to request a home page of any site, then you would type the url of that site.

GET:

Data is submitted as a part of url.

It is not secure but fast and quick.

It is good when you want user to bookmark page.

Data is limited to max length of query string.

GET method should not be used when sending passwords or other sensitive information.

It use Stack method for passing form variable.



Get Request is generally used to get the data from the server


    • When you click on a hyperlink
    • When Response.Redirect() statement is executed
    • When you type URL in the address bar and hit enter 



Example :  Now i want to submit the data.

Default.aspx



Login.aspx:



Output:

On Clicking on button


In Url you can see the values:








Output Explanation: Default.aspx has username data which is sent using get method to login.aspx (server).
 In the output of second screen shot we can see there is key (username) value(uday) data as query stirng in the URL which is submitted to login.aspx page and the output(Welcome uday) is show in login.aspx page.


POST

Data is submitted in http request body.

It is more secured but slower as compared to GET.

It use heap method for passing form variable

It can post unlimited form variables.

Data is not visible in the url.


Example:

Default.aspx

Login.aspx



Output:





Output Explanation

we can observe the post method did not send any query string like what we have seen in get method



Post Request is generally used to submit data to the server.
  • A post request is generally issued
  • When you click on submit button
  • When AUTOPOST back  is set to true and when a selection in the Drop Down  List is changed

Example 2 :




Now with in the button click we are retrieving the details the user has submitted , and then we are appending the values the user has entered using query string and redirecting to webform2

Output Screen:


When we are typing the URL and hitting Enter this is GET Request

Now I want to check in Fiddler Web Debugger and check the Type of Request





After Filling the Form Values:





And Then Clicking on Submit Btn

On Clicking on Submit Button then We are Posting the Data



Here the data is send to the server as part of the Body.



Note:


In asp.net Web Forms, IsPostBack page  property is used to check if the request is POST or GET.

If IsPostBack Property returns true, then we knows the request is http post. Else the request is GET


GET
POST
Data will be arranged in HTTP header by appending to the URL as query string
Data will be arranged in HTTP message body.
Data is in query string so user can view the data
Not visible to user
Less secured compared to POST method because data is in query string so it will be saved in browser history and web server logs
Bit safer than GET method because data is not saved in history or web server logs
As data is saved in URL so its saves only 2048 bytes data
Can be used for any amount of data
Can be bookmarked
Can’t bookmarked
Hacking will be easy
Hacking is difficult
Only ASCII character data type allowed
No restrictions. Allows binary data also
Caching is possible
No caching



Friday, October 28, 2016

Upload-and-save-insert-multiple-files-to-database using ASP.NET

.
*.aspx
<div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>


*.cs
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindData();
        }
    }


    public void BindData()
    {
        string constr = ConfigurationManager.ConnectionStrings["connecionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "SELECT Id, Name FROM images";
                cmd.Connection = con;
                con.Open();
                GridView1.DataSource = cmd.ExecuteReader();
                GridView1.DataBind();
                con.Close();
            }
        }
    }

Button_click Event

foreach (HttpPostedFile postedFile in FileUpload1.PostedFiles)
        {
            string filename = Path.GetFileName(postedFile.FileName);
            string contentType = postedFile.ContentType;
            using (Stream fs = postedFile.InputStream)
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    byte[] bytes = br.ReadBytes((Int32)fs.Length);
                    string constr = ConfigurationManager.ConnectionStrings["connecionString"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        string query = "insert into images values (@Name, @Type, @Data)";
                        using (SqlCommand cmd = new SqlCommand(query))
                        {
                            cmd.Connection = con;
                            cmd.Parameters.AddWithValue("@Name", filename);
                            cmd.Parameters.AddWithValue("@Type", contentType);
                            cmd.Parameters.AddWithValue("@Data", bytes);
                            con.Open();
                            cmd.ExecuteNonQuery();
                            con.Close();
                        }
                    }
                }
            }
        }
        Response.Redirect(Request.Url.AbsoluteUri);

Output:





Monday, October 24, 2016

Generate Barcode using C Sharp.


Barcode Font

First we should download the Font from the Url which is given below


Once downloaded follow the below steps.

1. Extract the ZIP file.

2. Click and Execute INSTALL.exe file.

Output:





*.aspx


<div>

        <table class="auto-style1">
            <tr>
                <td>
                    <asp:Label ID="lblNumber" runat="server" style="text-align:right" Text="Enter Number"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <asp:Button ID="btnGenerate" runat="server" Text="Generate" OnClick="btnGenerate_Click" style="height: 26px" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                </td>
            </tr>                  
        </table>
        </div>


*.CS


using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            string barCode = txtNumber.Text;
            System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
            using (Bitmap bitMap = new Bitmap(barCode.Length * 40, 80))
            {
                using (Graphics graphics = Graphics.FromImage(bitMap))
                {
                    Font oFont = new Font("IDAutomationHC39M", 16);
                    PointF point = new PointF(2f, 2f);
                    SolidBrush blackBrush = new SolidBrush(Color.Black);
                    SolidBrush whiteBrush = new SolidBrush(Color.White);
                    graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
                    graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
                }
                using (MemoryStream ms = new MemoryStream())
                {
                    bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] byteImage = ms.ToArray();
                    Convert.ToBase64String(byteImage);
                    imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
                }
                PlaceHolder1.Controls.Add(imgBarCode);
            }
        }
    }
}

Query Shortcuts in SQL Server Management Studio


we can create shorcuts in SQL Management Studio. Some of the shortcuts are predefined,

Examples

"Alt + F1" will execute "sp_help"

"Ctrl + 1" will execute "sp_who".

You can create new shortcuts for your own queries

Go to Sql Server Management Studio

Tools --> Options ---> Environment ---> Keyboard ---> Query Shortcuts


For example, you can create a shortcut for:
Syntax
"Ctrl + 3 " : SELECT * FROM  (single space after from keyword)
Use just the  table name in new query and press "ctrl + 3" and query will executed.





Sunday, October 23, 2016

What is the difference between String and string in C#?


Example :

string s = "uday";

String S = "uday";

string is an alias for System.String. So technically, there is no difference. It's like int vs. System.Int32.

string is a c sharp keyword.

It is recommended to use string any time when you're referring to an object.

e.g :  string name = "uday";

Where as use String if you need to refer specifically to the class. e.g.

string firstname= String.Format("Uday" {0}!", name);

This is the style that Microsoft tends to use in their examples

Note: You must have to include a using System when using String, otherwise you get the following error:

The type or namespace name 'String' could not be found (are you missing a using directive or an assembly reference?)

These are the aliases defined:

Alias     CLR type

string   System.String

sbyte    System.SByte

byte     System.Byte

Note:  String represents the System.String class while the string is the keyword for the System.String that we can use to create string objects.

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...