Cant seem to get code behind file working with aspx page

Below are my two files the .cs file and the aspx file.  i have compiled the cd to a dll without a problem but when i try to test it i have my three textboxes on the web page but the code behind doesn't proecess anything.

Help Please



Code Behind file between astrics

*****************************

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace MyDBLookup
{
    public class Lookup : Page
    {
        protected System.Web.UI.WebControls.Label MyLabel;
        protected System.Web.UI.WebControls.Button MyButton;
        protected System.Web.UI.WebControls.TextBox MyTextBox;
        protected System.Web.UI.WebControls.TextBox TextBox1;
        protected System.Web.UI.WebControls.TextBox TextBox2;
        protected System.Web.UI.WebControls.TextBox TextBox3;


        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            double MyVar = 0;
            if (TextBox1.Text != "0")
            {
                string ConnectionString = "data source=sql3;initial catalog=Baxter;UID=sa;PWD=XPassword;APP=KGM;DATABASE=Baxter";

                SqlConnection con = new SqlConnection(ConnectionString);
                con.Open();

                string query = "select TOP 1 Price from WebBonusProducts";
            
                SqlCommand cmd = new SqlCommand(query, con);
                SqlDataReader reader = cmd.ExecuteReader();

                while ( reader.Read());
                    TextBox2.Text = reader["Price"].ToString();

                MyVar = double.Parse(TextBox2.Text) * double.Parse(TextBox1.Text);
            
            TextBox3.Text = MyVar.ToString();

            }
        }
    }
}


*****************************




ASPX PAGE BETWEEN ASTRICS

******************************

<%@ Language="C#" Inherits="MyDBLookup.Lookup" %>



<html>
<body>

<form runat="server">
Quantity:
<asp:TextBox id="textBox1" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
Price Each:
<asp:TextBox id="textBox2" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
Order Total:
<asp:TextBox id="textBox3" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
</form>

</body>
</html>


******************************
rmardisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Stephan_SchrandtCommented:
0
Jesus RodriguezIT ManagerCommented:
Set the AutoPostBack="True" on the TextBox1 on the ASPX Page like this

<%@ Language="C#" Inherits="MyDBLookup.Lookup" %>

<html>
<body>

<form runat="server">
Quantity:
<asp:TextBox id="textBox1" runat="server" AutoPostBack="True" />
<p><asp:Label id="lbl1" runat="server" /></p>
Price Each:
<asp:TextBox id="textBox2" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
Order Total:
<asp:TextBox id="textBox3" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
</form>
</body>
</html>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jesus RodriguezIT ManagerCommented:
Also add  OnTextChanged="TextBox4_TextChanged" to this line
<asp:TextBox id="textBox1" runat="server" AutoPostBack="True" />

will be like this
<asp:TextBox id="textBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox4_TextChanged" />
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

käµfm³d 👽Commented:
@k-designers
Set the AutoPostBack="True" on the TextBox1 on the ASPX Page like this
Setting AutoPostBack to true has nothing to do with the event handler for a control. Enabling that property just means you can leave the TextBox (i.e. TextBox loses focus) and a postback will occur.
0
varunvnsCommented:
Hi rmardis,

I have faced similar problem in the past.
And the method I used to solve it is hit and trial.

First, please check whether Intellisense is working in the Code Behind File or not.
To check this, try writing the control name present in the aspx page and try accessing some property.
In case this is not getting done, that means that the files are rather disconnected.
Do one thing, Goto aspx page in source and add the following in the Page Directive.

CodeBehind="PageName.aspx.cs"

Mostly your problem will be solved here.

2. IF you get the Intellisense, go to the aspx page, select Source and press Save button multiple times. then goto Design view save it multiple times and then clean and build your solution again!

3. Also, look into the aspx.designer.cs file, whether the controls (textboxes) you added are present in them or not.. This is very imp. Because it happens sometimes that the designer.cs is not updated and the prob that you are facing, may come up.

Hope this helps!

Regards,
Varun Shringarpure
0
varunvnsCommented:
And yea forgot..

You need to add Autopostback = true on the textbox to wireup the event handler with the Textbox.
So that the event is handled and your code can run.

Regards,
Varun Shringarpure
0
käµfm³d 👽Commented:
You need to add Autopostback = true on the textbox to wireup the event handler with the Textbox.
No, you don't. You need to attach the handler as Stephan_Schrandt initially pointed out, and k-designers later demonstrates how to do in the markup. AutoPostBack has NOTHING to do with the handler which handles the event on the server.
0
Jesus RodriguezIT ManagerCommented:
What @kaufmed said is true. You don't need to add the Autopostback to attach the handler.
Quote "AutoPostBack has NOTHING to do with the handler which handles the event on the server."
0
rmardisAuthor Commented:
i used K-designers suggestion

