Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 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
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 23rd, 2009, 01:10 PM
Registered User
 
Join Date: Feb 2009
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default Utilizing individual database values in Code

All,

I am very new to ASP.NET and Databases, so I have a really dumb question but I haven't been able to find an easy solution in either the Beginning or Professional ASP.NET 3.5 books.

If I have a MS SQL database that has multiple items, for example a database on pipes. It contains manufacturer, brand, material, part number, and maybe a flow rate of a liquid like water.

If I want to utilize the flow rate value from the database and perform some sort of calculation on it, but not change it in any way in the database, what is the easiest way to do that in ASP.NET? For example, if I just want to add (or subract) 2 of the flow rates together from 2 different pipes from the database, and output that value to the page, how do I do that?

I'm sure there is an easy and straightforward way to do it, I'm just not that familiar with the terminology yet to figure it out.

Any help would be appreciated. Thank you in advance.

Gary

Last edited by petersga85; February 23rd, 2009 at 09:55 PM..
  #2 (permalink)  
Old February 23rd, 2009, 01:17 PM
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,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hi there..

Yes, the easy way is to just return the value you want and add/subtract what you need in the presentation (when you show it).

You also can do this in the sql query directly, adding or subtracting what you want direct in the select clause.

any of this method will not change anything in the database. In fact, the database will not change unless you UPDATE it in any way.

What would you want help with?? I'm sure any of the two books have examples about it.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #3 (permalink)  
Old February 23rd, 2009, 01:50 PM
Registered User
 
Join Date: Feb 2009
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default RE: Utilizing individual database values in Code

I guess the question I have then is how do I get a value returned that I can use. Every time I seem to do it, I get some sort of value of an "Anonymous Type" that I cant work with, I cant cast it into an Int32 or a Double type in order to perform any calculations.

I specifically am looking for an example of how to obtain a numerical value from a SQL Datasource and assign it to a variable (Int or Double) that calculations can be performed on, either within the page under a script tag, or in the code behind.

It seems that most of the examples in the books are designed to extract or filter the database for specific items from the database and display the entire record (or update, delete, etc), not necessarily to show you how to extract individual values or items and do something with that value.

If someone can point me to a specific example in one of the two books or show me a small segment of code that demonstrates what I am looking for above, that would be very helpful.

Thanks again,

Gary

Last edited by petersga85; February 23rd, 2009 at 09:55 PM..
  #4 (permalink)  
Old February 23rd, 2009, 04:07 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

It's my understanding that you want to use an SqlDataSource to obtain the flow rate, given a part number for a particular pipe, correct?

If so, the first challenge is creating the proper Select statement for the SqlDataSource. Let's say in your database, your table is named Pipes, and there are columns in that table named FlowRateGPM and PartNo.

Code:
<asp:SqlDataSource ID="sdsPipes" runat="server" ConnectionString="<%$ ConnectionStrings:PipesDatabaseConnectionString %>"
SelectCommand="SELECT [FlowRateGPM] FROM [Pipes] WHERE ([PartNo] = @PartNo)">
 <SelectParameters>
    <asp:ParameterName="PartNo" Type="String"/>
 </SelectParameters>
</asp:SqlDataSource>
So, to get the data out, all you need to do is to supply a part number to the PartNo select parameter. That will give you some rows of data, which presumably in this case would be a single row. You need to get the value of the FlowRateGPM column in that single row, and then cast it to an integer.

You could do this anywhere in your code you would need to pluck out that value. For example, let's say you have a TextBox called txtPartNo that holds the value for the part number. You could do the following:

Code:
sdsPipes.SelectParameters["PartNo"].DefaultValue = txtPartNo.Text;
DataView dataView = (DataView) sdsPipes.Select(DataSourceSelectArguments.Empty);
int flowRate = (int)dataView[0]["FlowRateGPM"];
Hope that helps.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
petersga85 (February 23rd, 2009)
  #5 (permalink)  
Old February 23rd, 2009, 09:35 PM
Registered User
 
Join Date: Feb 2009
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks - that was exactly what I was looking for. I could not find any reference to the DataView Class in the books. This should work.
  #6 (permalink)  
Old February 25th, 2009, 01:22 AM
Registered User
 
Join Date: Feb 2009
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default

