Pages

Saturday, 5 October 2019

Vote For Irene Kez Siju - MyCuteBaby Photo Contest

Vote For Irene Kez Siju - MyCuteBaby Photo Contest: Join MyCuteBaby & get yourself engaged with your family & friends, have fun along the way and make the best of memories! also win gift vouchers every month!

Tuesday, 24 January 2012

Getting Time from the Internet

NIST atomic Clock Time - using C#

public static DateTime GetFastestNISTDate() { var result = DateTime.MinValue;    
 // Initialize the list of NIST time servers
        // http://tf.nist.gov/tf-cgi/servers.cgi
        string[] servers = new string[] {
"nist1-ny.ustiming.org",
"nist1-nj.ustiming.org",
"nist1-pa.ustiming.org",
"time-a.nist.gov",
"time-b.nist.gov",
"nist1.aol-va.symmetricom.com",
"nist1.columbiacountyga.gov",
"nist1-chi.ustiming.org",
"nist.expertsmi.com",
"nist.netservicesgroup.com"
};

        // Try 5 servers in random order to spread the load
        Random rnd = new Random();
        foreach (string server in servers.OrderBy(s => rnd.NextDouble()).Take(5))
        {
            try
            {
                // Connect to the server (at port 13) and get the response
                string serverResponse = string.Empty;
                using (var reader = new StreamReader(new System.Net.Sockets.TcpClient(server, 13).GetStream()))
                {
                    serverResponse = reader.ReadToEnd();
                }

                // If a response was received
                if (!string.IsNullOrEmpty(serverResponse))
                {
                    // Split the response string ("55596 11-02-14 13:54:11 00 0 0 478.1 UTC(NIST) *")
                    string[] tokens = serverResponse.Split(' ');

                    // Check the number of tokens
                    if (tokens.Length >= 6)
                    {
                        // Check the health status
                        string health = tokens[5];
                        if (health == "0")
                        {
                            // Get date and time parts from the server response
                            string[] dateParts = tokens[1].Split('-');
                            string[] timeParts = tokens[2].Split(':');

                            // Create a DateTime instance
                            DateTime utcDateTime = new DateTime(
                                Convert.ToInt32(dateParts[0]) + 2000,
                                Convert.ToInt32(dateParts[1]), Convert.ToInt32(dateParts[2]),
                                Convert.ToInt32(timeParts[0]), Convert.ToInt32(timeParts[1]),
                                Convert.ToInt32(timeParts[2]));

                            // Convert received (UTC) DateTime value to the local timezone
                            result = utcDateTime.ToLocalTime();

                            return result;
                            // Response successfully received; exit the loop

                        }
                    }

                }

            }
            catch
            {
                // Ignore exception and try the next server
            }
        }
        return result;
    }

Tuesday, 3 January 2012

Excel data into GridView in asp.net


Export.aspx file
<asp:GridView ID="GridView1"
              runat="server"
              AllowPaging="true"
              PagerSettings-Mode="Numeric"
              PagerSettings-Position="Bottom"
              PagerStyle-Font-Size="Medium"
              PageSize = "10"
              OnPageIndexChanging="GridView1_PageIndexChanging" >
</asp:GridView>

Export.aspx.cs  file


using System.Data.OleDb;

protected void Page_Load(object sender, EventArgs e)
{
GetExcelSheetNames("Path");
}

private void GetExcelSheetNames(string excelFile)
{
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
try
{
DataSet ds = new DataSet();
// Connection String.
String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";Extended Properties=Excel 8.0;";
// Create connection.
objConn = new OleDbConnection(connString);
// Opens connection with the database.
objConn.Open();
// Get the data table containing the schema guid, and also sheet names.
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return;
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
// Add the sheet name to the string array.
// And respective data will be put into dataset table
foreach (DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + excelSheets[i] + "]", objConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
oleda.Fill(ds, "TABLE");
i++;
}
// Bind the data to the GridView
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
Session["Table"] = ds.Tables[0];
}
catch (Exception ex)
{
Response.Write(ex.Message);

}
finally
{
// Clean up.
if (objConn != null)
{
objConn.Close();
objConn.Dispose();
}
if (dt != null)
{
dt.Dispose();
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = ((DataTable)Session["Table"]).DefaultView;
GridView1.DataBind();
}

How to insert values into sql server database in asp.net?


SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\myData.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlCommand cmd = new SqlCommand("insert into tblMyTable(fldEmpId,fldName,fldAddress)values('" + txtEmpId.Text + "','" + txtName.Text + "','" + txtAddress.Text + "')");
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();