Wednesday, August 6, 2014

Create AJAX dropdownlist (select) using jQuery



Given below is an example of AJAX drop down list which fills data by making an AJAX call and also persists data on post back.

1) Create an AJAX handler

public class AjaxHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            if (HttpContext.Current.Request.QueryString["getDDLData"] == "1")
            {
                List<DDLData> values = new List<DDLData>();

                values.Add(new DDLData() { Text = "Option1", Value = "1" });
                values.Add(new DDLData() { Text = "Option2", Value = "2" });
                values.Add(new DDLData() { Text = "Option3", Value = "3" });

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string result = serializer.Serialize(values);

                context.Response.ContentType = "text/json";
                context.Response.Write(result.ToString());
            }
           
        }
        public class DDLData { public string Text; public string Value; }
        public bool IsReusable
        {
            get { return true; }
        }
    }

Make sure that you declare AJAX handler information in web.config. To learn about AJAX call check the AJAX call related articles this blog
  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
    <handlers>
      <add name="ajaxHandler" verb="GET,POST" path="RequestHandler.aspx" type="AJAXAsptest.AjaxHandler" />
    </handlers>
  </system.webServer>

 2) Create a “Select” element in HTML

    <select name="ddlData" id="ddlData">
            <option></option>
</select> 

 3) Make AJAX call to the handler and fill the dropdown list

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script>
        var selectedValue = '<%= Request.Form["ddlData"]!=null?Request.Form["ddlData"]:""%>';
        $.ajax({ url: "RequestHandler.aspx?getDDLData=1",
            success: function (data) {//Get result object from server
                var elementRef = $("#ddlData");
                elementRef.append(new Option("Select", "Select", false, true));
                $.each(data, function () {
                    elementRef.append(new Option(this.Text, this.Value));
                });
                if (selectedValue != '') {
                    elementRef.val(selectedValue);
                }
            }
        });
    </script>

 4) On server side you can get the selected value of dropdown from the “form” data

Response.Write(Request.Form["ddlData"]);




Monday, June 30, 2014

SQL Server – Convert results from query to string and send email from DB

Given below is an example of how to convert data from SQL server into a string and send the results in an email from database. This can be used to send scheduled reports. Also there is no need to create an executable in c# or any other language to send email reports in the below example.

  
/*Create temp table to store data.*/
DECLARE @temp TABLE (emp_id int,emp_name varchar(10))

INSERT INTO @temp (emp_id,emp_name) VALUES(1,'John')
INSERT INTO @temp (emp_id,emp_name) VALUES(2,'Tom')

DECLARE @result VARCHAR(4000)

/*Get results into a string*/
SELECT @result =
       (
       SELECT convert(varchar, emp_id)+','+emp_name+'\n'
       FROM @temp
       FOR XML PATH('')
       )

SELECT @result

/*Send the report to recipients*/
EXEC msdb..sp_send_dbmail @profile_name='TEST',
@recipients='someone@somewhere.com',
@subject='DB Job',
@body_format = 'HTML',

@body= @result

Thursday, May 22, 2014

ASP.NET URL Routing


 For SEO purposes it might be better to do URL routing for public facing asp.net applications. This will make the URLs more readable and parameters can be separated by “/” instead of “?” and “&”.

If you are using ASP.NET 4.0 and IIS 7 or higher it is easy to do URL routing in asp.net application. You have to do only 2 steps to setup URL routing

       1) Create a function to register routes in global.asax

void RegisterRoutes(RouteCollection routes)
        {
            routes.Ignore("{resource}.aspx/{*pathInfo}");

            routes.MapPageRoute(
               "",      // Route name
               "Home",      // Route URL
               "~/Default.aspx" // Web page to handle route
            );
        }
2) Invoke the function from Application_Start method

 void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RegisterRoutes(RouteTable.Routes);
        }

Now any request to http://webiste/Home will be redirected to default.aspx page

Now how to pass parameters? You can add parameters to the route URL as given below

routes.MapPageRoute(
               "About",      // Route name
               "AboutTest/{controller}/{action}/{id}",  // Route URL
               "~/About.aspx", // Web page to handle route
            true,
               new RouteValueDictionary { { "controller", "food" }, { "action", "show" },{"id","myid"} });

The parameters can be accessed from the page using “Page.RouteData.Values”

if (Page.RouteData.Values["controller"] != null)
            {
                Response.Write(Page.RouteData.Values["controller"]);
            }
            if (Page.RouteData.Values["action"] != null)
            {
                Response.Write(Page.RouteData.Values["action"]);
            }


Wednesday, May 21, 2014

C# Sort generic list by column name


Data in DataTable can be sorted using string column name

DataView dv = new DataView(ds.Tables[0], "","SortExpression" + " " + "SortDirection", DataViewRowState.CurrentRows);

For list we cannot pass sort expression and direction in string format. We have to find the column using reflection and then sort the column as given below

PropertyInfo pi = typeof(Employee).GetProperty("[ColumnName]");
lstData.OrderBy(i => pi.GetValue(i, null)).ToList();


