Month: June 2012

On Enter Button click redirect with querystring as textbox value

put your text box and button in panel with default button


   
   

OR

<script>

     functionbutton_click(objTextBox, objBtnID) {
         if (window.event.keyCode == 13) {
             if (objTextBox.value != “”) {
                 document.getElementById(objBtnID).focus();
                 document.getElementById(objBtnID).click();
             }
            
         }
      }
    </script>
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
    <asp:Button ID=”Button1″ runat=”server” Text=”Button” onclick=”Button1_Click” />
Page load:-
this.TextBox1.Attributes.Add(“onkeypress”, “button_click(this,'” + this.Button1.ClientID + “‘)”);
protected void Button1_Click(object sender, EventArgse)
    {
        if (!string.IsNullOrEmpty(TextBox1.Text))
        {
            Response.Redirect(“Default2.aspx?text=”+ TextBox1.Text);
        }
    }

Logging error to text file c#.Net

1. Add new static “ErrorLoging” class to solution and paste the below code in cs file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Diagnostics;

///

/// class contain methode for logging WriteToErrorLogFile_Trace(string)
///

public  static class ErrorLoging
{
    ///

    /// log error to path metion in web.config file for key “ErrorFolderPath”.
    /// new file will be created for each day
    ///

    /// error msg to log.
    /// exception object, Pass null to just log some msg.
    public static void WriteToErrorLogFile_Trace(string msg,Exception ex)
    {
        try
        {
            if (!System.IO.Directory.Exists(System.Configuration.ConfigurationManager.AppSettings[“ErrorFolderPath”] + “\\Errors\\”))
            {
                System.IO.Directory.CreateDirectory(System.Configuration.ConfigurationManager.AppSettings[“ErrorFolderPath”] + “\\Errors\\”);
            }
            //if (!System.IO.File.Exists(Application.StartupPath + “\\Errors\\” + DateTime.Today.Date.ToString().Replace(‘/’, ‘ ‘).Replace(‘:’, ‘ ‘)+”.txt”))
            //{
            //    System.IO.File.Create(Application.StartupPath + “\\Errors\\” + DateTime.Today.Date.ToString().Replace(‘/’, ‘ ‘).Replace(‘:’, ‘ ‘) + “.txt”);
            //}

            FileStream fs1 = new FileStream(System.Configuration.ConfigurationManager.AppSettings[“ErrorFolderPath”] + “\\Errors\\” + DateTime.Today.Year.ToString() + DateTime.Now.ToString(“MM”) + DateTime.Now.ToString(“dd”) + “.txt”, FileMode.Append, FileAccess.Write);
            StreamWriter s1 = new StreamWriter(fs1);

            s1.WriteLine(“==================” + DateTime.Now.ToString() + “===========================” + System.Environment.NewLine);
            s1.WriteLine(msg + System.Environment.NewLine);

            if (ex != null)
            {
                // Get stack trace for the exception with source file information
                var st = new StackTrace(ex, true);
                // Get the top stack frame
                var frame = st.GetFrame(0);

                s1.WriteLine(“Line Number:” + System.Environment.NewLine + frame.GetFileLineNumber() + System.Environment.NewLine);
                s1.WriteLine(“File Name:” + System.Environment.NewLine + frame.GetFileName() + System.Environment.NewLine);
                s1.WriteLine(“Message:” + System.Environment.NewLine + ex.Message + System.Environment.NewLine);
                s1.WriteLine(“Methode:” + System.Environment.NewLine + ex.TargetSite + System.Environment.NewLine);
                s1.WriteLine(“Source:” + System.Environment.NewLine + ex.Source + System.Environment.NewLine);
                s1.WriteLine(“InnerException:” + System.Environment.NewLine + ex.InnerException + System.Environment.NewLine);
                s1.WriteLine(“StackTrace:” + System.Environment.NewLine + ex.StackTrace + System.Environment.NewLine);
            }
            s1.WriteLine(“================================================================” + System.Environment.NewLine);

            s1.Close();
            fs1.Close();
        }
        catch (Exception e)
        {

            throw e;
        }

    }
}

2. add follwing key in web.config <appSettings>
<appSettings>
    <add key=ErrorFolderPath value=d:\ErrorLogFolder/>
  </appSettings>

3. Add below code on your page/user control cs file

///

    /// calls static error log method
    ///

    /// specific developer msg for logging
    /// Exception object
    /// true if want to throw exception to user
    public voidLogError(string msg, Exceptionex,bool showUser)
    {
        ErrorLoging.WriteToErrorLogFile_Trace(msg + System.Environment.NewLine+“Show User:”+showUser, ex);
        if(showUser)
        {
            throwex;
        }
    }

4. Write below in all your catch
try
{
}
catch(Exception ex)
{
 LogError(“error on my code”,ex,false);
}