Tag Archives: asp.net

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:

DNN Site Map

If your DNN website need a site map, you can find the open source application from [here].

After deploy the DNN SiteMap, the DNN really solve my problem, but I found out the SiteMap’s parent node order is not same with Tab Menu’s Order.

Here the Solution,

  1. download the source code [here]
  2. launch the VS.NET 2005 and open the solution
  3. double click SqlDataProvider.cs
  4. check out 2 methods here – GetRootNodesFromDb() and GetChildNodesFromDb(int parentTabId)
  5. notice the last line of each method which ORDER BY t.TabId
  6. ok, is show time now, replace  ORDER BY t.TabId to ORDER BY t.TabOrder, t.TabName –t.TabId
  7. you can compare both tab menu and sitemap parent node is tally now.

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:

DotNetNuke Module Development

For those who wish to learn or know more about DNN module development but do not know where to start.

Here the link for you to start.

  1. Creating a DNN Module and Understanding DNN Architectural Approach [Click Here]
  2. Creating a DotNetNuke® Module – For Absolute Beginners [Click Here]
  3. Creating a Super-Fast and Super-Easy DotNetNuke® Module – for Absolute Beginners [Click Here]

Before launch your visual studio.NET 2005, please make sure you installed the DNN template & starter kit which you can find it [here], also DNN Source Code Version which you can download from [www.dotnetnuke.com]

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>

SqlHelper.ExecuteScalar in DotNetNuke Modules

To deal with a complex business logic, you may write complex T-SQL in stored procedure. It may return -9, -5, -1, 0, 1, 2…. and so on.

We have to use SqlHelper.ExecuteScalar instead of SqlHelper.ExecuteNonQuery.

Example,

public override long DeleteUser(long callingUserId, long userId)
{
SqlParameter[] paramArray = new SqlParameter[3];
paramArray[0] = new SqlParameter("@piCallingUserId", callingUserId);
paramArray[1] = new SqlParameter("@piDeletedUserId", userId);

paramArray[2] = new SqlParameter("@oiRetVal", 99);

paramArray[2].Direction = ParameterDirection.Output;

SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, GetFullyQualifiedName("User_Del_User"), paramArray);

return long.Parse(paramArray[2].Value.ToString());
}

Incoming search terms:

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