473,770 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read data from DataTable on page_load. How?

I have a function that is called in page_load and the purpose of this
function is to look up basic data in a MSSQL table and return it in the form
of a datatable. The page_load will read the data and then fill a few simple
labels.

**Assuming** that I wrote the function properly, how exactly can I write the
page_load sub to read the data from the function? I've tried a few examples
from a couple of books I have but can't seem to get a working model. I'll go
back and add try/catch to the function later once the basics are in place
and working.

Help and assistance would GREATLY be appreciated! I'm having trouble
reading the data in the page_load sub. Using ASP/VB .NET 2.


Protected Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)

Dim dtAppData As DataTable
Dim dtRow As DataRow
Dim dsCapData As DataSet
Dim intDaysLeft As Integer
Dim strCloseDate As String

dsCapData = New DataSet()
dtAppData = DaysLeftInAppSe ason(1)

'TROUBLE HERE
For Each dtRow In dtAppData
intDaysLeft = dtAppData("Days Left")
strCloseDate = dtAppData("Clos eDate")
Next

lblLabel1.Text = intDaysLeft
lblLabel2.Text = strCloseDate

End Sub



Protected Function DaysLeftInAppSe ason(ByVal intAppID As Integer) As
DataTable

Dim objConnection As SqlConnection
Dim cmdSelect As SqlCommand
Dim drAppData As SqlDataReader
Dim dtResponse As DataTable
Dim dtColumn As DataColumn
Dim dtRow As DataRow
Dim strConnectStrin g As String
Dim strSQL As String

strConnectStrin g = System.Web.Conf iguration.....e tc....
strSQL = "SELECT CloseDate, DateDiff(Day, GetDate(), CloseDate) AS DaysLeft
FROM ListAppTypes WHERE ID = " & intAppID

objConnection = New SqlConnection(s trConnectString )
cmdSelect = New SqlCommand(strS QL, objConnection)
dtResponse = New DataTable("Capi talData")
dtColumn = New DataColumn("Clo seDate", GetType(String) )
dtColumn = New DataColumn("Day sLeft", GetType(Integer ))

objConnection.O pen()
drAppData = cmdSelect.Execu teReader()
drAppData.Read( )
dtRow = dtResponse.NewR ow()
dtRow("CloseDat e") = drAppData("Clos eDate")
dtRow("DaysLeft ") = drAppData("Days Left")
drAppData.Close ()
objConnection.C lose()

Return dtResponse

End Function
Mar 27 '06
10 3715
That's it!

Thanks everyone!

"dtAppData.Tabl es(0).Rows(0)(" DaysLeft")"

"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > wrote in message
news:7E******** *************** ***********@mic rosoft.com...
You have to type everything:

If it's only one row you certainly don't need a for each since that would
just be a waste of CPU cycles:
lblLabel1.Text = dtAppData.Table s(0).Rows(0)("D aysLeft")
lblLabel2.Text = dtAppData.Table s(0).Rows(0)("C loseDate")

Hope that helps.

peter


--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"D. Shane Fowlkes" wrote:
I understand and I thank you for your time. I've looked into 2 books and
searched the asp.net forums and still haven't had much luck. I'm having
trouble connecting the dots here and I legitimately am stuck. I have
found
numerous datatable examples and how to read them but they all deal with
binding to a repeater or something like that. I'm not trying to bind - I
just want to read the values from the first and only row in the DT.

Let me simplify my question - How can I read a column 1 and column 2 from
a
single row, 2 column datatable? It's that simple (I think). The
function
returns a 2 column, 1 row record and I'm trying to get the page_load sub
to
read it.

I tried your suggestion but it didn't work. VWD is still telling me
"Expression is of type System.Data.Dat aTable, which is not a collection
type". It is telling me this at "For Each dtRow In dtAppData".

Thanks again.
Dim dtAppData As DataTable
Dim dtRow As DataRow
Dim intDaysLeft As Integer
Dim strCloseDate As String

dtAppData = MyFunction()

For Each dtRow In dtAppData
intDaysLeft = dtRow("DaysLeft ")
strCloseDate = dtRow("CloseDat e")
Next



