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;
        }

Incoming search terms:

  • diary in c#
  • c# converttodatetime epoch
  • Convert ToDateTime() long
  • EpochTime C#
  • getepochtime c#
  • learn c#

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 client file exists
  • Remote Email Exist
  • php check if file exists on remote server
  • php check if file exist remote server
  • php check file exists remote server
  • net webclient file exists
  • httpfile exist check
  • how to check whether the URL exist in the remote server using WebClient in asp net
  • check if file exists on remote server with no share c#
  • check file exists remote server php

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:

  • asp net filedownload
  • asp net stream large file
  • asp net 파일다운로드
  • download large file error in ASP NET

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

Incoming search terms:

  • E-NUM

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:

  • sqlhelper output
  • sqlhelper output parameter
  • executenonquery output parameter
  • sqlhelper executenonquery output parameter
  • sqlhelper executenonquery output
  • how to get the output paramenter sqlhelper in asp net
  • output parameter using sqlhelper
  • sqlhelper out parameters
  • sqlhelper output c#
  • sqlhelper executenonquery output parameter always null

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:

  • sharepoint radiobutton list
  • c# net code to bind data to radiobuttonlist
  • code for active/inactive using radiobuttons in asp net
  • how to bind enum to radio button list in c#
  • list<enum> data binding
  • radiobuttonlist asp net enum
  • radiobuttonlist bind c#
  • set radio button list enum
  • sharepoint 2010 show radio button horizontally
  • sharepoint radio buttons horizontal
Get Adobe Flash playerPlugin by wpburn.com wordpress themes