Skip to main content

Posts

Showing posts from February, 2010

How to Remove duplicate records - Sql-server 2005

Query to remove duplicate records, in this query please add all the columns in GROUP BY clause that are duplicate, it is based on primary key DELETE from TableName WHERE CoumnName1 + CoumnName2 + CoumnName3 IN (select CoumnName1 + CoumnName2 + CoumnName3         FROM TableName         group by CoumnName1 , CoumnName2 ,CoumnName3         having Count(ID)>1) AND ID NOT IN (select MAX(ID) from TableName         WHERE CoumnName1 + CoumnName2 + CoumnName3         IN (select CoumnName1 + CoumnName2 + CoumnName3                 FROM TableName                 group by CoumnName1 , CoumnName2 ,CoumnName3                 having Count(ID)>1)  ...

How to DownLoad a File in ASP.NET ?

To DownLoad a file:- 1. Add a link to page, Like this :-                <a href="Download.ashx?FileName=file.pdf&FilePath=DownLoad/">Download Standing Order Mandate</a >           Where file.pdf = file to download                      DownLoad = Folder name where the file placed at server. 2. Then  Add a Generic Handler to the website/project with the name Download.ashx and add the following code:    public void ProcessRequest(HttpContext context)     {         if (context.Request.QueryString["FileName"] != null && context.Request.QueryString["FilePath"] != null)         {           ...

Date Format, C#

Date Fromat Output dd-MMM-yyyy 15-Feb-2010 dd-MM-yyyy 15-02-2010 dd-MMM-yyyy hh:mm:ss 15-Feb-2010 11:25:56 dd-MMM-yyyy hh:mm tt 15-Feb-2010 11:25 AM dd-MMM-yy 15-Feb-10 MMM dd, yyyy Feb 15, 2010 MMMM dd, yyyy April 15, 2010 Example: DateTime objDt = DateTime.Now; objDt.ToString("dd-MMM-YYYY") = 15-Feb-2010

Trim a string to a specified length, C#

Trim a string to a length, Here         public  string TrimString(string strValue,int Count)         {                        if (strValue.Length > Count)             {                 strValue = strValue.Substring(0, Count - 1) + "..";             }             return strValue;         }

Generate an alphanumeric Random Code, C#

        To generate an alphanumeric Random code          ///         /// Public static function Generate Random Code         /// generate the random code         /// Created By:   Munesh         /// Created Date: Feb 10, 2010         ///         /// string          public static string GenerateRandomCode()         {             int _minLength = 6, _maxLength = 6;             string _charsLCase = "abcdefgijkmnopqrstwxyz";             string _charsUC...

Resize image and save without saving it to temporary file, C#

Upload an image after resizing to specific size without saving it to temporary file.       ResizeImageAndSave(fileImage.PostedFile.InputStream, Width, Height, FileName);         ///         /// Resize image of Weight X Height and save         ///         public static void ResizeImageAndSave(Stream fs, int intW, int intH, string FileName)         {             System.Drawing.Image objImg = Bitmap.FromStream(fs);             int intImgH = objImg.Height;             int intImgW = objImg.Width;             double R = ((double)intImgW / (double)intImgH);  ...

Stored procedure for paging with sorting

CREATE PROCEDURE DataWithPagingAndSorting --Add the parameters for the stored procedure here     @insPageNo SMALLINT = 1,     @insPageSize SMALLINT = 10,     @SortType varchar(128) = 'DT', AS BEGIN     DECLARE @intTotalCount INT,             @intPageCount INT       SET NOCOUNT ON    SELECT @intTotalCount = COUNT(Id) from TableName                             SET @intPageCount = @intTotalCount/@insPageSize;                IF (@intTotalCount%@insPageSize<>0)             SET @intPageCount= @intPageCount+1         IF (@insPageNo>@intPageCount)          ...

Code to Read Files from Directory - C#, ASP.NET

public void ReadFile(strFileName) {       System.IO.DirectoryInfo obj = new DirectoryInfo(Server.MapPath("FolderName/" + strFileName);       FileInfo[] Files = obj.GetFiles();       for (int i = 0; i < Files.Length; i++)      {           string FileName = Files[i].Name;           string FileExtension = FileName.Substring(0, FileName.LastIndexOf("."));           InsertToDB(FileName, FileExtension ); --Function to save in database      } }

Get value of radiobuttonlist, javascript

Call the javascript function at page_load() protected void Page_Load (object sender, EventArgs e) { if (! IsPostBack ) { rbtnList.Attributes.Add(" onc lick ", "javascript:checkOthersOption('" + rbtnList.ClientID + "');"); } } And Add javascript to the aspx page function checkOthersOption(id) { var radioList = document.getElementById(id); var radio = radioList.getElementsByTagName('input'); alert(radio.length); for (var i = 0; i { if(radio[i].type =="radio") { if (radio[i].checked) alert(radio[i].value); } } }

Javascript Date validation function

function checkDate(ddlDay,ddlMonth,ddlYear) { var d=document.getElementById(ddlDay).value; var m=document.getElementById(ddlMonth).value; var y=document.getElementById(ddlYear).value; var yl=1900; // least year to consider var ym=2100; // most year to consider var flag = true; if (m 12) flag=false; if (d 31) flag=false; if (y ym) flag=false; if (m==4 || m==6 || m==9 || m==11) if (d==31) flag=false; if (m==2) { var b=parseInt(y/4); if (isNaN(b)) flag=false; if (d>29) flag=false; if (d==29 && ((y/4)!=parseInt(y/4))) flag=false; } if(flag) { return (true); } else { return (false); } return(true); }

How to Print a Panel

First do the necessary at server then save the control Panel in the session at Page MainPage.aspx protected void btn_Click(object sender, EventArgs e) { Session["Ctrl"] = pnlContainer;//Panel ID Response.Redirect("print.aspx"); } Create a new page with the print.aspx, Html Code as follows: Add Content PlaceHolder to html with name ph "<asp:PlaceHolder ID="ph" runat="server" /&gt And print.aspx.cs code is follows: protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (Session["Ctrl"] != null && Session["Ctrl"].ToString() != "") Ph.Controls.Add((Panel)Session["Ctrl"]); } else { Response.Redirect("MainPage.aspx"); } }

How to Avoid Sql-injection :-

Principle Implementation Never trust user input Validate all textbox entries using validation controls, regular expressions, code, and so on Never use dynamic SQL Use parameterized SQL or stored procedures Never connect to a database using an admin-level account Use a limited access account to connect to the database Don't store secrets in plain text Encrypt or hash passwords and other sensitive data; you should also encrypt connection strings Exceptions should divulge minimal information Don't reveal too much information in error messages; use customErrors to display minimal information in the event of unhandled error; set debug to false

How to get the readonly textbox value on the server

The ReadOnly TextBox will ignore the submitted text , this change due to security reasons . For example you want to use the textbox and you don't want to let the user to change the textbox manually , now the problem is that the selected value will not be accepted by the textbox when the user submit the form , because the textbox is in readonly mode , in this case you need to manually set the submitted textbox value , you can easily use the values that is submitted via form collection , something like this: Protected void Page_Load( object Sender, EventArgs e) { TextBox1.Text = Server.HtmlEncode(Request.Form(TextBox1.UniqueID)) }