Wednesday, 30 December 2015

Create and save excel file using StringBuilder in server folder using asp.net c#

public void Save_stringBuilder()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<table border='1' width='800' align='center' style='text-align:center'><tr><td colspan='3' style=' background-color:#000; height:100; color:#fff; font-size:x-large'>Header</td></tr><tr><td height='100' align='center'><img src='C:\\Users\\Krishna\\Desktop\\SaveGridViewAsExcelOnDisk\\a.gif' width='70' height='80'>&nbsp;</td><td>&nbsp;</td><td><img src='C:\\Users\\Krishna\\Desktop\\SaveGridViewAsExcelOnDisk\\b.gif' width='70' height='80'></td></tr><tr><td>&asldkfj;laksjdk fj;k as askdjflajksdf;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&asdkf;ajsdlkfj asdjfljasd faskldjflkas;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&asdkjfflkasjd aksdjfl;jalsd skjdflkkjasd fas;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td  height='100' align='center'><img src='C:\\Users\\Krishna\\Desktop\\SaveGridViewAsExcelOnDisk\\b.gif' width='70' height='80' /> </td><td>&nbsp;</td></tr></table>");

        string path = Server.MapPath("~/Files/");
        if (!Directory.Exists(path))  // create folder if not exist
        {
            Directory.CreateDirectory(path);
        }
        using (StringWriter sw = new StringWriter(sb))
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                StreamWriter writer = File.AppendText(path + "SavestringBuilder.xls");
                hw.BeginRender();             
                string html = sb.ToString();
                writer.WriteLine(html);// write file on server folder
                writer.Close();
            }
        }


    }

Check Upload Image size and height width using asp.net c#


protected void Btn_Upload(object sender, EventArgs e)
    {
        System.Drawing.Image img = System.Drawing.Image.FromStream(Upload_img.PostedFile.InputStream);
        int height = img.Height;
        int width = img.Width;
        decimal size = Math.Round(((decimal)Upload_img.PostedFile.ContentLength / (decimal)1024), 2);
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alert", "alert('Size: " + size + "KB\\nHeight: " + height + "\\nWidth: " + width + "');", true);
      

    }

Monday, 14 December 2015

Gett all column names of table Target_Table with comma separated (,) by commas



DECLARE @SQL    nvarchar(4000) = ''
SELECT @SQL += '['+COLUMN_NAME+']' + ', ' FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Employee'

select SUBSTRING(@SQL,0,len(@SQL))


result-

[EmployeeID], [OldID], [Contract_Type], [EmployeeCode]

Thursday, 3 December 2015

How to set radiobuttonlist selected value and clear on check box changed event using jquery in asp.net



// HTML

<asp:CheckBox ID="CheckBox1" runat="server" Font-Bold="True" Text="Select" onchange="return SetRadios();"></asp:CheckBox>


<asp:RadioButtonList ID="rdBtnSHG" runat="server" Width="378px" RepeatDirection="Horizontal">
             <asp:ListItem Value="1">Add new</asp:ListItem>
              <asp:ListItem Value="2">Add+Update</asp:ListItem>

  </asp:RadioButtonList>






// JS
function SetRadios() {
      
            if ($('#<%=cbSHG.ClientID%>').is(":checked")) {
                $('[id*=rdBtnSHG]  :radio[value="1"] ').prop('checked', true);
            }
            else {
                $("#<%= rdBtnSHG.ClientID %> input[type=radio]").prop('checked', false);
            }           
             return false;

    }

Tuesday, 1 December 2015

Export to excel from using stringbuilder in asp.net c#

// HTML

<div>
        <div id="dvInfo" runat="server">
            <br />
            <center>
                <b>Student Informations</b>
             </center>
            <br />
            <table align='center' border='1' bordercolor='#00aeef' width='50%' class='reporttable1'
                cellspacing='0' cellpadding='0' style='font-size: 10;'>
                <tr align='center'>
                    <td style='width: 10%'>
                        <b>RollNo</b>
                    </td>
                    <td>
                        <b>Name</b>
                    </td>
                    <td>
                        <b>City</b>
                    </td>
                </tr>
                <tr>
                    <td>
                        A1001
                    </td>
                    <td>
                        Raja
                    </td>
                    <td>
                        IIT Kanpur
                    </td>
                </tr>
                <tr>
                    <td>
                        A1002
                    </td>
                    <td>
                        Mohit
                    </td>
                    <td>
                        ITI Gonda
                    </td>
                </tr>
            </table>
            <br />
            <br />
        </div>
        <center>
            <asp:Button ID="Button2" runat="server" Style="height: 30px;" OnClick="Button2_Click"
                Text="Export HTML to Excel" />
        </center>

    </div>

// ASPX.cs

protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {

        StrExport.Append(@"<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'><head><title>Report</title>");
        StrExport.Append(@"<body lang=EN-US style='mso-element:header' id=h1><span style='mso--code:DATE'></span><div class=Section1>");
        StrExport.Append("<DIV  style='font-size:12px;'>");
        StrExport.Append(dvInfo.InnerHtml);
        StrExport.Append("</div></body></html>");
        string strFile = "Reporst.xls";
        string strcontentType = "application/excel";
        Response.ClearContent();
        Response.ClearHeaders();
        Response.BufferOutput = true;
        Response.ContentType = strcontentType;
        Response.AddHeader("Content-Disposition", "attachment; filename=" + strFile);
        Response.Write(StrExport.ToString());
        Response.Flush();
        Response.Close();
        Response.End();

        }
        catch (Exception ex)
        {
        }
    }

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...