Archive for Ottobre 3rd, 2007

SQL Insert return Identity w/sample proc

Ottobre 3rd, 2007

Description: accepts SqlParameter array, proc name and connection string name.
Returns ID

Link: http://www.codekeep.net/snippets/7d87b789-5951-4999-8362-fb62048a7d0f.aspx

    public int ModifyDataReturnID(SqlParameter[] parameters, string ProcName, string ConnectionStringName)
    {
        SqlConnection cn;
        SqlCommand cmd = new SqlCommand();
        int returnID=0;

        cn = new SqlConnection(ConfigurationManager.ConnectionStrings[ConnectionStringName].ToString());

        try
        {
            cn.Open();
            for (int i = 0; i < parameters.Length; i++)
            {
                SqlParameter sParam = (SqlParameter)parameters[i];
                cmd.Parameters.Add(sParam);
            }
            SqlParameter paramIdOutput = new SqlParameter("@ProductID", SqlDbType.Int, 4);
            paramIdOutput.Direction = ParameterDirection.Output;

            cmd.Parameters.Add(paramIdOutput);

            cmd.Connection = cn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = ProcName;
            cmd.ExecuteNonQuery();
            returnID = (int)cmd.Parameters["@ProductID"].Value;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            cn.Close();
        }
        return returnID;
    }
    /*
     * ALTER PROCEDURE Blah
(
	@ProductTitle varchar(100),
	@CategoryID int,
	@OriginalPrice decimal
)
AS
BEGIN
	SET NOCOUNT ON;

    INSERT Products(CategoryID,
					ProductName,
					Price)
	VALUES (@CategoryID,
			@ProductTitle,
			@OriginalPrice)

	SELECT @ProductID = @@Identity
END
    */

Software Review: Synergy

Ottobre 3rd, 2007

This application allows users to share one keyboard and mouse with multiple computers running at the same time, even if the computers use different operating systems. And the best part? It’s free. By Lee Underwood. 1003

Validation using errorprovider

Ottobre 3rd, 2007

Description: Validating the input in a textbox using the errorprovider.

Link: http://www.codekeep.net/snippets/677b287a-f014-4f41-a13b-a82381d9549e.aspx

        private bool ValidCustomerId() {
            bool status = true;
            if (txtCustomerId.Text == "") {
                errorProvider1.SetError(txtCustomerId, "Please fill in the customerid.");
                status = false;
            }
            else {
                if (txtCustomerId.Text.Length > 0 && txtCustomerId.Text.Length < 11) {
                    errorProvider1.SetError(txtCustomerId, "");
                }
                else {
                    errorProvider1.SetError(txtCustomerId, "Please check the length of the customerid. Max length is 10.");
                    status = false;
                }
            }
            return status;
        } // private bool ValidCustomerId

SQL ExecuteNonQuery takes SqlParameter[], ConnString, proc

Ottobre 3rd, 2007

Description: Calls Non Query, accepts an array of Parameters, connection string variable and proc name. calls a stored procedure to modify the database.

Link: http://www.codekeep.net/snippets/e6f8dec4-d8d4-43e2-831d-405a40a994ab.aspx

public void ModifyData(SqlParameter[] parameters, string ProcName, string ConnectionStringName)
    {
        SqlConnection cn;
        SqlCommand cmd = new SqlCommand();

        cn = new SqlConnection(ConfigurationManager.ConnectionStrings[ConnectionStringName].ToString());

        try
        {
            cn.Open();
            for (int i = 0; i < parameters.Length; i++)
            {
                SqlParameter sParam = (SqlParameter)parameters[i];
                cmd.Parameters.Add(sParam);
            }
            cmd.Connection = cn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = ProcName;
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            cn.Close();
        }
    }

Test string for decimal

Ottobre 3rd, 2007

Description: Tests a string value to make sure it converts to a decimal. Writes to a label if it fails. Change variable names and use.

Link: http://www.codekeep.net/snippets/72fe0d19-3628-46cb-9288-ac077eb63fc0.aspx

 try
            {
                iOriginalPrice = decimal.Parse(sOriginalPrice);
            }
            catch (Exception ex)
            {
                lMessage.Text = "Could not convert " + sOriginalPrice + "to a valid price. Please re-enter";
                return;
            }