Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 5th, 2007, 11:45 PM
Registered User
 
Join Date: Jun 2007
Location: bangalore, karnataka, India.
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
Reply With Quote
  #2 (permalink)  
Old July 6th, 2007, 07:59 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,190
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
================================================== =========
Reply With Quote
  #3 (permalink)  
Old July 6th, 2007, 01:53 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,413
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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 08:28 AM
Passing a class by reference James Diamond VB How-To 13 February 16th, 2004 05:07 AM
Reference a class within ocx shahpragnesh_p Pro VB Databases 1 August 28th, 2003 08:31 AM



All times are GMT -4. The time now is 12:22 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.