Name not declared Error

V

Vic

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.eDataSet Then
If mDatabaseType = eDatabaseType.eODBC Then
Dim objCmd As New Odbc.OdbcCommand(sql,
oODBCConnection)
objCmd.CommandType = CommandType.Text

Dim objDA As New Odbc.OdbcDataAdapter(objCmd)
objDA.Fill(objDS, strTable)
mDataSetObj = objDS
Else
'Create the Command Object
Dim objCmd As New SqlClient.SqlCommand(sql,
objSQLConn)
objCmd.CommandType = CommandType.Text
Dim objDA As New SqlClient.SqlDataAdapter(objCmd)
objDA.Fill(objDS, strTable)
mDataSetObj = objDS
End If
Else
If mDatabaseType = eDatabaseType.eODBC Then
Dim objCmd As New Odbc.OdbcCommand(sql,
oODBCConnection)
objCmd.CommandType = CommandType.Text
Dim objDR As OdbcDataReader = objCmd.ExecuteReader
Else
Dim objCmd As New SqlClient.SqlCommand(sql,
objSQLConn)
objCmd.CommandType = CommandType.Text
Dim objDR As SqlDataReader = objCmd.ExecuteReader
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.FieldCount - 1
strXml.Append(" " &
objDR.GetName(intIndex) & "=""" _
& objDR.GetValue(intIndex) & """")
Next
strXml.Append("/>")
End While

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

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

Family Tree Mike

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

Vic

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

Thanks,
Vic
 
F

Family Tree Mike

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.
 
T

Tom Shelton

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.eDataSet Then
If mDatabaseType = eDatabaseType.eODBC Then
Dim objCmd As New Odbc.OdbcCommand(sql,
oODBCConnection)
objCmd.CommandType = CommandType.Text

Dim objDA As New Odbc.OdbcDataAdapter(objCmd)
objDA.Fill(objDS, strTable)
mDataSetObj = objDS
Else
'Create the Command Object
Dim objCmd As New SqlClient.SqlCommand(sql,
objSQLConn)
objCmd.CommandType = CommandType.Text
Dim objDA As New SqlClient.SqlDataAdapter(objCmd)
objDA.Fill(objDS, strTable)
mDataSetObj = objDS
End If
Else
If mDatabaseType = eDatabaseType.eODBC Then
Dim objCmd As New Odbc.OdbcCommand(sql,
oODBCConnection)
objCmd.CommandType = CommandType.Text
Dim objDR As OdbcDataReader = objCmd.ExecuteReader
Else
Dim objCmd As New SqlClient.SqlCommand(sql,
objSQLConn)
objCmd.CommandType = CommandType.Text
Dim objDR As SqlDataReader = objCmd.ExecuteReader
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.FieldCount - 1
strXml.Append(" " &
objDR.GetName(intIndex) & "=""" _
& objDR.GetValue(intIndex) & """")
Next
strXml.Append("/>")
End While

blnNextResult = objDR.NextResult()
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.OdbcCommand(Sql, oODBCConnnection)

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(connection, sqlconnection)
if not sqlconnection is nothing then
' call sqlconnection specific method
end if

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top