VB.NET create datatable?
I am trying to create a datatable in VB.NET, but when I output a row from this table it shows: "System.Data.DataRow".
I have compared my code to several websites and it seems correct. However if I instead Response.Write these values they display correctly.
So here is my code:
Sub Page_Load(Sender as Object, E as EventArgs)
If Not IsPostback Then
...
End If
Main
End Sub
Sub Main()
Dim FirstMonthDate As Date = CDate("1/1/" & dYear.SelectedValue)
Dim LastMonthDate As Date = GlobalFunctions.GlobalF.MonthLastDate(dYear.SelectedValue & "-" & dEndMonth.SelectedValue & "-1")
Dim TheGroup As String
Dim LastWeek As String
Dim FirstWeek As String
Dim dLastWeek As Date
Dim dFirstWeek As Date
Dim iWeek As String
Dim commandstring As String = "SELECT cast(DateDiff(wk, '1/1/2011', '" & LastMonthDate & "') as varchar(30)) AS SELECTED_WEEK "
Dim DSDateData As New System.Data.DataSet
DSDateData = GlobalFunctions.GlobalF.GetDevSQLServerDataSet(commandstring)
Dim dRow As DataRow
For Each dRow In DSDateData.Tables(0).Rows
iWeek = dRow("SELECTED_WEEK")
Next
Dim FirstYearDate As Date = CDate("1/1/" & dYear.SelectedValue)
commandstring = "SELECT DateAdd(ww, " & iWeek & ", '" & FirstYearDate & "') AS LAST_WEEK "
D开发者_如何学Pythonim DSDateData2 As New System.Data.DataSet
DSDateData2 = GlobalFunctions.GlobalF.GetDevSQLServerDataSet(commandstring)
For Each dRow In DSDateData2.Tables(0).Rows
LastWeek = dRow("LAST_WEEK")
Next
dLastWeek = CDate(LastWeek)
commandstring = "SELECT DATEADD(dd, -1, DATEADD(wk, DATEDIFF(wk,0, dateadd(dd,6-datepart(day,'" & LastMonthDate & "'),'" & LastMonthDate & "') ), 0)) AS FIRST_WEEK "
Dim DSDateData3 As New System.Data.DataSet
DSDateData3 = GlobalFunctions.GlobalF.GetDevSQLServerDataSet(commandstring)
For Each dRow In DSDateData3.Tables(0).Rows
FirstWeek = dRow("FIRST_WEEK")
Next
dFirstWeek = CDate(FirstWeek)
dFirstWeek = DateAdd(DateInterval.WeekOfYear, -1, dFirstWeek)
Dim arrDayYear As New ArrayList()
Dim dFirstDay As New Date
Dim arrList As ArrayList = New ArrayList()
Dim dt2 As New DataTable
Dim sWeek As DataColumn = New DataColumn("sWeek")
sWeek.DataType = System.Type.GetType("System.String")
Dim sDay As DataColumn = New DataColumn("sDay")
sDay.DataType = System.Type.GetType("System.String")
dt2.Columns.Add(sWeek)
dt2.Columns.Add(sDay)
Response.Write("NW1")
Dim dr As DataRow
dr = dt2.NewRow()
dr("sWeek") = "New week"
dr("sDay") = "New day"
dt2.Rows.Add(dr)
Response.Write(dr)
Response.Write("<br>")
dFirstDay = dFirstDay.AddDays(1)
...
End Sub
...
It's this last Response.Write that is causing the "System.Data.DataRow". But the Response.Write before this (NW1) does print. So what am I doing wrong?
Your code is correct.
Calling Response.Write(dr)
prints dr.ToString()
.
Since DataRow
doesn't override ToString()
, it jsut returns the typename.
You probably want to prin the value of a column.
You must iterate the data rows or select a specific data row. Iterating and outputing the values would be like so:
//c#
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
Console.WriteLine(row[col]);
}
//VB.Net
For Each (row As DataRow In dt.Rows)
For Each (col As DataColumn In dt.Columns)
Console.WriteLine(row[col])
Next
Next
To print just the first row:
DataRow row = dt.Rows[0];
foreach (DataColumn col in row.Columns)
Console.WriteLine(row[col]);
Response.Write() Needs a string, so it is converting your variable (dr) to a string. The default "toString" method just uses the name of the class. In this case "System.Data.DataRow"
It seems like you might want to loop over the row and output each value individually something like...
For Each element In dr.ItemArray
Response.Write(element)
Next
Response.Write("<br>")
精彩评论