Friday 29 June 2012

Validate Words in C#


Text==>u r input Value,,,,,Lenth==>Lenth of words to be check    
 public int ValidateWords(string Text, int Length)
        {
            int result = 0;
            bool lastWasSpace = false;

            foreach (char c in Text)
            {
                if (char.IsWhiteSpace(c))
                {
                    if (lastWasSpace == false)
                    {
                        result++;
                    }
                    lastWasSpace = true;
                }
                else
                {
                    // Count other characters every time.
                    //result++;
                    lastWasSpace = false;
                }
            }

            if (result <= Length && result > 0)
            {
                return 1;
            }
            else if (result > Length)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }

Validate Character in C#


Test-->u r input value,, Lenth---->no of character to validate

     public int ValidateLength(string Text, int Length)
        {
            if (Text.Length <= Length && Text.Length > 0)
            {
                return 1;
            }
            else if (Text.Length > Length)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }

how Get No of StoredProcedure name and count in sqlserver


To Get No of StoredProcedure name and count in sqlserver


USE DBNAME

SELECT * From SYS.PROCEDURES

To Get No of Table name and count in sqlserver


To Get No of Table name and count in sqlserver

USE DBNAME

SELECT * FROM sys.tables

Date Time Difference calculation in c# page, Like second,Hour,day...


private string CalculateTime(DateTime EventTime)
    {
        var ts = new TimeSpan(DateTime.Now.Ticks - EventTime.Ticks);
        double delta = Math.Abs(ts.TotalSeconds);

        const int SECOND = 1;
        const int MINUTE = 60 * SECOND;
        const int HOUR = 60 * MINUTE;
        const int DAY = 24 * HOUR;
        const int MONTH = 30 * DAY;

        if (delta < 0)
        {
            return "not yet";
        }
        if (delta < 1 * MINUTE)
        {
            return ts.Seconds == 1 ? "One second ago" : ts.Seconds + " Seconds ago";
        }
        if (delta < 2 * MINUTE)
        {
            return "a minute ago";
        }
        if (delta < 45 * MINUTE)
        {
            return ts.Minutes + " minutes ago";
        }
        if (delta < 90 * MINUTE)
        {
            return "an hour ago";
        }
        if (delta < 24 * HOUR)
        {
            return ts.Hours + " hours ago";
        }
        if (delta < 48 * HOUR)
        {
            return "last day";
        }
        if (delta < 30 * DAY)
        {
            return ts.Days + " days ago";
        }
        if (delta < 12 * MONTH)
        {
            int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
            return months <= 1 ? "one month ago" : months + " months ago";
        }
        else
        {
            int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
            return years <= 1 ? "one year ago" : years + " years ago";
        }
    }

then use this method assign to label or what ever your control

How to show text in asp textbox when textmode is password

  Create prerender event    Like
 
  <asp:TextBox ID=" textboxID " runat="server" AutoPostBack="True" TextMode="Password"
                                                        OnTextChanged="txtOldPassword_TextChanged"
                                                        OnPreRender=" textboxID_PreRender"></asp:TextBox>


   protected void  textboxID_PreRender(object sender, EventArgs e)
    {
        string strSample = textboxID.Text;
         textboxID.Attributes.Add("value", strSample);
    }

After that it will assign to that corresponding textbox ....

Adding Item to DataBound Drop Down List


            DropdownList.DataSource = Assgn Source;    EX: datatable or dataset or list values
             DropdownList .DataTextField = "Test To Display in ui page";
             DropdownList .DataValueField = "id";
             DropdownList .DataBind();
             DropdownList .Items.Insert(0, new ListItem("--All---", "0"));

Thursday 28 June 2012

First Alert And Redirect to Other page in asp.net


Page.ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp",   "alert('Test completed'); window.location.href = 'YourPage.aspx';", false;

Friday 22 June 2012

Select Top n Rows Using LINQ in asp.net


List<class name> objList = dalHelper.DataLayerClassName.MethodName(argument);            
              return objList = objList.Take(5).ToList();

5===> Here u can give how many numbers u want..

Friday 15 June 2012

Add color code for in C# Page

lblColor.text=system.drawing.translater.fromHtml("color code");

Add Enumeration value to dropdown list in C#

step 1==>Create Enum in class
step ==> Add dropdown list in aspx page
step ==> add below code to .cs page

dropdownlistname.DataSource = Enum.GetValues(typeof(enumClassName.MethodName));
dropdownlistname.DataBind();

Generate Guid in c#

Guid guidNewID = Guid.NewGuid();

Convert String to guid:

string strValue = guidNewID.tostring();

Guid guidValue = new Guid(strValue);

How to generate a random unique 5 digit number + C#

Below is the method used to generate random 5 digit number.

public int getRandomID ()

{

Random r = new Random()

return r.Next(10000,99999);

}

The number 10000, 99999 specifies the range.