Name not declared Error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Vmlj?=

    Name not declared Error

    Hi,

    I am getting "Name objDR is not declared" error. I am declaring objDR in
    the if
    then else section. How do I over come this issue.

    Thanks for the help.

    Dim objDS As New DataSet("Root")
    Dim strXml As New StringBuilder
    Dim strTable As String = "row"
    Dim objRetMetaData As New DataSet
    Dim blnNextResult As Boolean = True
    Dim intCount As Integer = 0
    Dim intIndex As Integer = 0

    If OpenConnection( ) Then
    If retType = eReturnType.eDa taSet Then
    If mDatabaseType = eDatabaseType.e ODBC Then
    Dim objCmd As New Odbc.OdbcComman d(sql,
    oODBCConnection )
    objCmd.CommandT ype = CommandType.Tex t

    Dim objDA As New Odbc.OdbcDataAd apter(objCmd)
    objDA.Fill(objD S, strTable)
    mDataSetObj = objDS
    Else
    'Create the Command Object
    Dim objCmd As New SqlClient.SqlCo mmand(sql,
    objSQLConn)
    objCmd.CommandT ype = CommandType.Tex t
    Dim objDA As New SqlClient.SqlDa taAdapter(objCm d)
    objDA.Fill(objD S, strTable)
    mDataSetObj = objDS
    End If
    Else
    If mDatabaseType = eDatabaseType.e ODBC Then
    Dim objCmd As New Odbc.OdbcComman d(sql,
    oODBCConnection )
    objCmd.CommandT ype = CommandType.Tex t
    Dim objDR As OdbcDataReader = objCmd.ExecuteR eader
    Else
    Dim objCmd As New SqlClient.SqlCo mmand(sql,
    objSQLConn)
    objCmd.CommandT ype = CommandType.Tex t
    Dim objDR As SqlDataReader = objCmd.ExecuteR eader
    End If
    strXml.Append(" <Root>")
    Do Until blnNextResult = False
    If objDR.HasRows() = False Then
    Exit Do
    Else
    While objDR.Read()
    strXml.Append(" <row")
    For intIndex = 0 To objDR.FieldCoun t - 1
    strXml.Append(" " &
    objDR.GetName(i ntIndex) & "=""" _
    & objDR.GetValue( intIndex) & """")
    Next
    strXml.Append("/>")
    End While

    blnNextResult = objDR.NextResul t()
    If blnNextResult = True Then
    intCount += 1
    End If

    End If
    Loop
    strXml.Append(" </Root>")
    mReturnXML = strXml.ToString
    objDR.Close()
    End If
    End If

  • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

    #2
    RE: Name not declared Error

    Move the declaration to the top (dim objDR as OdbcDataReader) . Keep the
    initialization where it is, (objDR = objCmd.ExecuteR eader).

    "Vic" wrote:
    Hi,
    >
    I am getting "Name objDR is not declared" error. I am declaring objDR in
    the if
    then else section. How do I over come this issue.
    >
    Thanks for the help.
    >
    Dim objDS As New DataSet("Root")
    Dim strXml As New StringBuilder
    Dim strTable As String = "row"
    Dim objRetMetaData As New DataSet
    Dim blnNextResult As Boolean = True
    Dim intCount As Integer = 0
    Dim intIndex As Integer = 0
    >
    If OpenConnection( ) Then
    If retType = eReturnType.eDa taSet Then
    If mDatabaseType = eDatabaseType.e ODBC Then
    Dim objCmd As New Odbc.OdbcComman d(sql,
    oODBCConnection )
    objCmd.CommandT ype = CommandType.Tex t
    >
    Dim objDA As New Odbc.OdbcDataAd apter(objCmd)
    objDA.Fill(objD S, strTable)
    mDataSetObj = objDS
    Else
    'Create the Command Object
    Dim objCmd As New SqlClient.SqlCo mmand(sql,
    objSQLConn)
    objCmd.CommandT ype = CommandType.Tex t
    Dim objDA As New SqlClient.SqlDa taAdapter(objCm d)
    objDA.Fill(objD S, strTable)
    mDataSetObj = objDS
    End If
    Else
    If mDatabaseType = eDatabaseType.e ODBC Then
    Dim objCmd As New Odbc.OdbcComman d(sql,
    oODBCConnection )
    objCmd.CommandT ype = CommandType.Tex t
    Dim objDR As OdbcDataReader = objCmd.ExecuteR eader
    Else
    Dim objCmd As New SqlClient.SqlCo mmand(sql,
    objSQLConn)
    objCmd.CommandT ype = CommandType.Tex t
    Dim objDR As SqlDataReader = objCmd.ExecuteR eader
    End If
    strXml.Append(" <Root>")
    Do Until blnNextResult = False
    If objDR.HasRows() = False Then
    Exit Do
    Else
    While objDR.Read()
    strXml.Append(" <row")
    For intIndex = 0 To objDR.FieldCoun t - 1
    strXml.Append(" " &
    objDR.GetName(i ntIndex) & "=""" _
    & objDR.GetValue( intIndex) & """")
    Next
    strXml.Append("/>")
    End While
    >
    blnNextResult = objDR.NextResul t()
    If blnNextResult = True Then
    intCount += 1
    End If
    >
    End If
    Loop
    strXml.Append(" </Root>")
    mReturnXML = strXml.ToString
    objDR.Close()
    End If
    End If
    >

    Comment

    • =?Utf-8?B?Vmlj?=

      #3
      RE: Name not declared Error

      I cannot because I am declaring objDR as sqlDataReader in the else condition.

      Thanks,
      Vic

      "Family Tree Mike" wrote:
      Move the declaration to the top (dim objDR as OdbcDataReader) . Keep the
      initialization where it is, (objDR = objCmd.ExecuteR eader).
      >
      "Vic" wrote:
      >
      Hi,

      I am getting "Name objDR is not declared" error. I am declaring objDR in
      the if
      then else section. How do I over come this issue.

      Thanks for the help.

      Dim objDS As New DataSet("Root")
      Dim strXml As New StringBuilder
      Dim strTable As String = "row"
      Dim objRetMetaData As New DataSet
      Dim blnNextResult As Boolean = True
      Dim intCount As Integer = 0
      Dim intIndex As Integer = 0

      If OpenConnection( ) Then
      If retType = eReturnType.eDa taSet Then
      If mDatabaseType = eDatabaseType.e ODBC Then
      Dim objCmd As New Odbc.OdbcComman d(sql,
      oODBCConnection )
      objCmd.CommandT ype = CommandType.Tex t

      Dim objDA As New Odbc.OdbcDataAd apter(objCmd)
      objDA.Fill(objD S, strTable)
      mDataSetObj = objDS
      Else
      'Create the Command Object
      Dim objCmd As New SqlClient.SqlCo mmand(sql,
      objSQLConn)
      objCmd.CommandT ype = CommandType.Tex t
      Dim objDA As New SqlClient.SqlDa taAdapter(objCm d)
      objDA.Fill(objD S, strTable)
      mDataSetObj = objDS
      End If
      Else
      If mDatabaseType = eDatabaseType.e ODBC Then
      Dim objCmd As New Odbc.OdbcComman d(sql,
      oODBCConnection )
      objCmd.CommandT ype = CommandType.Tex t
      Dim objDR As OdbcDataReader = objCmd.ExecuteR eader
      Else
      Dim objCmd As New SqlClient.SqlCo mmand(sql,
      objSQLConn)
      objCmd.CommandT ype = CommandType.Tex t
      Dim objDR As SqlDataReader = objCmd.ExecuteR eader
      End If
      strXml.Append(" <Root>")
      Do Until blnNextResult = False
      If objDR.HasRows() = False Then
      Exit Do
      Else
      While objDR.Read()
      strXml.Append(" <row")
      For intIndex = 0 To objDR.FieldCoun t - 1
      strXml.Append(" " &
      objDR.GetName(i ntIndex) & "=""" _
      & objDR.GetValue( intIndex) & """")
      Next
      strXml.Append("/>")
      End While

      blnNextResult = objDR.NextResul t()
      If blnNextResult = True Then
      intCount += 1
      End If

      End If
      Loop
      strXml.Append(" </Root>")
      mReturnXML = strXml.ToString
      objDR.Close()
      End If
      End If

      Comment

      • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

        #4
        RE: Name not declared Error

        Sorry, now I see what you are trying to do.

        SqlDataReader and OdbcDataReader both inherit from DbDataReader, so that
        should be the declared type at the top of your routine.

        "Vic" wrote:
        I cannot because I am declaring objDR as sqlDataReader in the else condition.
        >
        Thanks,
        Vic
        >
        "Family Tree Mike" wrote:
        >
        Move the declaration to the top (dim objDR as OdbcDataReader) . Keep the
        initialization where it is, (objDR = objCmd.ExecuteR eader).

        "Vic" wrote:
        Hi,
        >
        I am getting "Name objDR is not declared" error. I am declaring objDR in
        the if
        then else section. How do I over come this issue.
        >
        Thanks for the help.
        >
        Dim objDS As New DataSet("Root")
        Dim strXml As New StringBuilder
        Dim strTable As String = "row"
        Dim objRetMetaData As New DataSet
        Dim blnNextResult As Boolean = True
        Dim intCount As Integer = 0
        Dim intIndex As Integer = 0
        >
        If OpenConnection( ) Then
        If retType = eReturnType.eDa taSet Then
        If mDatabaseType = eDatabaseType.e ODBC Then
        Dim objCmd As New Odbc.OdbcComman d(sql,
        oODBCConnection )
        objCmd.CommandT ype = CommandType.Tex t
        >
        Dim objDA As New Odbc.OdbcDataAd apter(objCmd)
        objDA.Fill(objD S, strTable)
        mDataSetObj = objDS
        Else
        'Create the Command Object
        Dim objCmd As New SqlClient.SqlCo mmand(sql,
        objSQLConn)
        objCmd.CommandT ype = CommandType.Tex t
        Dim objDA As New SqlClient.SqlDa taAdapter(objCm d)
        objDA.Fill(objD S, strTable)
        mDataSetObj = objDS
        End If
        Else
        If mDatabaseType = eDatabaseType.e ODBC Then
        Dim objCmd As New Odbc.OdbcComman d(sql,
        oODBCConnection )
        objCmd.CommandT ype = CommandType.Tex t
        Dim objDR As OdbcDataReader = objCmd.ExecuteR eader
        Else
        Dim objCmd As New SqlClient.SqlCo mmand(sql,
        objSQLConn)
        objCmd.CommandT ype = CommandType.Tex t
        Dim objDR As SqlDataReader = objCmd.ExecuteR eader
        End If
        strXml.Append(" <Root>")
        Do Until blnNextResult = False
        If objDR.HasRows() = False Then
        Exit Do
        Else
        While objDR.Read()
        strXml.Append(" <row")
        For intIndex = 0 To objDR.FieldCoun t - 1
        strXml.Append(" " &
        objDR.GetName(i ntIndex) & "=""" _
        & objDR.GetValue( intIndex) & """")
        Next
        strXml.Append("/>")
        End While
        >
        blnNextResult = objDR.NextResul t()
        If blnNextResult = True Then
        intCount += 1
        End If
        >
        End If
        Loop
        strXml.Append(" </Root>")
        mReturnXML = strXml.ToString
        objDR.Close()
        End If
        End If
        >

        Comment

        • Tom Shelton

          #5
          Re: Name not declared Error

          On 2008-04-07, Vic <Vic@discussion s.microsoft.com wrote:
          Hi,
          >
          I am getting "Name objDR is not declared" error. I am declaring objDR in
          the if
          then else section. How do I over come this issue.
          >
          Thanks for the help.
          >
          Dim objDS As New DataSet("Root")
          Dim strXml As New StringBuilder
          Dim strTable As String = "row"
          Dim objRetMetaData As New DataSet
          Dim blnNextResult As Boolean = True
          Dim intCount As Integer = 0
          Dim intIndex As Integer = 0
          >
          If OpenConnection( ) Then
          If retType = eReturnType.eDa taSet Then
          If mDatabaseType = eDatabaseType.e ODBC Then
          Dim objCmd As New Odbc.OdbcComman d(sql,
          oODBCConnection )
          objCmd.CommandT ype = CommandType.Tex t
          >
          Dim objDA As New Odbc.OdbcDataAd apter(objCmd)
          objDA.Fill(objD S, strTable)
          mDataSetObj = objDS
          Else
          'Create the Command Object
          Dim objCmd As New SqlClient.SqlCo mmand(sql,
          objSQLConn)
          objCmd.CommandT ype = CommandType.Tex t
          Dim objDA As New SqlClient.SqlDa taAdapter(objCm d)
          objDA.Fill(objD S, strTable)
          mDataSetObj = objDS
          End If
          Else
          If mDatabaseType = eDatabaseType.e ODBC Then
          Dim objCmd As New Odbc.OdbcComman d(sql,
          oODBCConnection )
          objCmd.CommandT ype = CommandType.Tex t
          Dim objDR As OdbcDataReader = objCmd.ExecuteR eader
          Else
          Dim objCmd As New SqlClient.SqlCo mmand(sql,
          objSQLConn)
          objCmd.CommandT ype = CommandType.Tex t
          Dim objDR As SqlDataReader = objCmd.ExecuteR eader
          End If
          strXml.Append(" <Root>")
          Do Until blnNextResult = False
          If objDR.HasRows() = False Then
          Exit Do
          Else
          While objDR.Read()
          strXml.Append(" <row")
          For intIndex = 0 To objDR.FieldCoun t - 1
          strXml.Append(" " &
          objDR.GetName(i ntIndex) & "=""" _
          & objDR.GetValue( intIndex) & """")
          Next
          strXml.Append("/>")
          End While
          >
          blnNextResult = objDR.NextResul t()
          If blnNextResult = True Then
          intCount += 1
          End If
          >
          End If
          Loop
          strXml.Append(" </Root>")
          mReturnXML = strXml.ToString
          objDR.Close()
          End If
          End If
          >
          Your main problem is that you are trying to reuse the same names for
          different types, and you are expecting a value declared in on block to
          be visible in another... The simplest fix for this method is to move
          your Command and DataReader declarations out of the if blocks to the top
          of your method. You'll want to declare them as IDbCommand and
          IDataReader...

          Dim objCmd As IDbCommand
          Dim objDR As IDataReader


          This means that you will remove the declaration syntax at their
          initialization and it will look like:

          objCmd = New Odbc.OdbcComman d(Sql, oODBCConnnectio n)

          Same with the reader. This will mean that you will have to remove the
          call to objDR.HasRows - since that is not defined on IDataReader
          inteface, but I really don't see it necessary on the code you have
          written.

          There are a lot of other refactorings that I would consider as well.
          For example, you could simply have your OpenConnection method return a
          IDBConnection. Basically, I would create factories for your various db
          objects that create the correct types, but return them as the generic
          interfaces that all of these types implement. Write to the interface,
          not to the implementation. In the rare case that you might need an
          implementation specific method/property, you can always do a trycast or
          an is test:

          dim sqlconnection = trycast(connect ion, sqlconnection)
          if not sqlconnection is nothing then
          ' call sqlconnection specific method
          end if

          HTH
          --
          Tom Shelton

          Comment

          Working...