"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > wrote in
message
news:52******** *************** ***********@mic rosoft.com...
> One final comment:
> We see (meaning Marina, I and many others) this happening all the time.
> The
> MS people put a huge investment into the Quickstarts and Samples
> applications; they install with either Visual Studio .NET or the .NET
> Framework SDK.
>
> The examples are progressive, easy to use, and very comprehensive. And
> they
> are all available in your choice of language. So, if you don't start at
> the
> Quickstarts, you are doing yourself a disservice in getting to "First
> Base"
> quickly and with less frustration.
> Peter
>
> --
> Co-founder, Eggheadcafe.com developer portal:
> http://www.eggheadcafe.com
> UnBlog:
> http://petesbloggerama.blogspot.com
>
>
>
>
> "D. Shane Fowlkes" wrote:
>
>> I have a function that is called in page_load and the purpose of this
>> function is to look up basic data in a MSSQL table and return it in
>> the
>> form
>> of a datatable. The page_load will read the data and then fill a few
>> simple
>> labels.
>>
>> **Assuming** that I wrote the function properly, how exactly can I
>> write
>> the
>> page_load sub to read the data from the function? I've tried a few
>> examples
>> from a couple of books I have but can't seem to get a working model.
>> I'll
>> go
>> back and add try/catch to the function later once the basics are in
>> place
>> and working.
>>
>> Help and assistance would GREATLY be appreciated! I'm having trouble
>> reading the data in the page_load sub. Using ASP/VB .NET 2.
>>
>>
>>
>>
>> Protected Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
>>
>> Dim dtAppData As DataTable
>> Dim dtRow As DataRow
>> Dim dsCapData As DataSet
>> Dim intDaysLeft As Integer
>> Dim strCloseDate As String
>>
>> dsCapData = New DataSet()
>> dtAppData = DaysLeftInAppSe ason(1)
>>
>> 'TROUBLE HERE
>> For Each dtRow In dtAppData
>> intDaysLeft = dtAppData("Days Left")
>> strCloseDate = dtAppData("Clos eDate")
>> Next
>>
>> lblLabel1.Text = intDaysLeft
>> lblLabel2.Text = strCloseDate
>>
>> End Sub
>>
>>
>>
>>
>>
>>
>>
>> Protected Function DaysLeftInAppSe ason(ByVal intAppID As Integer) As
>> DataTable
>>
>> Dim objConnection As SqlConnection
>> Dim cmdSelect As SqlCommand
>> Dim drAppData As SqlDataReader
>> Dim dtResponse As DataTable
>> Dim dtColumn As DataColumn
>> Dim dtRow As DataRow
>> Dim strConnectStrin g As String
>> Dim strSQL As String
>>
>> strConnectStrin g = System.Web.Conf iguration.....e tc....
>> strSQL = "SELECT CloseDate, DateDiff(Day, GetDate(), CloseDate) AS
>> DaysLeft
>> FROM ListAppTypes WHERE ID = " & intAppID
>>
>> objConnection = New SqlConnection(s trConnectString )
>> cmdSelect = New SqlCommand(strS QL, objConnection)
>> dtResponse = New DataTable("Capi talData")
>> dtColumn = New DataColumn("Clo seDate", GetType(String) )
>> dtColumn = New DataColumn("Day sLeft", GetType(Integer ))
>>
>> objConnection.O pen()
>> drAppData = cmdSelect.Execu teReader()
>> drAppData.Read( )
>> dtRow = dtResponse.NewR ow()
>> dtRow("CloseDat e") = drAppData("Clos eDate")
>> dtRow("DaysLeft ") = drAppData("Days Left")
>> drAppData.Close ()
>> objConnection.C lose()
>>
>> Return dtResponse
>>
>> End Function
>>
>>
>>


Mar 27 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2509
by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to have a separate data adapter & a separate data set for each select query? If thats possible then how?
2
2571
by: Mark Jones | last post by:
I'm trying to delete data from a DataGrid using a ButtonColumn with a CommandName="Delete" but it's not working a the min. I'm using session data which fills a DataTable which then fills the DataGrid. The code is as follows (dgBasket is the DataGrid): private void Page_Load(object sender, System.EventArgs e) { DataTable myTable = (DataTable) Session;
2
5653
by: Bonj | last post by:
Hi I've been following the example on http://aspnet.4guysfromrolla.com/articles/071002-1.3.aspx and no matter what I do, i can't get the DataGrid1_EditCommand event handler to fire. Could someone please tell me what I'm doing wrong. Code below. public class WebForm1 : System.Web.UI.Page
7
3370
by: Brian Henry | last post by:
Hi, I have a data grid that has the following properties EnableViewState = false AllowPaging = true AllowSorting = true AllowCustomPaging = false now, when the page switches I want to get the current page number and display it in a label on the form... but it doesnt seem to return anything but zero no mater which page i am on in CurrentPageIndex property of the
2
2034
by: Brian Henry | last post by:
Hi, I have a data grid that is set up like this Page items displayed = 10 EnableViewState = false (i dont want to send large amounts of data over the internet!) CustomPaging = false AllowPaging = true
4
2997
by: D. Shane Fowlkes | last post by:
Up until now, I've always had my functions return integers, strings, or booleans. Now, I've (hopefully) written a function to return a 2 column, single row datareader. Assuming I did this correctly (the function), how could I look at the results of the function in page_load and get the values? A little guidance would be great. Thanks once again!! (using ASP/VB .NET 2 and VWD)
6
3710
by: rcoco | last post by:
Hi, I have a datagrid that is ment to insert data. But when I run the form only the header appears. I would like some advise from you all and solve this problem I'm using visual studio 2003. My code looks like this: private void Fill() { DataTable table = new DataTable(); SqlDataAdapter adapter = new SqlDataAdapter("SELECT * from
2
3031
by: chike_oji | last post by:
Please can someone help me. I am writing a web application, that allows for the upload of an excel sheet into the database. I have an upload button and a save button. The upload button allows for the retrieval of the excel data into a DataTable, which is bound to a GridView for previewing before saving to the Database. But, whenever I click on the save button, I lose the DataTable's data, so I lose the data I want to save to the...
8
3087
by: Brock | last post by:
I am trying to populate a Crystal Report from data in my DataGrid. The reason for this is that I want the user to be able to change values without updating the database, but still have their report reflect the values they anticipate committing to see hypothetical totals of columns from a set of records. These records are displaying properly on my DataGrid but I'm not sure how to get Crystal Reports 10 to use as its datasource the dataset...
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.