public class DataBase
{
private string connection_kapt_testes = "Server=xx.xx.xx.xx;Port=5432;Database=xxx;Username=xxx;Password=xxx; Pooling=false";
public static NpgsqlConnection Conecta()
{
NpgsqlConnection Conn = new NpgsqlConnection(connection_kapt_testes);
Conn.Open();
return Conn;
}
public static void Desconecta(NpgsqlConnection npgsqlConnection)
{
if (npgsqlConnection.State == ConnectionState.Open)
{
npgsqlConnection.Close();
}
}
}
Eu mudava era isto para funcionar assim
E no codigo behind assim.
public partial class Logistics_list_of_material : System.Web.UI.Page
{
public string username = HttpContext.Current.User.Identity.Name;//nome do utilizador
// mostra a tabela inteira
protected void Page_Load(object sender, EventArgs e)
{
if (!Request.IsAuthenticated)//não autenticado
{
Response.Redirect("~/Account/Login.aspx");
}
if (!Page.IsPostBack)//autenticado
{
refresh_logistics_table();
//refresh de 5 em 5 segundos
SomeMethod();
//configuração de permissão de escrita e leitura
//DataBase.Conecta();
NpgsqlConnection npgsqlConnection = DataBase.Conecta();
Npgsql.NpgsqlCommand comm = new Npgsql.NpgsqlCommand();
comm.CommandText = " SELECT logistics FROM users where username ILIKE '" + username + "'";
comm.Connection = npgsqlConnection;
Npgsql.NpgsqlDataReader rd = comm.ExecuteReader();
if (rd.Read())
{
if (rd.GetBoolean(0) == false)
{
GridView_logistics.HeaderRow.Cells[5].Visible = false;
GridView_logistics.FooterRow.Cells[5].Visible = false;
for (int i = 0; i < GridView_logistics.Rows.Count; i++)
{
GridViewRow row = GridView_logistics.Rows[i];
row.Cells[5].Visible = false;
}
}
}
DataBase.Desconecta(npgsqlConnection);
}
}
private readonly object syncLock = new object();
public void SomeMethod()
{
lock (syncLock)
{
//refresh de 5 em 5 segundos
Response.AppendHeader("Refresh", "5");
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void refresh_logistics_table()
{
string now_min = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
string now_max = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
//DataBase.Conecta();
NpgsqlConnection npgsqlConnection = DataBase.Conecta();
Npgsql.NpgsqlCommand comm = new Npgsql.NpgsqlCommand();
//mostra os valores na tabela do dia.
comm.CommandText = "SELECT * FROM logistics_request WHERE date_time_request >= '" + now_min + "' AND date_time_request < '" + now_max + "' ORDER BY delivered ASC, date_time_request DESC";
comm.Connection = npgsqlConnection;
Npgsql.NpgsqlDataAdapter myDataAdapter = new Npgsql.NpgsqlDataAdapter();
myDataAdapter.SelectCommand = comm;
DataSet myDataSet = new DataSet();
int user_exist = myDataAdapter.Fill(myDataSet);
Panel_gridview_logistics.Visible = true;
GridView_logistics.DataSource = myDataSet;
GridView_logistics.DataBind();
DataBase.Desconecta(npgsqlConnection);
}
protected void row_paint(object sender, GridViewRowEventArgs e)
{
// quando montar as linhas do tipo DADOS
if (e.Row.RowType == DataControlRowType.DataRow)
{
// pega os campos tipo e propriedade
string delivered = DataBinder.Eval(e.Row.DataItem, "delivered").ToString();
// cores para aprovação
if (delivered == "FALSE")
e.Row.BackColor = Color.LightPink;
if (delivered == "TRUE")
{
// e.Row.Cells[5].Visible = false;
e.Row.Cells[5].Text = "✔";
e.Row.BackColor = Color.LightGreen;
}
}
}
protected void gridview_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView_logistics.RowDeleting += new GridViewDeleteEventHandler(gridview_RowDeleting);
DataBase.Conecta();
Npgsql.NpgsqlCommand comm = new Npgsql.NpgsqlCommand();
comm.Connection = DataBase.Conn;
string id_line = GridView_logistics.DataKeys[e.RowIndex]["line"].ToString();
string id_time = GridView_logistics.DataKeys[e.RowIndex]["date_time_request"].ToString();
var space = id_time.Split(' ');
var date = space[0];
var date2 = date.Split('-');
var day = date2[0];
var month = date2[1];
var year = date2[2];
var time = space[1];
var time2 = time.Split(':');
var hour = time2[0];
var min = time2[1];
var sec = time2[2];
string date_form = year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec;
string delivered = "TRUE";
string now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
comm.CommandText = "UPDATE logistics_request SET delivered='" + delivered + "', delivered_by='" + username + "', date_time_delivery='" + now + "' WHERE date_time_request='" + date_form + "' AND line= '" + id_line + "'";
comm.ExecuteNonQuery();
DataBase.Desconecta();
refresh_logistics_table();
}
protected void grdData_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView_logistics.PageIndex = e.NewPageIndex;
refresh_logistics_table();
}
}
Não sei se isto dá erros, estou a editar só em texto
↧