locked
Getting error with SQLconnection RRS feed

  • Question

  • User-848281640 posted

    Hi friend, Iam getting a error with this line

    Sqlconnection conn =new sqlconnection(); The error is like this "An unhandled exception of type 'System.StackOverflowException' occurred in App_Code.iqplprez.dll".

    Here is my code:

    In clsdatalayer.cs

    public class empregdatalayer
    {
        clsconnection conobj = new clsconnection();
       
        empregdatalayer objdata = new empregdatalayer();
        public empregdatalayer()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public void insertdetails(empregentitylayer objentity)
        {
            int eid;
            DataTable dt1 = new DataTable();
            SqlCommand cmd = new SqlCommand();


            cmd.Parameters.AddWithValue("@name", objentity.name);
            cmd.Parameters.AddWithValue("@address", objentity.address);
            cmd.Parameters.AddWithValue("@phone", objentity.phone);
            conobj.executenonquerystoredprocedure("insertproc", cmd);
            string nm = objentity.name;

            dt1 = selectempid(nm);

            eid = Convert.ToInt32(dt1.Rows[0][0].ToString());
      }

     public DataTable selectempid(string nm)
        {
            string qr = "select empid from empdetails where empname='" + nm + "'";
            DataTable dt = new DataTable();
            dt = conobj.dtdetails(qr);

            return dt;
        }


    IN clsconnection.cs

    public class clsconnection
    {
        SqlConnection conn = new SqlConnection(); //this line error is occuring
     
        public clsconnection()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public void getconnection()
        {
            if (conn.State == ConnectionState.Closed)
            {

                conn.ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString;
                conn.Open();
            }
        }
        public DataTable dtdetails(string query)
        {
            getconnection();
            SqlCommand cmd = new SqlCommand(query, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            conn.Close();
            return dt;

        }

     public void executenonquerystoredprocedure(string query,SqlCommand cmd)
        {

            getconnection();
            SqlCommand newcmd = new SqlCommand();
            newcmd = cmd;

            newcmd.CommandType = CommandType.StoredProcedure;
            newcmd.CommandText = query;

            newcmd.Connection = conn;
            newcmd.ExecuteNonQuery();


            conn.Close();
        }

        

    }



    please help me to solve my problem. Thank you.

    Monday, August 10, 2009 10:34 AM

Answers

  • User-861155893 posted

    Your code states

    public class empregdatalayer
    {
        clsconnection conobj = new clsconnection();
       
        empregdatalayer objdata = new empregdatalayer(); <<<<

    this is a recursive call... the object empregdatalayer, when instantiated, creates another and another

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 10, 2009 3:43 PM

All replies

  • User-1419202310 posted

    A stack overflow is caused by too many nested function calls, not too much
    data. normally the stack overflow is caused by a recursive call.

    Check if there are any of that kind.
     

    Monday, August 10, 2009 11:50 AM
  • User1696475319 posted

    What does the stack trace say?

    Monday, August 10, 2009 12:21 PM
  • User-848281640 posted

    iam getting error like this

    Sqlconnection conn =new sqlconnection();// this line getting exception  error

    The error is like this "An unhandled exception of type 'System.StackOverflowException' occurred in App_Code.iqplprez.dll".

    System.StackOverflowException

    {Cannot evaluate expression because the current thread is in a stack overflow state.}

    Monday, August 10, 2009 1:55 PM
  • User-538024033 posted

     The most obvious thing I see is your case is off... are you just typing it incorrectly in your post, or did you type it incorrectly in your code?

    SqlConnection conn = new SqlConnection();
    //not Sqlconnection conn = new sqlconnection();

     


     

    Monday, August 10, 2009 2:52 PM
  • User-861155893 posted

    Your code states

    public class empregdatalayer
    {
        clsconnection conobj = new clsconnection();
       
        empregdatalayer objdata = new empregdatalayer(); <<<<

    this is a recursive call... the object empregdatalayer, when instantiated, creates another and another

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 10, 2009 3:43 PM
  • User-848281640 posted

    yes friend, that was the problem, thank you for all who replied in solving my problem.

    Tuesday, August 11, 2009 1:49 PM