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 February 11th, 2008, 04:46 AM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default Adding non-form data to an Insert

I want to include the DateTime of a request as one of the elements that I capture in my contact form. I've defined a DateTime variable in my code behind. I was trying to place it into a text field of my form, but the text does not appear.

public string strDate = DateTime.Now.ToString();

protected void Page_Load(object sender, EventArgs e)
{
     tbDate.Text = strDate;
}

If I could get that to work I could use the <asp:ControlParameter> which I'm most comfortable using to do an insert. So my question is twofold. One, why doesn't this work?

Two, I'd really prefer to pass the variable directly to my SqlDataSource control. If I don't want the user to input it, the back of my head keeps screaming that it should not be part of the UI. If I could pass the variable directly in, I could drop the Date textbox entirely.

-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
__________________
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
 
Old February 11th, 2008, 08:59 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I am not sure why the text doesn't appear for you. Using this code:

string s = DateTime.Now.ToString();
TextBox1.Text = s;

achieves the desired result. Have you considered, instead of using this field, using the getdate() method of Sql Server to get the current date and time rather then using a text box?

hth.

================================================== =========
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
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old February 12th, 2008, 05:47 AM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

I couldn't figure out why it didn't work either. I'll try skipping the page load as shown.

It sounds like getdate() is a method of the database? I will check it out. I know that SQL Server has a lot more advanced functionality, but I haven't ever tried "programming" it like that. I'll run some searches and check back if I have more questions on it.

Thanks!

-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
 
Old February 13th, 2008, 04:09 AM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

:S OK, from the frying pan into the fire. Can you make sense of this? I tried moving the assignment line out of the Page_Load and rewriting the lines exactly the way you have. I get a Compilation Error on the second line.

string s = DateTime.Now.ToString();
TextBox1.Text = s; //This line throws the error.

CS1519: Invalid token '=' in class, struct, or interface member declaration. But the help files on CS1519 don't seem to relate to this situation, instead talking about invalid modifiers in a variable declaration.

When I tried it with the line TextBox1.Text = s; inside a Page_Load event handler, the page runs, but the time is not inserted into the text box. When I move both lines into the Page_Load, it throws an error.


protected void Page_Load(object sender, EventArgs e)
{ // this line throws the error
string s = DateTime.Now.ToString();
TextBox1.Text = s;
}

CS1513: } expected. What's going on?

-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
 
Old February 13th, 2008, 08:53 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

It sounds like your class looks something like this:

public partial class foo
{
   protected void Page_Load(object sender, eventargs e)
  {
    string s = DateTime.Now.ToString();
    TextBox1.Text = s;
  }

Notice the missing closing brace. Does adding an additional closing brace to the end of your class fix your problem?

================================================== =========
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
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old February 17th, 2008, 01:00 AM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

:o I think I understand it now. I went back to look, and the closing brace was there. Beat my head against it for awhile longer (and did some partially satisfactory searches on SQL Server).

I was working with an old file from one of .NET courses. The instructor was teaching us VB. I finally noticed, that while I'd modified most everything to work for C#, the AutoEventWireup was set to false. I'm sure there are ways to work with that, but I'm not familiar with them. I added a fresh webform to my project added a label control and typed the lines into my page_load event in the code behind. :) Now I'm grinning.

I'm guessing that with the wireup set to false, there was nothing to tie the code in the code behind to the control on the page? If that's true then am I correct in thinking that it had no idea what TextBox1 was; so it was trying to interpret the line as a "class, struct, or interface member declaration"?

-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding controls to a form Scootterp Access VBA 2 August 4th, 2009 02:32 PM
insert data in two tables from form mfarooqw ASP.NET 1.0 and 1.1 Professional 1 July 10th, 2007 08:34 AM
How to insert bulk of data through form in databas ashuphp Beginning PHP 1 April 11th, 2007 07:36 AM
insert form data in an sql and email the content ddnk77872 PHP Databases 3 February 13th, 2007 09:47 AM
Adding controls to a form based on data received jontheniceguy Visual Basic 2005 Basics 0 April 7th, 2006 10:07 AM





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