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.

Recommended Answers

All 4 Replies

It seems I can not depict the problem properly: The button in aspx file, in which id is "closeConnect" is connected to the disconnectFromDatabase doesn't work. It is expected to disconnect, but it is failed to disconnect.

You shouldn't be trying to leave a connection open like that. You should close the connection in the same request in which it was opened. Also I doubt Your static variables will work quite like you want. ASP.net is a multi threaded environment which means you could still have multiple instances of those static variables, one for each thread that might be open. In general you should avoid things like global static variables or singletons when working in ASP.Net.

commented: thank you +1

Hi Cherryhomesj;
Thank you for your reply.

You shouldn't be trying to leave a connection open like that. You should close the connection in the same request in which it was opened.

How can I handle this? Where can I get information?

just move your code to close the connection to the end of the request where you open the connection.

your code should follow the basic format of

open connection
perform work in database
close connection

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.