Thursday, August 7, 2025

How to convert div to pdf using itextsharp asp.net - C# - Simplest and less code

First you have to download itextsharp.dll and itextsharp.pdfa.dll files and  you have to give the font size as below because in some case it will give error that font size is small.

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

        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

    <div id="mainDiv" runat="server">

        

        <asp:Label runat="server" Text="Test" Font-Bold="true" Font-Size="13px" />

        <br />


        <asp:Label runat="server" Text="E-Admit Card" Font-Bold="true" Font-Size="13px" />

        <br />


        <table border="1">

            <tr>

                <td>

                    <asp:Label runat="server" Text="Date Of Test:" Font-Size="13px" />

                </td>

                <td>

                    <asp:Label runat="server" Text="20/11/2024" Font-Size="13px" />

                </td>

            </tr>

        </table>

        

    </div>

        <asp:Button ID="Button1" runat="server" Text="Print" OnClick="Button1_Click" />

</form>


===========================

 protected void Button1_Click(object sender, EventArgs e)

    {

        Response.ContentType = "application/pdf";

        Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");

        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        StringWriter stringWriter = new StringWriter();

        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

        

        mainDiv.RenderControl(htmlTextWriter);

        StringReader stringReader = new StringReader(stringWriter.ToString());

        Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);

        HTMLWorker htmlparser = new HTMLWorker(Doc);

        PdfWriter.GetInstance(Doc, Response.OutputStream);


        Doc.Open();

        htmlparser.Parse(stringReader);

        Doc.Close();

        Response.Write(Doc);

        Response.End();

    }

Tuesday, July 29, 2025

Nupkg extension file from nuget package

 Convert nupkg file to zip file and you will get dll files in it. 


On the right hand side of nuget website you will get "download package" option.

Monday, July 28, 2025

How to print a print statement of sql in asp.net c# - how to call a another event in asp.net event

 using System;

using System.Data.SqlClient;


public class SqlPrintExample

{

    public static void Main(string[] args)

    {

        string connectionString = "Data Source=YourServer;Initial Catalog=YourDatabase;Integrated Security=True";

        string sqlQuery = "PRINT 'This message comes from SQL.'; SELECT GETDATE() AS CurrentDateTime;";


        using (SqlConnection connection = new SqlConnection(connectionString))

        {

            // Attach an event handler to capture InfoMessage events (including PRINT statements)

            connection.InfoMessage += Connection_InfoMessage;

or// conn.InfoMessage += new SqlInfoMessageEventHandler( Connection_InfoMessage);

            try

            {

                connection.Open();

                using (SqlCommand command = new SqlCommand(sqlQuery, connection))

                {

                    using (SqlDataReader reader = command.ExecuteReader())

                    {

                        while (reader.Read())

                        {

                            Console.WriteLine($"Current Date Time from SQL: {reader["CurrentDateTime"]}");

                        }

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine($"Error: {ex.Message}");

            }

        }

    }


    private static void Connection_InfoMessage(object sender, SqlInfoMessageEventArgs e)

    {

        // Display the message from the SQL PRINT statement

        Console.WriteLine($"SQL InfoMessage: {e.Message}");

    }

}

do while loop in sql server - Cursor in Sql Server

For data looping in sql Server, we use While loop  or cursor


 DECLARE @cnt INT = 0;


WHILE @cnt < 10

BEGIN

   PRINT 'Inside simulated FOR LOOP on TechOnTheNet.com';

   SET @cnt = @cnt + 1;

END;


PRINT 'Done simulated FOR LOOP on TechOnTheNet.com';

GO


For cursor:-


https://www.c-sharpcorner.com/blogs/how-to-use-cursors-and-while-loop-in-sql-server

Thursday, July 17, 2025

How to download the file secretly using webclient class -asp.net c#

  using (WebClient client = new WebClient())

        {

            


            string url = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOJA1ZDa4CQ6OwLx-iL10cLEXpzoJZmZUVLHjJqHBr3-Q9TBEBpjD_ZZgtNGMj59CXGWtZ1yu-CfUoyuU8I7nGjgpwk0wCal_oGSPZNREQssIHeC5PFC10v8YdEIe5ie2wLyfGsrHSBR213ATgNXPyotNs66AoYF2CWPfAx9gu2cgYKw/s1600/Screenshot%202025-05-14%20184428.png";

            

            string savePath = "D:/image1212.jpg";

            client.DownloadFile(url, savePath);


        }