How do I get the code part to work in Visual Basic? I keep getting all sorts of errors when trying to implement this. Will txtPartNo.Text always be a String, or would it be a number as it is in the database?

If I imbed this in the page under a <script> tag will it still know what the DataSource is (i.e. sdsPipes) or do I need to re-declare it?

What does the DataSourceSelectArguments.Empty do?
  #7 (permalink)  
Old February 25th, 2009, 02:39 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

VB:

Code:
sdsPipes.SelectParameters("PartNo").DefaultValue = txtPartNo.Text
Dim dataView As DataView = DirectCast(sdsPipes.Select(DataSourceSelectArguments.Empty), DataView)
Dim flowRate As Integer = DirectCast(dataView(0)("FlowRateGPM"), Integer)
You can embed this in the page under a <script runat="server"> tag, should be no problem. You need to put it into a method, obviously. Probably you would put it into an event handler, like a button click or something.

As long as the SqlDataSource is in that page, you can refer to it by its ID, like any other control.

I was just using getting the part number from a TextBox as an example. In this case, txtPartNo.Text would be... um, text. I just assumed your part number was a string in the database. If it's an integer, you would specify <asp:ParameterName="PartNo" Type="Int"/> in the SqlDataSource, and you would want to convert the text box value to an integer.

Code:
sdsPipes.SelectParameters("PartNo").DefaultValue = Convert.ToInt32(txtPartNo.Text)
The Select method has a DataSourceSelectArguments parameter which you must supply. These let you do things to the data while it is being retrieved, like sort it, page it, or retrieve the number of records. Since you don't need to do that here, you must specify DataSourceSelectArguments.Empty.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}

Last edited by Lee Dumond; February 25th, 2009 at 02:42 AM..
The Following User Says Thank You to Lee Dumond For This Useful Post:
petersga85 (March 3rd, 2009)
  #8 (permalink)  
Old March 3rd, 2009, 12:36 AM
Registered User
 
Join Date: Feb 2009
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default

OK, I was finally able to get back to this today.

I still dont know what I'm doing wrong. When I try to do what you have suggested by putting the code on the page under the tag <script runat="server" > and I have this line in the code:

sdsPipes.SelectParameters("PartNo").DefaultValue = txtPartNo.Text

I get an error that says a "Declaration Expected" for sdsPipes with sdsPipes underlined in blue, even though on the same page I have:

<asp:SqlDataSource ID="sdsPipes" runat="server" ConnectionString="<%$ ConnectionStrings:PipesDatabaseConnectionString %>"
SelectCommand="SELECT [FlowRateGPM] FROM [Pipes] WHERE ([PartNo] = @PartNo)">
<SelectParameters>
<asp:ParameterName="PartNo" Type="String"/>
</SelectParameters>
</
asp:SqlDataSource>

I dont understand what declaration it is looking for. It seems that it is not remembering that the data source is defined.
  #9 (permalink)  
Old March 3rd, 2009, 01:14 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

What event handler do you have this code in?
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #10 (permalink)  
Old March 3rd, 2009, 01:30 AM
Registered User
 
Join Date: Feb 2009
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Thumbs up

Yeah, I just figured that part out - I added it to a Button Click event handler and I got it working just like you said. I think I was looking at it backwards.

Your code is using the Parameter to identify the PartNo and then using that in the SELECT query. I had a SELECT query already done and I was trying to use your code to get info out of that and it wasn't working.

I understand what you were doing now and it makes more sense. I wouldnt have figured out the DataView and DirectCast parts without your help.

Thank you again.

Gary
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Muenchian Grouping on individual nodes in xslt1.0 sudhish.sikhamani XSLT 9 July 1st, 2008 10:32 AM
Missing Individual aspx Files adamcherochak BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 7 March 3rd, 2008 02:30 PM
updating individual columns kscase VB Databases Basics 11 May 10th, 2007 03:18 PM
manipulating individual rows inside a datagrid.. slaverymorgue ASP.NET 1.0 and 1.1 Basics 0 February 19th, 2007 12:25 AM
Inserting records into a databae aadz5 ASP.NET 1.0 and 1.1 Basics 1 October 19th, 2003 10:47 AM



All times are GMT -4. The time now is 01:49 PM.


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