using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data.OleDb;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       //username on labels
       userdestra.Text = Context.User.Identity.Name.ToString();
       usersinistra.Text = Context.User.Identity.Name.ToString();
       //grid sorting   
       GridView1.Sort("id", SortDirection.Descending);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //datapoints inserting
        OleDbConnection conn = null;
        OleDbCommand command = null;
        DateTime now;
        string another = WebConfigurationManager.ConnectionStrings["AccessFileName"].ToString();
        now = DateTime.UtcNow;
        now = now.AddHours(+1);
        string sql = "INSERT INTO datapoints ([my_datastream],[data],[ip],[myvalue]) VALUES (@datastream,@data,@ip,@myvalue)";
        try
        {
            conn = new OleDbConnection(another);
            conn.Open();
            command = new OleDbCommand(sql, conn);
            command.Parameters.AddWithValue("@datastream", DropDownList1.Text);
            command.Parameters.AddWithValue("@data", now.ToString());
            command.Parameters.AddWithValue("@ip", Request.UserHostAddress.ToString());
            command.Parameters.AddWithValue("@myvalue", TextBox1.Text);
            int added = 0;
            try
            {
                added = command.ExecuteNonQuery();
                Label2.Text = added.ToString() + " record inserted.";
                Label1.Text = "Date: " + now.ToString() + "<br />With IP: " + Request.UserHostAddress.ToString();
                Image1.Visible = true;
                Image2.Visible = false;
            }
            catch (Exception err)
            {
                Label2.Text = "Error inserting record. <br />";
                Label2.Text += "Error type: " + err.Message;
                Image2.Visible = true;
                Image1.Visible = false;
            }

        }
        finally
        {
            if (conn != null)
            {
                conn.Close();
                Label3.Text = "Connection with db is " + conn.State.ToString();
            }
        }
        GridView1.DataBind();
    }



    protected void Button2_Click(object sender, EventArgs e)
    {
        //datapoints deleting
        OleDbConnection conn = null;
        OleDbCommand command = null;
        DateTime now;
        string another = WebConfigurationManager.ConnectionStrings["AccessFileName"].ToString();
        now = DateTime.UtcNow;
        now = now.AddHours(+1);
        string sql = "DELETE FROM datapoints WHERE ([id] = @id)";
        try
        {
            conn = new OleDbConnection(another);
            conn.Open();
            command = new OleDbCommand(sql, conn);
            command.Parameters.AddWithValue("@datastream", TextBox2.Text);
            int added = 0;
            try
            {
                added = command.ExecuteNonQuery();
                Label2.Text = added.ToString() + " record deleted.";
                Label1.Text = "Date: " + now.ToString() + "<br />With IP: " + Request.UserHostAddress.ToString();
                Image1.Visible = true;
                Image2.Visible = false;
            }
            catch (Exception err)
            {
                Label2.Text = "Error deleting record. <br />";
                Label2.Text += "Error type: " + err.Message;
                Image2.Visible = true;
                Image1.Visible = false;
            }

        }
        finally
        {
            if (conn != null)
            {
                conn.Close();
                Label3.Text = "Connection with db is " + conn.State.ToString();
            }
        }
        GridView1.DataBind();
    }

}