1

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 "
            Dim 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?

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

this isn't making sense to me. How do I print an entire row?
2

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]);

1 Comment

how do I print just the first row?
0

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>")

2 Comments

This was helpful but I don't see a property named "element". Could you be more definitive please?
"element" in this case would be the name of the variable that you want to hold each item as you loop over the data row. And it would actually need to be element in dr.ItemArray I believe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.