473,765 Members | 2,203 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 #1
10 3714
It would probably be harder to have this question be more vague or
non-precise.

"Having trouble" does not mean anything. You need to show us relevant code
snippets, and say what you expect to happen vs exactly what is happening -
meaning exact error messages, which line #, etc.

"D. Shane Fowlkes" <sh********** @h-o-t-m-a-i-l.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
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 #2

See below:

'TROUBLE HERE
For Each dtRow In dtAppData
intDaysLeft = dtAppData("Days Left")
strCloseDate = dtAppData("Clos eDate")
Next
'TROUBLE HERE
For Each dtRow In dtAppData
intDaysLeft = dtRow("DaysLeft ")
strCloseDate = dtRow("CloseDat e")
Next
' you are writing for each dtRow (DataRow), so you need
to use the dtRow object to get your data, not the dtAppData( the datatable)

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 #3
Thanks.

Let me ask you this. Assuming my function works properly, how would YOU,
being a more experienced .NET developer, write a page_load sub to read the
datatable? That's where I'm stuck.


"Marina Levit [MVP]" <so*****@nospam .com> wrote in message
news:ec******** ******@TK2MSFTN GP14.phx.gbl...
It would probably be harder to have this question be more vague or
non-precise.

"Having trouble" does not mean anything. You need to show us relevant code
snippets, and say what you expect to happen vs exactly what is happening -
meaning exact error messages, which line #, etc.

"D. Shane Fowlkes" <sh********** @h-o-t-m-a-i-l.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
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 #4
What do you mean 'read the datatable'? What does that actually mean? The
datatable is there and presumably ready for use.

Does the function return a datatable? Populate a datatable declared at the
class level?

Again, it's impossible to understand what you are trying to do without some
code snippets.

"D. Shane Fowlkes" <sh********** @h-o-t-m-a-i-l.com> wrote in message
news:OG******** ********@TK2MSF TNGP14.phx.gbl. ..
Thanks.

Let me ask you this. Assuming my function works properly, how would YOU,
being a more experienced .NET developer, write a page_load sub to read the
datatable? That's where I'm stuck.


"Marina Levit [MVP]" <so*****@nospam .com> wrote in message
news:ec******** ******@TK2MSFTN GP14.phx.gbl...
It would probably be harder to have this question be more vague or
non-precise.

"Having trouble" does not mean anything. You need to show us relevant
code snippets, and say what you expect to happen vs exactly what is
happening - meaning exact error messages, which line #, etc.

"D. Shane Fowlkes" <sh********** @h-o-t-m-a-i-l.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
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 #5
You know, I apologize. The size of my outlook window with posts, it seemed
like the end of message was your description, and I did not see that there
was a code snippet below that.

You have a couple of problems:

1. You never add the row to the datatable. You call NewRow, assign values
to the row - but you don't add the row to the actual datatable. So it never
has any rows.
2. As peter pointed out, 'dtAppData("Day sLeft")' accesses the column
definition for DaysLeft. Not the row value. You need to call
'dtRow("DaysLef t")' for the row's value for that column.

A couple of asides:

I am not sure why you are using a datareader, just to manually create a
datatable, manually create columns in it, and populate the datatable. Why
not fill the datatable directly from the database to begin with and avoid
the datareader?

Secondly, please turn Option Strict On. It will catch a lot of run time
problems at compile time instead of runtime.

"D. Shane Fowlkes" <sh********** @h-o-t-m-a-i-l.com> wrote in message
news:OG******** ********@TK2MSF TNGP14.phx.gbl. ..
Thanks.

Let me ask you this. Assuming my function works properly, how would YOU,
being a more experienced .NET developer, write a page_load sub to read the
datatable? That's where I'm stuck.


"Marina Levit [MVP]" <so*****@nospam .com> wrote in message
news:ec******** ******@TK2MSFTN GP14.phx.gbl...
It would probably be harder to have this question be more vague or
non-precise.

"Having trouble" does not mean anything. You need to show us relevant
code snippets, and say what you expect to happen vs exactly what is
happening - meaning exact error messages, which line #, etc.

"D. Shane Fowlkes" <sh********** @h-o-t-m-a-i-l.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
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 #6
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 #7
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 #8
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 #9
It needs to be:

For Each dtRow in dtAppData.Rows

Since you want to go through the collection. You should use intellisense in
helping you find the right properties and methods. There are numerous
examples out there of how to loop through all the rows in a datatable. Not
that this is what you want - because you only have 1 row coming back.

Ditch the datareader, and read up on using a SqlDataAdapter to fill a
datatable.

And finally, searching ASP.NET forums is not a good idea, when you are
looking for ADO.NET information. It makes sense that ASP.NET forums would
have examples of databinding, since that is what you do a lot in web
applications.

"D. Shane Fowlkes" <sh********** @h-o-t-m-a-i-l.com> wrote in message
news:OT******** ********@tk2msf tngp13.phx.gbl. ..
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 #10

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

Similar topics

5
2504
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
3709
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
3030
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
3086
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
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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...
0
9832
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
7375
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
6649
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
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.