Click here to Skip to main content
15,921,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the above error display in internet explorer window. How to solve the error.



C#
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Summary description for global
/// </summary>
public class global
{
    SqlConnection con = new SqlConnection("server=DHINESH\\SQLEXPRESS;database=password;integrated security=true");
    SqlCommand Com;
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    SqlDataReader dr;
    
	public global()
	{
        openCon();
	}
    ~global()
    {
        closeCon();
    }
    public void openCon()
    {
       string s = ConfigurationManager.ConnectionStrings["con"].ToString();
       this.con = new SqlConnection(s);
       this.con.Open();
       
    }
    public void closeCon()
    {
       
            
         // this.Con.Close();
        this.con.Close();
        
    }
        
    public int gettable(string[,] MyInparam, string MySpname)
    {
        this.Com = con.CreateCommand();
        this.Com.CommandText = MySpname.ToString();
        this.Com.CommandType = System.Data.CommandType.StoredProcedure;
        for (int i = 0; i < MyInparam.Length / 2; i++)
        {
            Com.Parameters.AddWithValue(MyInparam[i, 0], MyInparam[i, 1]);
        }
       int a= this.Com.ExecuteNonQuery();
       return a;
    }
    public int getvalue(string query)
    {
        this.Com=con.CreateCommand();
        this.Com.CommandText=query.ToString();
        this.Com.CommandType = CommandType.Text;
        int a = (int)Com.ExecuteScalar();
        return a;
    }
    public DataSet getdata(string query)
    {
        this.Com = con.CreateCommand();
        this.Com.CommandText = query.ToString();
        this.Com.CommandType = CommandType.Text;
        this.da = new SqlDataAdapter(Com);
        this.da.Fill(ds);
        return ds;
    }
    public DataSet getdataset(string[,] MyInparam, string MySpname)
    {
        ds = new DataSet();
        
        this.Com = con.CreateCommand();
        this.Com.CommandText = MySpname.ToString();
        this.Com.CommandType = System.Data.CommandType.StoredProcedure;
        for (int i = 0; i < MyInparam.Length / 2; i++)
        {
            Com.Parameters.AddWithValue(MyInparam[i, 0], MyInparam[i, 1]);
        }
        this.da = new SqlDataAdapter(Com);
        this.da.Fill(ds);
        return ds;
        
    }
    public string CreateRandomCode(int codeCount)
    {
        string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
        string[] allCharArray = allChar.Split(',');
        string randomCode = "";
        int temp = -1;
        Random rand = new Random();
        for (int i = 0; i < codeCount; i++)
        {
            if (temp != -1)
            {
                rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
            }
            int t = rand.Next(36);
            if (temp != -1 && temp == t)
            {
                return CreateRandomCode(codeCount);
            }
            temp = t;
            randomCode += allCharArray[t];
        }
        return randomCode;
    }
}
Posted
Updated 24-Feb-11 20:37pm
v2

Hi, Close the connection at end of every methods.

e.g

public DataSet getdata(string query)
{
   this.Com = con.CreateCommand();
   this.Com.CommandText = query.ToString();
   this.Com.CommandType = CommandType.Text;
   this.da = new SqlDataAdapter(Com);
   this.da.Fill(ds);
  
   //  Close the connection
    
   closeCon();

   // 
   return ds;
}


i hope it will solve your problem

[edit]Code block added to preserve formatting - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
OriginalGriff 25-Feb-11 2:47am    
Good advice! Gets my five.
Espen Harlinn 25-Feb-11 10:42am    
That's how it's usually done when you work with DataSets in asp.et applications, my 5
attiq_khan is right: you should close connections as soon as you have finished with them. SQL Connections are a scarce system resource, and should be opened for the least time possible.
i.e. In any procedure where you are accessing the database:
1) Open the connection
2) Access the database
3) Close the connection

At present you open the connection in the constructor, and close it in the destructor. That means that the connection is lying there, open but unused, until the user decides to do something. Potentially, that can mean that other users cannot access the DB at all, because all available connections are in use - and all waiting for the user to come back from lunch...

This kind of thing really annoys database administrators!
 
Share this answer
 
Comments
Espen Harlinn 25-Feb-11 10:43am    
Good points :)
Have cunsider to use "using" in c# for example it will work like this
using(con = new SqlConnection(.......))
{
//Here you can do what ever you want to do and will not have to worry about closing the connection at all 
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900