• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2809
  • Last Modified:

'table' is a type and cannot be used as an expression

Hi,

Please kindly guide me what went wrong and how can we resolve this error:

-trying to flip the columns on a datagrid, from standard format:
ID NAME AGE SEX

-to-

ID
Name
AGE
SEX

Thanks.


Server Error in '/' Application.
--------------------------------------------------------------------------------

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30684: 'table' is a type and cannot be used as an expression.
Source Error:
Line 77:       table.Rows.Add(r)
Line 78:    Next dt
Line 79:    ds.Tables.Add(table)
Line 80: End Function 'FlipDataSet
Line 81:
 

Source File: c:\inetpub\wwwroot\qualityassurance\cardadd.aspx    Line: 79


Private Function GenerateSource() As DataTable
Dim con As New SqlConnection("")
Dim cmd As SqlCommand = con.CreateCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "SP_operator_id_name_desig_team"
cmd.Parameters.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int, 4, ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", DataRowVersion.Current, Nothing))

Dim da As New SqlDataAdapter(cmd)
Dim ds As new DataSet()
da.Fill(ds,"DataSetName")
Return ds.Tables("DataSetName")
Dim new_ds As DataSet = FlipDataSet(ds) ' Flip the DataSet
Dim my_DataView As DataView = new_ds.Tables(0).DefaultView
datagrid1.DataSource=my_DataView
cmd.dispose
cmd.parameters.clear
con.close
End Function


Public Function FlipDataSet(my_DataSet As DataSet) As DataSet
   Dim ds As New DataSet()
   Dim dt As DataTable
   For Each dt In  my_DataSet.Tables
      Dim table As New DataTable()
      Dim i As Integer
      For i = 0 To dt.Rows.Count
         table.Columns.Add(Convert.ToString(i))
      Next i
      Dim r As DataRow
      Dim k As Integer
      For k = 0 To dt.Columns.Count - 1
         r = table.NewRow()
         r(0) = dt.Columns(k).ToString()
         Dim j As Integer
         For j = 1 To dt.Rows.Count
            r(j) = dt.Rows((j - 1))(k)
         Next j
      Next k
      table.Rows.Add(r)
   Next dt
   ds.Tables.Add(table)
End Function 'FlipDataSet


0
lynnton
Asked:
lynnton
  • 2
  • 2
2 Solutions
 
ihenryCommented:
:o) Hopefully it is just a typo

    Public Function FlipDataSet(ByVal my_DataSet As DataSet) As DataSet
        Dim ds As New DataSet
        Dim dt As DataTable
        For Each dt In my_DataSet.Tables
            Dim table As New DataTable
            Dim i As Integer
            For i = 0 To dt.Rows.Count
                table.Columns.Add(Convert.ToString(i))
            Next i
            Dim r As DataRow
            Dim k As Integer
            For k = 0 To dt.Columns.Count - 1
                r = table.NewRow()
                r(0) = dt.Columns(k).ToString()
                Dim j As Integer
                For j = 1 To dt.Rows.Count
                    r(j) = dt.Rows((j - 1))(k)
                Next j
                table.Rows.Add(r)
            Next k
            ds.Tables.Add(table)
        Next dt
    End Function 'FlipDataSet
0
 
lynntonAuthor Commented:
ihenry,

I'm not sure if this is good news, no error, the datagrid didn't display. The datagrid didn't show when executed.

Thanks.


    Sub Page_Load(Sender As Object, E As EventArgs)
    GenerateSource()
    End Sub

    Private Function GenerateSource() As DataTable
    Dim con As New SqlConnection("Server=....")
    Dim cmd As SqlCommand = con.CreateCommand()
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "SP_operator_id_name_desig_team"
    cmd.Parameters.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int, 4, ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", DataRowVersion.Current, Nothing))
    Dim da As New SqlDataAdapter(cmd)
    Dim ds As new DataSet()
    da.Fill(ds,"DataSetName")
    Return ds.Tables("DataSetName")
    Dim new_ds As DataSet = FlipDataSet(ds) ' Flip the DataSet
    Dim my_DataView As DataView = new_ds.Tables(0).DefaultView
    cmd.dispose
    cmd.parameters.clear
    con.close
    datagrid1.DataSource=my_DataView
    datagrid1.DataBind()
    End function


0
 
tusharashahCommented:
That is because you need to return your DataSet from FlipDataset() function..

        Next dt
     Return ds
    End Function 'FlipDataSet

-tushar
0
 
lynntonAuthor Commented:
tusharashah,

How are you today?

Change done. Sadly no change, I've tried exec sp_opeartor_id_name_desig_name  it returned 100 records.

How can we troubleshoot furthur to determine the culprit?

Thanks.
0
 
tusharashahCommented:
Private Function GenerateSource() As DataTable
    Dim con As New SqlConnection("Server=....")
    Dim cmd As SqlCommand = con.CreateCommand()
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "SP_operator_id_name_desig_team"
    cmd.Parameters.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int, 4, ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", DataRowVersion.Current, Nothing))
    Dim da As New SqlDataAdapter(cmd)
    Dim ds As new DataSet()
    da.Fill(ds,"DataSetName")
'    Return ds.Tables("DataSetName")       <------------------------ Remove this line...
    Dim new_ds As DataSet = FlipDataSet(ds) ' Flip the DataSet
    Dim my_DataView As DataView = new_ds.Tables(0).DefaultView
    cmd.dispose
    cmd.parameters.clear
    con.close
    datagrid1.DataSource=my_DataView
    datagrid1.DataBind()
    End function
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now