Archive for Febbraio, 2008

Tooltip for Dropdownlist in gridview ,it’s in footer template

CodeKeep C# Feed Febbraio 29th, 2008

Description: Tooltip for Dropdownlist in gridview ,it's in footer template

Link: http://www.codekeep.net/snippets/ee9761c9-fbb1-4023-b619-8d64f8d4b422.aspx

// This is written in ---** Page.PreRender += new EventHandler(Page_PreRender); *******

protected void Page_PreRender(object sender, EventArgs e)
    {
        DropDownList _dd1 = (DropDownList)gvServers.FooterRow.Cells[0].FindControl("ddStPlatform");

        for (int i = 0; i < _dd1.Items.Count; i++)
        {
            _dd1.Items[i].Attributes.Add("title", _dd1.Items[i].ToString());

        }
    }


//*******--And below code written in -*-gvServers_DataBound-*--- protected void gvServers_DataBound(object sender, EventArgs e)*********



        DropDownList _dd1 = (DropDownList)gvServers.FooterRow.Cells[0].FindControl("ddStPlatform");

        for (int i = 0; i < _dd1.Items.Count; i++)
        {
            _dd1.Items[i].Attributes.Add("title", _dd1.Items[i].ToString());

        }
    

TouchDown SQL

CodeKeep C# Feed Febbraio 28th, 2008

Description: SQL statements

Link: http://www.codekeep.net/snippets/48e17542-57b4-4ecc-b475-b9e63fb92d12.aspx

select  rtrim(substring('Rama Cahn 345345345345',1,16))

select convert(varchar(8),'01/01/08',3)as Effdate

select replicate('Y',9)

select stuff('0101',3,0,':')	

select HCOVRDEP = case when HCOVRDEP=0 then 'N' else 'Y' end from table

select ltrim(rtrim(' Ram '))

Insert SP end 
	If @@ERROR = 0
			Select '1'
		else
			Select @@ERROR

Avoid Duplicate Wizard Names

CodeKeep C# Feed Febbraio 28th, 2008

Description: Avoid Duplicate Wizard Names

Link: http://www.codekeep.net/snippets/0f4ee0a5-81dc-4eb6-a281-0f31fb196880.aspx

    try
                    {
                        Stack stack = (Stack)HttpContext.Current.Session[WizardConstants.WIZARD_SESSION_STACK];
                        WizardSessionObject old = (WizardSessionObject)stack.Pop();
                    }
                    catch
                    {

                    }

                    Response.Redirect(UrlHelper.ResolveUrl("AddEntryToBatchWizard.aspx"));

ExecuteStoredProcedure

CodeKeep C# Feed Febbraio 28th, 2008

Description: Execute a stored procedure against sql server database with the parameters provided and returns the return value

Link: http://www.codekeep.net/snippets/17c7ab50-760d-482e-9ba4-c3ce69229a48.aspx

static public object ExecuteStored(string NomeStored, string ConnectionString, params SqlParameter[] Ingresso)
{
    System.Data.SqlClient.SqlCommand sql = null;
    System.Data.SqlClient.SqlConnection conn = null;
    object ValRet = null;
    try
    {
        conn = new System.Data.SqlClient.SqlConnection(ConnectionString);
        conn.Open();

        sql = new System.Data.SqlClient.SqlCommand(NomeStored, conn);
        sql.CommandType = CommandType.StoredProcedure;
        sql.Parameters.AddRange(Ingresso);
        sql.CommandTimeout = 0;
        ValRet = sql.ExecuteScalar();
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        sql.Dispose();
        conn.Close();
        conn.Dispose();
    }
    return ValRet;
}

Remove 1 or multiple items from a listbox gracefully (don’t lose place) good for long lists

CodeKeep C# Feed Febbraio 28th, 2008

Description: Removes items from a list box and remembers your approximate placement in the list so if the list if very long you don't lose your place everytime you delete entries. Windows forms.

