Hi There;
I have a ASPX file and a code-behind cs file. I try to connect to a postgresql, perfom some database issue, and disconnect from it finally. To name a few, I print all records of database to the html file, add some new records. All these operations are connected to <asp:Button elements in the aspx file.
After this introduction, I would like to show my codes: Here are they:
This is the aspx file:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="select_data_2.aspx.cs" Inherits="_08_Veritabanlari_AD0_NET_select_data_2" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<%-- This button enables us to print all contens of the database to the html.--%>
<asp:Button id="sendButon" runat="server" Text="Print the contents" OnClick="viewDatabase"></asp:Button>
<%-- It is expected that this button disconnects us from the database but it doesn't. --%>
<asp:Button id="closeConnect" runat="server" Text="Disconnect from database" OnClick="disconnectFromDatabase"></asp:Button>
<%-- This button enables us to print all contens of the database to the html.--%>
<asp:Button id="isConnected" runat="server" Text="Is connected to the Database?" OnClick="isConnectedToDatabase"></asp:Button>
<%-- This button does not working, this is not the case now.--%>
<asp:Button id="sendButon2" runat="server" Text="Print the contents 2(NOT WORKING)" OnClick="DATAGRIDSORGULA"></asp:Button>
<asp:Button id="getBiggest" runat="server" Text="Get greatest id" OnClick="viewID"></asp:Button>
</p>
<p>
<asp:Label ID="Label1" runat="server" Text="Enter name:"></asp:Label>
<asp:TextBox ID="nameText" runat="server" ></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Enter surname:"></asp:Label>
<asp:TextBox ID="surnameText" runat="server" ></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="Enter salary:"></asp:Label>
<asp:TextBox ID="cashText" runat="server" ></asp:TextBox>
</p>
<p>
<asp:Button id="insertButton" runat="server" Text="Insert to database" OnClick="insertData"></asp:Button>
</p>
<p>
<asp:Label ID="outputLabel" runat="server" ></asp:Label>
</p>
<p>
<asp:DataGrid runat="server" ID="datagridd1" ViewStateMode="Disabled"></asp:DataGrid>
</p>
</div>
</form>
</body>
</html>
And here is the content of the "select_data_2.aspx.cs". Our focus must be "disconnectFromDatabase" procedure,so I do not share the file at all.
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using Npgsql;
public partial class _08_Veritabanlari_AD0_NET_select_data_2 : System.Web.UI.Page
{
public _08_Veritabanlari_AD0_NET_select_data_2()
{
this.connectToDatabase();
this.getBiggestId();
}
private DataSet ds = new DataSet();
private DataTable dt = new DataTable();
private int id;
private static NpgsqlConnection conn;
private static NpgsqlCommand command;
private NpgsqlDataReader dr;
private string connstring;
//this is the problemmatic procedure
protected void disconnectFromDatabase(object nesne, EventArgs e)
{
try
{
conn.Close();
conn.Dispose();
conn = null;
}
catch (Exception msg)
{
// something went wrong, and you wanna know why
// Response.Write(msg.ToString());
throw;
}
}
}
How can I overcome the situation? Thanks in advance.