Translate

Thursday 4 January 2018

DataTable to JSON using c#, Ado.net

use this code :

Out Put :





used namespace 

using System.Collections.Generic;
using System.Web.Script.Serialization;

using System.Data;




public string DataTableToJSON()
    {
        DataTable dtCustomer = new DataTable();
        dtCustomer.Columns.AddRange(
                              new DataColumn[]
                              { new DataColumn("ID", typeof(int)),
                                new DataColumn("Name", typeof(string)),
                                new DataColumn("City", typeof(string))
                              }
                              );
        // Add value to the related column
        dtCustomer.Rows.Add(1001, "PABITRA BEHERA","Bhubaneswar");
        dtCustomer.Rows.Add(1002, "Arya Kumar", "Cuttack");
        dtCustomer.Rows.Add(1003, "Mr Roket(Braja)", "Nayagard");
        dtCustomer.Rows.Add(1004, "Mr Trupti", "Bhadrak");
     
        JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
        List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
        Dictionary<string, object> childRow;
        foreach (DataRow row in dtCustomer.Rows)
        {
            childRow = new Dictionary<string, object>();
            foreach (DataColumn col in dtCustomer.Columns)
            {
                childRow.Add(col.ColumnName, row[col]);
            }
            parentRow.Add(childRow);
        }
        return jsSerializer.Serialize(parentRow);

    }


Call the function in page load
i.e.
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Response.Write(DataTableToJSON()); 
        }

    }

Monday 1 January 2018

HTML5 required attribute Custom user message

Scenario where I need the Custom Message

Here I used an input control  i.e. to input user as Customer name.

<input type="text" id="txtCustomerName"  required>

For validation  I need show " Customer name is required  !" where as the current input show me an message like "please fill out this field."

So I need to change the default error message to custom error message. My custom error message with tag is as follows.


<input type="text" id="txtCustomerName" required placeholder="Enter Customer name"
 oninvalid="this.setCustomValidity('Customer name is required  !')" oninput="setCustomValidity('')">




Details Code :

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<form >
<input type="text" id="txtCustomerName"  required>
  <br>
  <br>
<input type="text" id="txtCustomerName" 
       required placeholder="Enter Customer name"

 oninvalid="this.setCustomValidity('Customer name is required  !')"
       oninput="setCustomValidity('')">
<br>
  <br>
 <input type="submit" value="SUBMIT">

</form>
</body>
</html>