Archive for » November, 2008 «

Wednesday, November 26th, 2008 | Author: admin
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page

    {
        public String connectionString = ConfigurationManager.ConnectionStrings[
          "DefaultDatabase"].ConnectionString;

        protected void Page_Load(object sender, EventArgs e)
        {
            string contentID = "";
            contentID = (string)Request.QueryString["ID"];
            if (contentID != "" && contentID != null)
            {

                LoadControl();
            }
            //ph.Controls.Add(new AnthemText.TextAndImage());
        }

        protected void LoadControl()
        {
            string contentID = Request.QueryString["ID"];
            string layoutID = CommonDBCommands.SelectValue(connectionString, "txtContent", "cntLayoutID", "cntID", Request.QueryString["ID"]);
            string customControl = CommonDBCommands.SelectValue(connectionString, "txtLayout", "layCustomControl", "layID", layoutID);
            string controlReference = CommonDBCommands.SelectValue(connectionString, "txtLayout", "layReference", "layID", layoutID);

            //System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFrom(@"C:\Users\mlecount\Documents\Visual Studio 2008\Projects\WebApplication1\WebApplication1\bin\App_Web_textandimage.ascx.3910acf1.dll");
            System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFrom(Server.MapPath("~/bin/" + controlReference));
            object obj = asm.CreateInstance("AnthemText." + customControl);
            if (obj is UserControl)
            {
                UserControl uc = (UserControl)obj;

                ph.Controls.Add(uc);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            Response.Redirect("~/Default.aspx?ID=" + txtContentID.Text, false);

        }
    }
}

Category: ASP.NET, C#  | Leave a Comment
Saturday, November 22nd, 2008 | Author: admin

 

The Stored Procedure

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON

GO
-- =============================================
-- Author:        <Marcus Le Count>
-- Create date: <22 November 2008>
-- Description:    <Inserts a new record into the Accounts Table>
-- =============================================

ALTER PROCEDURE [dbo].[CreateNewAccount]
    (
    @AccountName nvarchar(50),
    @OpeningBalance money,
    @Balance money,
    @DateCreated datetime,
    @LastUpdate datetime,
    @AccountType int,
    @CreatedBy int
    )
AS
DECLARE @MyIdentity int
BEGIN
    INSERT INTO [accounts]
           ([accountName]
           ,[balance]
           ,[openingBalance]
           ,[dateCreated]
           ,[dateLastUpdate]
           ,[accountType]
           ,[createdBy])
     VALUES
           (@AccountName
            ,@Balance
            ,@OpeningBalance
            ,@DateCreated
            ,@LastUpdate
            ,@AccountType
            ,@CreatedBy)

    SELECT @MyIdentity = SCOPE_IDENTITY()

    SELECT @MyIdentity AS [MyIdentity]

RETURN

END

 

The ASP.NET code

public String CreateNewAccount()
    {
        SqlConnection conn;
        SqlCommand comm;

        string newID;

        //read the connection string from web.config
        string connectionString = ConfigurationManager.ConnectionStrings[
        "DefaultDatabase"].ConnectionString;

        //Initialize connection
        conn = new SqlConnection(connectionString);

        //create command (using stored Procedure)
        comm = new SqlCommand("CreateNewAccount", conn);
        comm.CommandType = CommandType.StoredProcedure;

        //add the command parameters
        comm.Parameters.AddWithValue("@AccountName", System.Data.SqlDbType.NVarChar);
        comm.Parameters["@AccountName"].Value = m_AccountName;
        comm.Parameters.AddWithValue("@OpeningBalance", System.Data.SqlDbType.Money);
        comm.Parameters["@OpeningBalance"].Value = m_OpeningBalance;
        comm.Parameters.AddWithValue("@Balance", System.Data.SqlDbType.Money);
        comm.Parameters["@Balance"].Value = m_Balance;
        comm.Parameters.AddWithValue("@DateCreated", System.Data.SqlDbType.DateTime);
        comm.Parameters["@DateCreated"].Value = m_DateCreated;
        comm.Parameters.AddWithValue("@LastUpdate", System.Data.SqlDbType.DateTime);
        comm.Parameters["@LastUpdate"].Value = m_LastUpdate;
        comm.Parameters.AddWithValue("@AccountType", System.Data.SqlDbType.Int);
        comm.Parameters["@AccountType"].Value = m_AccountType;
        comm.Parameters.AddWithValue("@CreatedBy", System.Data.SqlDbType.Int);
        comm.Parameters["@CreatedBy"].Value = m_CreatedBy;
        try
        {
            //open the connection
            conn.Open();

            //execute the command
            newID = Convert.ToString(comm.ExecuteScalar());
            return newID;
        }
        catch
        {
            return "false";
        }
        finally
        {
            //close the connection
            conn.Close();
            comm.Dispose();
        }

    }

Sunday, November 16th, 2008 | Author: admin
<asp:Repeater ID="RepeaterMenu" runat="server" OnItemDataBound="RepeaterMenu_ItemDataBound">
        <ItemTemplate>
            <asp:Label ID="lblLi" runat="server" Text="Label"></asp:Label>
        </ItemTemplate>
    </asp:Repeater>

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string pageName = Path.GetFileNameWithoutExtension(Page.Request.PhysicalPath);

            //Declare Objects
            SqlConnection conn;
            SqlDataAdapter sqlDA;

            StringBuilder SQL = new StringBuilder();
            SQL.Append(" SELECT displayName, pageName");
            SQL.Append(" FROM menu");

            string connectionString = ConfigurationManager.ConnectionStrings[
             "DefaultDatabase"].ConnectionString;

            //Initialize connection
            conn = new SqlConnection(connectionString);

            try
            {
                conn.Open();

                sqlDA = new SqlDataAdapter(SQL.ToString(), conn);

                DataTable dt = new DataTable();
                sqlDA.Fill(dt);
                conn.Close();

                RepeaterMenu.DataSource = dt;
                RepeaterMenu.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }

        }
    }

    protected void RepeaterMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        string loadedPageName = Path.GetFileNameWithoutExtension(Page.Request.PhysicalPath).ToUpper();

        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            DataRowView row = e.Item.DataItem as DataRowView;

            string pageName = row["pageName"].ToString();
            string displayName = row["displayName"].ToString().ToUpper();

            if (displayName == loadedPageName)
            {
                ((Label)e.Item.FindControl("lblLi")).Text = "<li class='activeMenu'><a href='" + pageName + "'>" + displayName + "</a></li>";
            }
            else
            {
                ((Label)e.Item.FindControl("lblLi")).Text = "<li><a href='" + pageName + "'>" + displayName + "</a></li>";
            }
        }
    }

Category: ASP.NET, C#  | Leave a Comment
Wednesday, November 05th, 2008 | Author: admin
//Get the start and end of the current week.
       DayOfWeek dayToday = DateTime.Now.DayOfWeek;
       int days = dayToday - DayOfWeek.Monday;
       DateTime startDate = DateTime.Now.AddDays(-days);
       DateTime endDate = start.AddDays(6);

       string fromDate = startDate.ToString("yyyy/MM/dd");
       string toDate = endDate.ToString("yyyy/MM/dd");

Category: ASP.NET, C#  | Leave a Comment
Monday, November 03rd, 2008 | Author: admin

If you need to get the size of the file you are uploading;

int fileSize = FileUpload1.PostedFile.ContentLength;

Category: ASP.NET, C#  | Leave a Comment