Link: http://www.codekeep.net/snippets/1d48b438-6ec1-4cf7-9c38-b6bfef4487fd.aspx

        /// <summary>
        /// Removes the selected item(s) from list box.
        /// All seleted items (can be zero, 1 or many) will be removed from the list.
        /// User will be left with a single item in the list selected that will be either the item in
        /// the list immediately preceeeding the first item removed or the first item in list.
        /// </summary>
        /// <param name="targetListBox">The list box to remove selected items from</param>
        private void RemoveSelectItemsFromListBox(ListBox targetListBox)
        {
            if (targetListBox != null && targetListBox.SelectedItems.Count > 0)
            {
                // Remember the item in the list that immediately preceeds the 
                // selected item. After the sleected item(s) are deleted this entry
                // will be made the selected item. This is nice when the user
                // has potentially 100's of entries in thier list and deletes one. It prevents
                // them springing back to the top of the list and losing thier place. 
                object focusItem = null;
                if (targetListBox.SelectedIndex > 0)
                    focusItem = targetListBox.Items[targetListBox.SelectedIndex - 1];

                // Remove each selected item
                while (targetListBox.SelectedItems.Count > 0)
                    targetListBox.Items.Remove(targetListBox.SelectedItem);

                // If your form only lights up the save button on changes in data
        // light it up here
                // SetDataDirty(true);

                // Go to the focus item in the list (or the top item if the focus item isn't available)
                if (focusItem != null && targetListBox.Items.Contains(focusItem))
                    targetListBox.SelectedItem = focusItem;
                else if (targetListBox.Items.Count > 0)
                    targetListBox.SelectedIndex = 0;

            }
        }

Using Reflection to loop thru properties of a given object

CodeKeep C# Feed Febbraio 27th, 2008

Description: Using Reflection to loop thru properties of a given object

Link: http://www.codekeep.net/snippets/c857244e-95df-424f-8a36-2b1bd6141e83.aspx

 // create some entity
  SomeEntity se = new SomeEntity ();
  Type objectType = se.GetType();
  PropertyInfo[] properties =  objectType.GetProperties();
 
   //Reflection rawks!!!!!
   foreach (PropertyInfo property in properties)
   {
      //you can then set the value for the given property here
      property.SetValue(se, someValue, null);
   )

Check for Alpha-Numeric

CodeKeep C# Feed Febbraio 27th, 2008

Description: method to Check for AlphaNumeric

Link: http://www.codekeep.net/snippets/b21ab7f1-c8f1-4e5c-9e00-212522929fdf.aspx

// method to Check for AlphaNumeric.
        public bool IsAlphaNumeric(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }

State Validation

CodeKeep C# Feed Febbraio 27th, 2008

Description: checks if the string is a valid state abbreviation

Link: http://www.codekeep.net/snippets/b6155d90-c6bd-4120-a0de-04a7aa13666f.aspx

/// <summary>
        /// this checks if the string is a valid state abbreviation
        /// </summary>
        /// <param name="state"></param>
        /// <returns>bool</returns>
        public static bool CheckValidState(string state)
        {
            if (String.IsNullOrEmpty(state)) return false;
            string[] arrStates = { "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", 
                "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE",
                "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", 
                "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY" };
            try
            {
                foreach (string s in arrStates)
                {
                    if (state.ToUpper() == s)
                    {
                        //break out found a match
                        return true;
                    }
                }
            }
            catch
            {
                return false;
            }
            //must not have found a match
            return false;
        }

Convert string to “MM/dd/yyyy” format date

CodeKeep C# Feed Febbraio 27th, 2008

Description: Returns true if value can be converted to a "MM/dd/yyyy" format date

Link: http://www.codekeep.net/snippets/48af9342-f5b7-4045-b735-1b146ae31683.aspx

/// <summary>
        /// Returns true if value can be converted to a "MM/dd/yyyy" format date
        /// </summary>
        /// <param name="anyString"></param>
        /// <returns>bool</returns>
        public static bool IsDate(string anyString)
        {
            if (anyString == null)
            { anyString = ""; }

            if (anyString.Length > 0)
            {
                string _dateFormat = "MM/dd/yyyy";

                DateTime tempDate;
                if (DateTime.TryParseExact(anyString, _dateFormat, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out tempDate))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else { return false; }
        }

Check if string is numeric

CodeKeep C# Feed Febbraio 27th, 2008

Description: Checks to see whether or not a given string is made of all numeric chars

Link: http://www.codekeep.net/snippets/ee3611e6-a330-4b89-a50c-b8ac9fe4c326.aspx

 public static bool CheckIfNumeric(string myNumber)
        {
            bool IsNum = true;
            if (String.IsNullOrEmpty(myNumber)) return false;
            for (int index = 0; index < myNumber.Length; index++)
            {
                if (!Char.IsNumber(myNumber[index]))
                {
                    IsNum = false;
                    break;
                }
            }
            return IsNum;
        }

Next »