Archive

Archive for October, 2010

Get Page Name from URL

October 30, 2010 4 comments

Hi all,

try this example to Get Page Name from URL

protected void Page_Load(object sender, EventArgs e)
{
        string url = Request.Url.AbsolutePath.ToString();
        string[] splitUrl1 = url.Split('/');
        string pagename = splitUrl1[splitUrl1.Length - 1].ToString();
        if (pagename == "Test.aspx")
        {
            //do something
        }
}

and there is another way to get Page Name

string[] pagename_aspx = Page.GetType().Name.ToLower().Split(new char[] { '_' });
string PageName = pagename_aspx[pagename_aspx.Length - 2];

Hope this helps

Good Luck

Categories: ASP.Net

Select Multiple Rows in One Row SQL Server

October 9, 2010 Leave a comment

Hi all,

try this example to combine multiple rows into one


declare @Table Table
(
[Numbers] varchar(40)
)
INSERT INTO @Table VALUES('One');
INSERT INTO @Table VALUES('Two');
INSERT INTO @Table VALUES('Three');
INSERT INTO @Table VALUES('Four');
INSERT INTO @Table VALUES('Five');

-- Query to combine multiple rows into one
DECLARE @str VARCHAR(100)
SELECT @str = COALESCE(@str + '|', '') + [Numbers]
FROM @Table
Print @str

Hope this helps

Good Luck

Categories: SQL Server

Log Any Error in Website

October 3, 2010 Leave a comment

Hi all

I’m going now to give example to show how to log any error can be accoured in website like any Exception in txt file

First in root of my website i should create Folder for example i’ll create Error folder which will contain txt files which will be created if any Error accoured in my website and don’t forget to give that folder premission for Rad/Write

Second thing Create Class called ErrHandler.cs which have the following code :-


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Globalization;
public class ErrHandler
{
    private static string IsErrorLogEnabled = System.Configuration.ConfigurationManager.AppSettings["EnableErrorBLog"];
    /// Handles error by accepting the error message
    /// Displays the page on which the error occured
    public static void WriteError(string errorMessage)
    {
        if (IsErrorLogEnabled == "True")
        {
            try
            {
                string CurrentDay = System.DateTime.Now.Day.ToString();
                string CurrentMonth = System.DateTime.Now.Month.ToString();
                string CurrentYear = System.DateTime.Now.Year.ToString();
                string LogFileName = CurrentDay + "-" + CurrentMonth + "-" + CurrentYear + ".txt";
                string path = "~/Error/" + LogFileName;
                if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
                {
                    File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
                }
                using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
                {
                    w.WriteLine("\r\nLog Entry : ");
                    w.WriteLine("{0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
                    string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() +
                                  ". Error Message:" + errorMessage;
                    w.WriteLine(err);
                    w.WriteLine("______________________________");
                    w.Flush();
                    w.Close();
                }
            }
            catch (Exception ex)
            {
                WriteError(ex.Message);
            }
        }
    }
}

and In Web.Config i’ll add line to Enable and Disable Error Logging

<appSettings>
  <add key="EnableErrorBLog" value="True"/>
 </appSettings>

and to use this call try make your Code between Try and Catch block


try
 {
      //-- any code here
  }
catch (Exception ex)
 {
     WriteError(ex.Message);
}

Hope this helps

Good Luck.

Categories: ASP.Net

Pass Multiple values in CommandArgument and Get them

October 3, 2010 Leave a comment

Hi all,

try this example to Multiple values in CommandArgument and get these values with Gridview control

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false">
    <Columns>
        <asp:TemplateField>
        <ItemTemplate>
         <asp:Button ID="Button1" runat="server" Text='<%#Eval("CustomerID")%>'
          CommandArgument= '<%#Eval("PostalCode") + "," + Eval("City") + "," + Eval("Country") %>' OnClick = "Button1_Click" />
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

so now to get these values you need to do the following :-

protected void Button1_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    string[] CommandArgument = btn.CommandArgument.Split(',');
    string CommandArgument1 = CommandArgument[0];
    string CommandArgument2 = CommandArgument[1];
    string CommandArgument3 = CommandArgument[2];
}

Hope this helps

Good Luck

Categories: ASP.Net