<asp:TextBox id="textBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox4_TextChanged" />

I changed  OnTextChanged="TextBox4_TextChanged" /> to OnTextChanged="TextBox1_TextChanged" />

after entering a number into TextBox1 i received the following message

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:


[InvalidOperationException: Invalid attempt to read when no data is present.]
   System.Data.SqlClient.SqlDataReader.GetValue(Int32 i) +118
   System.Data.SqlClient.SqlDataReader.get_Item(String name) +27
   MyStuffy.MyClassy.TextBox1_TextChanged(Object sender, EventArgs e) +183
   System.Web.UI.WebControls.TextBox.OnTextChanged(EventArgs e) +8742974
   System.Web.UI.WebControls.TextBox.RaisePostDataChangedEvent() +55
   System.Web.UI.WebControls.TextBox.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +10
   System.Web.UI.Page.RaiseChangedEvents() +165
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1485

 
0
ddayx10Commented:
>>"You don't need to add autopostback property to textbox"... << unless there is no other physical means of posting back on the page as it seems from the code sample is the case here. Easy to overlook... I've done it in the past :)

So for the posted question you actually do need AutoPostBack on.

@rmardis if you added some other control that caused a postback to the page (like an <asp:button>) then you could count on that to cause your OnTextChanged to fire when it posted back the page once you had assigned the event. With the code you provided if you want the OnTextChanged to fire you need autopostback=true and the event will fire when the textbox content has been changed and the textbox loses focus.

Its a decision... What do I want to cause my OnTextChanged event to fire? A button or some other control that posts back the page OR when my textbox1 loses focus and has had its text changed.

Also note the user may not notice the slight difference between the good sample posted by @k-designers and his TextChanged event signature...just a small correction:

<asp:TextBox id="textBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged" />

No points just thought user might get confused with all the posts and differences of opinion.
0
ddayx10Commented:
remove the semi-colon on your while statement end of line:

        while (reader.Read())
        TextBox2.Text = reader["Price"].ToString();
0
käµfm³d 👽Commented:
Change this:

while ( reader.Read());
                TextBox2.Text = reader["Price"].ToString();

Open in new window


to this:

while ( reader.Read())
{
    TextBox2.Text = reader["Price"].ToString();
}

Open in new window

0
ddayx10Commented:
Also consider improving your connection management by updating your code to something like this:
string ConnectionString = "data source=sql3;initial catalog=Baxter;UID=sa;PWD=XPassword;APP=KGM;DATABASE=Baxter";
        SqlConnection con = new SqlConnection(ConnectionString);
        using (con)
        {
            string query = "select TOP 1 Price from WebBonusProducts";
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();

            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                TextBox2.Text = reader["Price"].ToString();
            }
        }
        MyVar = double.Parse(TextBox2.Text) * double.Parse(TextBox1.Text);
        TextBox3.Text = MyVar.ToString();

Open in new window

0
käµfm³d 👽Commented:
While I agree with ddayx10's latest suggestion regarding the using { ... } construct, I'd actually suggest putting the declaration itself within the using's parentheses:

using (SqlConnection con = new SqlConnection(ConnectionString))
{
...

Open in new window


While there is nothing technically incorrect with his approach, that fact that using automatically calls the Dispose method of the object it encapsulates means if you accidentally reference "con" after the using exits, then you will get an ObjectDisposedException exception at the line which references "con".
0
ddayx10Commented:
YEP I was just being lazy...
0
ddayx10Commented:
The more I think about the whole thing though @kaufmed its a distinction without meaningful context.

I probably wouldn't want to use it again, and in that case I'd do it the way you suggest.

If I had multiple operations I'd probably run all my various connection operations in that one using statment and still do it the way you suggest.

If I for some reason did want to use the same sqlconnection object later (which was I think what you were pointing out) I'd just re-initialize it and keep on going.

So I guess I agree...but not for the sample reason you suggested.
0
käµfm³d 👽Commented:
If I for some reason did want to use the same sqlconnection object later ... I'd just re-initialize it and keep on going.
Certainly, and that makes sense. However, it would be quite easy, especially for a beginner, to think that this is OK:

SqlConnection con = new SqlConnection(ConnectionString);

using (con)
{
    // do something with con
}

using (con)
{
    // do something else with con
}

Open in new window


...that is until you get the ObjectDisposedException in the 2nd using becuase you didn't re-initialize the connection after (implicitly) disposing it in the first using.
0
ddayx10Commented:
... but that's like saying.... I might forget to cast something later or I might make a typo in the name... Yes I might make a mistake later but then I'd know and correct it.

I call... "last word" (not that its likely to do me any good) haha and so there :)
0
rmardisAuthor Commented:
this worked to get it to fire.  i also had to close the reader to get the data to populate.  Thank you so much, and i thank all who also tried to help me you all are really experts!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.