Friday, March 16, 2012

Populating a textbox with a datasource

I have a textbox that needs to get it's value from database based on a value parsed from text in another textbox.

I am using a datasource with a select command to hit the database, but I have no need to collect anything but a single value, so it seems very wasteful to have to load anything into a dataset or the like, just to get that one thing to a textbox.

What's the easiest way to get this one value for the textbox?

thanks

Did you thought keep the data in a Session variable?

Your select command should similar to below

SELECT ID FROM table WHERE ID='1'

Now you could use one SqlCommand.ExecuteScalar which only returns 1 value from the database.

Check below link for more information

http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx

HC


rsegecin:

Did you thought keep the data in a Session variable?

That wouldn't do much because I wouldstill have to do all the fetching to get itinto the session variable, or any other variable for that matter.


Haissam:

Your select command should similar to below

SELECT ID FROM table WHERE ID='1'

Now you could use one SqlCommand.ExecuteScalar which only returns 1 value from the database.

Check below link for more information

http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx

HC

Yes that is very similar to the SQL I'm using

I was considering ExecuteScalar thought, the drawback is really that it's still a lot of code for one little value, and I have been reusing the datasources pretty religiously so far, eliminating the need for much code at all.

I'm hoping to keep that up, but maybe there is no particularly simple way... I wish datasources had a Scalar command that returned the value :/

0 comments:

Post a Comment