Thursday, February 19, 2015

Calling a method when a breakpoint is hit automatically (“When Hit”) OPTION

Below is the sample Code.



Now, i want to Use When Hit option to Call     Method3()

Steps to Follows:

First Insert Break point.




Next, Right Click on the BreakPoint and select  When Hit option as shown below.



Following Window is displayed.




Calling the Method3() with curly bracs and Click on Ok Button.

And Run and see the output:







DebuggerHidden attribute To Hide Method From Debugging

When ever you want to hide a method from debugging than you have to using this Attribute

To use this attribute you have to use Namespace: using System.Diagnostics;







When I run, CallMethod1 is called and when I Press F11 the control directly goes to CallMethod2 instead of CallMethod1, Because the Method is Hidden(DebuggerHidden)



Wednesday, February 18, 2015

Save Images in GridView and Display in GridView.

Design the Form as Follows:

    <form id="form1" runat="server">

        <div>
            <table>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="EmpId"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server" MaxLength="3"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text="Ename"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server" Style="margin-top: 0px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label3" runat="server" Text="Emp Image"></asp:Label>
                    </td>
                    <td>
                        <asp:FileUpload ID="FileUpload1" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label4" runat="server"></asp:Label>
                    </td>
                </tr>
            </table>
        </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="FALSE">
            <Columns>
                <asp:BoundField HeaderText="EmpId" DataField="EmpId" />
                <asp:BoundField HeaderText="Ename" DataField="Ename" />
                <asp:TemplateField HeaderText="Image">
                    <ItemTemplate>
                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler.ashx?ImID="+ Eval("EmpId") %>' Height="150px" Width="150px" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

        </asp:GridView>


    </form>








.cs Page


    string sqlc = System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnection"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)

    {
        SqlConnection con = new SqlConnection(sqlc);
        SqlCommand cmd=new SqlCommand();
        Stream fs = FileUpload1.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(fs);
        Byte[] bytes = br.ReadBytes((Int32)fs.Length);
        con.Open();
        cmd.Parameters.AddWithValue("@EmpId", Convert.ToInt32(TextBox1.Text));
        cmd.Parameters.AddWithValue("@Ename", TextBox2.Text.ToString());
        cmd.Parameters.Add("@Eimage", SqlDbType.Binary).Value = bytes;
        cmd.Connection=con;
        cmd.CommandText = "insert into EmpImg(EmpId,Ename,Eimage)values(@EmpId,@Ename,@Eimage)";
        cmd.ExecuteNonQuery();
        con.Close();
        Label4.Text = "Data inserted";
        BindGrid();
    }

    public void BindGrid()

    {
        SqlConnection con = new SqlConnection(sqlc);
        SqlCommand cmd = new SqlCommand("select * from EmpImg", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

Add New Item  


Write the following Code in Handler 


using System;

using System.Web;

public class Handler : IHttpHandler {

    
        public void ProcessRequest(HttpContext context)
        {
            string strcon = System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnection"].ToString();
            string imageid = context.Request.QueryString["ImID"];
            System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(strcon);
            connection.Open();
            System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("select Eimage from EmpImg where EmpId=" + imageid, connection);
            System.Data.SqlClient.SqlDataReader dr = command.ExecuteReader();
            dr.Read();
            context.Response.BinaryWrite((Byte[])dr[0]);
            connection.Close();
            context.Response.End();
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}






Output:





Upload multiple files with ASP.Net new feature in VS 2012

Create a folder with Name Files in the Application.




Design the form as follows:


 <div>

        <asp:FileUpload ID="flupload" runat="server"  AllowMultiple="true"/>
        <asp:Button ID="btnUpload" Text="Upload" runat="server"  OnClick="btnUpload_Click"/>
        <asp:Label ID="lblMessage" runat="server"  />
    </div>



.cs Page





.cs Page.

protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        foreach (HttpPostedFile postedFile in flupload.PostedFiles)
        {
            string fileName = Path.GetFileName(postedFile.FileName);
            postedFile.SaveAs(Server.MapPath("~/Files/") + fileName);
        }
        lblMessage.Text = string.Format("{0} files have been uploaded successfully.", flupload.PostedFiles.Count);
    }
Output:



Click on Browse Button.



Select the Files and Click on Upload.


Click on Upload


After uploading you can see the files in your folder which you have created (Files). Just Refresh the Folder.

Visual Studio Tips

Ctrl-K +C

Press Ctrl and then Press K followed by C (Ctrl K C) (To comment the Code)

Example:




=================================================
To Write Exceptions(try, catch) in part of Code.

Ctrl +K , Ctrl +S    Alt-Shift-Arrow(Right,Left,Bottom,Top)

Here is my Code below, and now i want to Add try, catch block to it.




So, first select the block of code as shown below.




and then Press Ctrl+K first and  followed by Ctrl + S




And, now u can see the code with try and catch block.







===============================================
Ctrl-K +U

Press Ctrl and then Press K followed by U (Ctrl K U) (To uncomment the Code)

Example:



=================================================
Ctrl -K+D

To format the Code.

Select, the Code as shown below.



 and then press Ctrl-K followed by D




Kubernetes

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