.Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 15th, 2003, 01:01 PM
trueneutral trueneutral is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 21 trueneutral User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Object reference not set to an instance of an object

I understand the problem here, just not the solution.

I am creating a webpage using ASP that dynamically creates input textboxes, and then takes any text from those boxes and writes them to a database.

The Textboxes are named dynamically depending on how many there are. For example, if 26 text boxes are needed, they are created and named from "Text0" to "Text25".

Unfortunately because these are created during runtime, I am unable to call them to extract the information that might be in them.

For example, if I wanted to call: text1.text.toString() and save it to a value, it'll give me the "Object reference not set to an instance of an object" error. I've tried sneaky ways of trying to get around it, but in every case (even if the compiler doesn't yell at me for syntax), as soon as I call the runtime variable, it'll give me that error.

Does anyone know how I can call runtime variables in VB.NET?

Thanks

Reply With Quote
  #2  
Old September 16th, 2003, 12:21 PM
Wingman Wingman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Bavaria, Germany
Posts: 140 Wingman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 40 m 41 sec
Reputation Power: 6
Assuming you create your textboxes this way:

Code:
<% for (int i = 0; i < 3; i++) { %>
<input name="text<%=i%>" type="text">
<% } %>

...you can access its text values via...

Code:
private void btnSubmit_Click(object sender, EventArgs e) {
	foreach (string key in Request.Form) {
		if (key.StartsWith("text")) {
			Response.Write(key + ": " + Request.Form[key] + "<br />");
		}
	}
}


Anyhow...you can always access each element inside your form via Request.Form[string key]. If you have dynamic stuff imho this is the only way.

Wingman

Reply With Quote
  #3  
Old September 2nd, 2008, 04:45 AM
scoper's Avatar
scoper scoper is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2004
Location: Surrey, UK
Posts: 853 scoper User rank is First Lieutenant (10000 - 20000 Reputation Level)scoper User rank is First Lieutenant (10000 - 20000 Reputation Level)scoper User rank is First Lieutenant (10000 - 20000 Reputation Level)scoper User rank is First Lieutenant (10000 - 20000 Reputation Level)scoper User rank is First Lieutenant (10000 - 20000 Reputation Level)scoper User rank is First Lieutenant (10000 - 20000 Reputation Level)scoper User rank is First Lieutenant (10000 - 20000 Reputation Level)scoper User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 21 h 31 m 52 sec
Reputation Power: 170
Send a message via MSN to scoper Send a message via Google Talk to scoper Send a message via Skype to scoper
This is my main issue with VB... C# will not even let you compile that because there is no variable called "text1" (for example) defined in the current scope - (if you create a server-side textbox at runtime there is still no member variable associated with it)

One thing you could do is give each of your newly created controls an ID and then use FindControl (member of Page) - the method returns a Control, so you will have to cast the return to a TextBox (or whatever) to use properties pertaining to that specific object - although being VB it will quite happily perform the conversion for you - you just won't have any decent intellisense for it.

Code:
VB
Dim text1 as new Textbox()
text1.ID = "text1"
Controls.Add(text1)
...
FindControl("text1").Text = "text"


Code:
C#
Textbox text1 = new Textbox();
text1.ID = "text1";
Controls.Add(text1);
...
((Textbox)FindControl("text1")).Text = "text";
__________________
Scott Perham - MCPD
Ekina.net - Application Design

That URL too long? Why not URL IT!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > Object reference not set to an instance of an object


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT