Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 5th, 2007, 11:45 PM
Registered User
 
Join Date: Jun 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using class reference getting error

i am using a class(newuser.cs)for inserting username, the code i had use is below

/* Newuser.cs */
 public class Newuser
{
    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["sqlcon"]);

    public void adduser(insertuser user)
    {
       SqlCommand cmd = new SqlCommand("login",sqlcon);
       cmd.CommandType = CommandType.StoredProcedure;


        cmd.Parameters.Add("@username", SqlDbType.Char);
        cmd.Parameters["@username"].Direction = ParameterDirection.Input;
        cmd.Parameters["username"].Value = user.username;

     }
}


public class insertuser

{
  public string username
        {
            get
            {
                return username;
            }

            set
            {
                username = value;
            }
        }
}

and i am calling this class in the button click event

/* login.aspx.cs */

protected void btnUpdate_Click(object sender, EventArgs e)
    {
        insertuser add = new insertuser();
        add.username = TextBox1.Text;

    }

but after i entered values in textbox1 and once i clicked on button i am getting this error pointing to username=value. and in messagebox :- An unhandled exception of type System.StackOverflowException' occurred in App_Code.dtsq82es.dll

is there anyone to help me to overcome from this problem.please tell me how i can able to get the username to my database.

Thanking you
 
Old July 6th, 2007, 07:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there...

the compiler is right.. you have an stack overflow because you are using the property in a bad way... if you don't store the value of the property in a variable, there will be eternal calls to the property...

Code:
public string username
        {
            get
            {
                return username;
            }

            set
            {
                username = value;
            }
        }
this is your property..

username is the name of the property.. and also is where you are storing the new value.. since they have the same name, when you pass a value to username something like this happens..

username get's a value, username call username get property to store it, username get's a value, username call itself (again) get property to store it, and again and again...

you have to store the value in another variable... something like this...

Code:
private string username
public string Username
        {
            get
            {
                return username;
            }

            set
            {
                username = value;
            }
        }
remember that C# is case sensitive...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old July 6th, 2007, 01:53 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

If you follow a good naming convention, these problems are encountered much less often. A lot of people elect to prefix class scope variables with an underscore. I also like to use some form of prefix notation to make reading the code easier.
Code:
private string _strUsername;
public string Username
        {
            get{ return _strUsername; }
            set{ _strUsername = value; }
        }


-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Reference Cache or Application from a class pauliehaha ASP.NET 2.0 Professional 1 April 17th, 2007 09:51 AM
How to Reference a Page in an External Class Aaron Edwards ASP.NET 2.0 Basics 0 March 12th, 2007 01:18 AM
Class Object reference error. mjhoagland ASP.NET 1.0 and 1.1 Basics 2 February 2nd, 2006 09:28 AM
Passing a class by reference James Diamond VB How-To 13 February 16th, 2004 06:07 AM
Reference a class within ocx shahpragnesh_p Pro VB Databases 1 August 28th, 2003 08:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.