Given below is an example in ASP.NET
-------------------------------------------------------------
HTML (Add inside ‘form’ tags)
    <asp:DropDownList runat="server" ID="ddlSortColumn"></asp:DropDownList>
    <asp:DropDownList runat="server" ID="ddlSortDirection">
        <asp:ListItem Text="asc"></asp:ListItem>
        <asp:ListItem Text="desc"></asp:ListItem>
    </asp:DropDownList>
    <asp:Button runat="server" ID="btnReport" Text="Report"
        onclick="btnReport_Click" />
    <br />
    <asp:GridView runat="server" ID="grvData"></asp:GridView>

C# (CodeBehind)
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IList<PropertyInfo> props = new List<PropertyInfo>(typeof(Employee).GetProperties());
                ddlSortColumn.DataSource = props;
                ddlSortColumn.DataTextField = "Name";
                ddlSortColumn.DataValueField = "Name";
                ddlSortColumn.DataBind();

                grvData.DataSource = GetData();
                grvData.DataBind();
            }
        }
        protected void btnReport_Click(object sender, EventArgs e)
        {
            PropertyInfo pi = typeof(Employee).GetProperty(ddlSortColumn.SelectedValue);
            if (ddlSortDirection.SelectedItem.Text == "asc")
            {
                grvData.DataSource = GetData().OrderBy(i => pi.GetValue(i, null)).ToList();
            }
            else
            {
                grvData.DataSource = GetData().OrderByDescending(i => pi.GetValue(i, null)).ToList();
            }
            grvData.DataBind();
        }
        public List<Employee> GetData()
        {
            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee() { Id = 1, Name = "Employee 1", Age = 25 });
            employees.Add(new Employee() { Id = 2, Name = "Employee 2", Age = 35 });
            employees.Add(new Employee() { Id = 3, Name = "Employee 3", Age = 39 });
            return employees;
        }
        public class Employee
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }


Wednesday, February 26, 2014

How to return error messages in AJAX calls

In AJAX calls exception can occur mainly due to 1) communication error 2) server error. We will look at how to handle these exceptions

Given below is a simple AJAX application – both client code and server handler

JS Code
  <script>

        $.ajax({ url: "NewRequestHandler.aspx?val1=10&val2=11",
            success: function (data) {//Get result object from server
                alert(data.Result);
                if (data.Error!= null) {
                    alert(data.Error); //Get error returned by server
                }
            },
error: function (xhr, errorType, exception) { //Triggered by communication error, like http 404  etc
                alert(xhr.responseText);
                return false;
            }
        });

    </script>

Server Code
public class NewRequestHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            //this object will be returned in the response
            NewData data = new NewData();
            try
            {
                int val1 = Int32.Parse(context.Request.QueryString["val1"]);
                int val2 = Int32.Parse(context.Request.QueryString["val2"]);
                data.Result = val1 + val2;
            }
            catch (Exception ex)
            {
                //capture any exceptions and add it to the response object
                data.Error = ex.Message;
            }
            //convert the object to JSON and return
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            context.Response.ContentType = "text/json";
            context.Response.Write(serializer.Serialize(data));
        }
        public bool IsReusable
        {
            get { return true; }
        }
        public class NewData
        {
            public int Result;
            public string Error;
        }
    }

Any error in communicating with the server will be captured by the below line of code in JS

error: function (xhr, errorType, exception) { //Triggered by communication error, like http 404  etc

Errors occurring in server is captured by the C# and returned in the JSON object. JS code can check for this ‘error’ property to display exceptions from server

if (data.Error!= null) {
                    alert(data.Error); //Get error returned by server
                }

Tuesday, January 21, 2014

How to crawl websites in multiple languages using C#

In this article we will create a console app to crawl a webpage and read data using c#. We can use HttpWebRequest and HttpWebResponse also to get response from a webpage but I used httpagilitypack to read data in the example given below. It is much more easier to read the title, header and other html tags using htmlagility.

1)      Create a console application.

2)      Download htmlagilitypack from http://htmlagilitypack.codeplex.com/

3)      Copy htmlagility pack DLL to folder inside the project and add reference to HtmlAgilityPack.dll

4)      Next create a webclient and pass the URL to read to webclient. To read content in all languages (like Chinese, Japanese, Indonesian, Russian etc) make sure that the encoding is UTF8.

Complete code for crawling and reading data from webpage is given below

class Program
    {
        static void Main(string[] args)
        {
            WebClient webclient = new WebClient();
            HtmlDocument htmlDoc = new HtmlDocument();
            htmlDoc.Load(webclient.OpenRead("http://deebujacob.blogspot.com/2013/03/rendering-multiple-series-in-highcharts.html"), Encoding.UTF8);

            Console.WriteLine(htmlDoc.DocumentNode.SelectSingleNode("//title").InnerText);
            Console.WriteLine(htmlDoc.DocumentNode.SelectSingleNode("//body//h1").InnerText);

            Console.ReadLine();

        }
    }


How to store different languages in SQL Server

To store different languages (like Chinese, Japanese etc)  in SQL server the data type of the column should be NVARCHAR.

Also make sure that the stored procedure used to store data also has NVARCHAR parameter. If you are using SQL statement to directly insert data into the table then use "N" before the data.

Example given below


Create table #temp(data nvarchar(100))

INSERT INTO #temp(data) values (N'ハロウィンの飾りつけになっているJVCケンウッド丸の内ショールームに行ってきました。')

SELECT * FROM #temp