Archive | ASP.NET RSS feed for this section

C# – EpochTime

Snipplet code to convert current date time to epoch time
you may go to http://www.epochconverter.com/ to check the result.

public long GetEpochTime()
        {
            DateTime dtCurTime = DateTime.Now;
            DateTime dtEpochStartTime = Convert.ToDateTime("1/1/1970 00:00:00 AM");
            TimeSpan ts = dtCurTime.Subtract(dtEpochStartTime);

            long epochtime;
            epochtime = ((((((ts.Days * 24) + ts.Hours) * 60) + ts.Minutes) * 60) + ts.Seconds);
            return epochtime;
        }

Check File Exist from Remote Server

Some snippet code for checking file exists from remote server using WebClient.

        private bool RemoteFileExists(string url)
        {
            bool bResult = false;
            using (WebClient client = new WebClient())
            {
                try
                {
                    client.UseDefaultCredentials = true;
                    Stream stream = client.OpenRead(url);
                    if (stream != null)
                        bResult = true;
                    else
                        bResult = false;
                }
                catch
                {
                    bResult = false;
                }
            }
            return bResult;
        }

Incoming search terms:

ASP.NET Large File Download Error

If you have an ASP.NET application which allow users to download files from the system and these files size not in small size. Each file more than 50mb, for instance.

When an user click on the file to download, your application will prompt an error in the middle of downloading a file. Wondering what happen? Perhaps you need to check your code.

Response.Flush(); <- this is very important line. Must include this.

Below is a complete code of downloading a file.

Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.ContentType = "Application/octet-stream";
Response.Flush();
Response.BinaryWrite((byte[])objCustomInfo.File_Attachment);
Response.End();

Incoming search terms:

mailto in GridView

An application required to have Microsoft Outlook launch after click the user’s email.

Here the snippet code from gridview,

<asp:TemplateField HeaderText="Email" SortExpression="Email">
<ItemTemplate>
<asp:HyperLink ID="hylinkEmail" runat="server" NavigateUrl='<%#(Eval("Email", "mailto:{0}"))%>'>
<%#DataBinder.Eval(Container.DataItem, "Email")%>
</asp:HyperLink>
</ItemTemplate>
<ItemStyle />
</asp:TemplateField>

extending Enum

Today I m faced a problem when I want to re-use Enum without major change on the based code.

Here the simple example,

I got a drop down list which bind Enum, here the sample code:

public enum Gender
{
[Description("I m a Male")] M,
[Description("I m a Female")] F
}

if you just bind the Enum, your dropdownlist will simply show you “M” and “F”, what if I want to show “I m a Male” and “I m a Female”?

OK, here the trick. (I will put the Author URL once I found, I m forgot the link)

in PageLoad or Code Behind,

using System.ComponentModel;
using System.Reflection;

public static string GetDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description :  value.ToString();
}

Here you will see gender.ToString(“d”), it will return 0 or 1, if you simply put ToString(), it will return M or F

foreach (Gender gender in Enum.GetValues(typeof(Gender)))
{
this.ddlGender.Items.Add(new ListItem(GetDescription(gender ),  gender.ToString("d")));
}

Hope this Help

output param using SqlHelper.ExecuteNonQuery

in case you develop the DNN module using SqlHelper.ExecuteNonQuery which involve parent and child table, and you wish to grab the parents identity for child table manipulation. It always return null, so please use another API which is SQLHelper.ExecuteScalar(),

when you want to return the output param in sp! remember to CAST it as integer! Or else the value return will become “NULL”.

SELECT CAST(scope_identity() as int) ;

Incoming search terms:

ASP.NET RadioButtonList bind enum & display direction

If you wish to bind a simple data such as “Inactive” & “Active” using the asp.net radiobuttonlist, you can try out this way.
C# Code,

public enum Status
{
active,
inactive
}
RadioButtonList1.DataSource = Enum.GetValues(typeof(status));
RadioButtonList1.DataBind();

ASPX page,

<asp:radiobuttonlist runat="server" id="RadioButtonList1">
</asp:radiobuttonlist>

How to display the radiobutton horizontal? usually the radionbutton will display vertical, so simply add RepeatDirection=”Horizontal” inside the radionbuttonlist tag

simple example as below,

<asp:RadioButtonList ID="rbtnStatus" RepeatDirection="Horizontal" runat="server"></asp:RadioButtonList>
Related Posts with Thumbnails

Incoming search terms:

Get Adobe Flash player