Foros del Web » Programación para mayores de 30 ;) » .NET »

Object reference not set to an instance of an object

Estas en el tema de Object reference not set to an instance of an object en el foro de .NET en Foros del Web. Cita: Quiza se les haga familiar el titulo, y quiza me manden al foro donde ya se resolvio, pero les comentare, ya lo lei y ...
  #1 (permalink)  
Antiguo 22/03/2011, 12:49
 
Fecha de Ingreso: julio-2006
Mensajes: 2
Antigüedad: 17 años, 8 meses
Puntos: 0
Pregunta Object reference not set to an instance of an object

Cita:
Quiza se les haga familiar el titulo, y quiza me manden al foro donde ya se resolvio, pero les comentare, ya lo lei y aun no logro solucionarlo, me lo tope en una clase que estoy haciendo, estoy ocupando VB 2010 y aun no lo corrijo, alguien puede apoyarme? es al momento de tratar de crear la conexion con la base de datos que contiene los archivos .dbf (recordemos que la base de datos es solo una carpeta)

Este es el codigo:



Código Clase VB 2010:
Ver original
  1. Imports System.Data
  2.  
  3. Public Class Connect_DataBase
  4.  
  5.     Public driver As String
  6.     Public database As System.Data.Odbc.OdbcConnection
  7.     'Public database As OleDb.OleDbConnection
  8.     Public status_message As String
  9.     Public select_string = "Select * from "
  10.  
  11.     Public Sub New(ByVal sPath As String)
  12.         driver = "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=" & sPath & ";"
  13.         'driver = "Provider=Microsoft.Jet.OLED B.4.0;Data Source=" & sPath & "; Extended Properties=dBASE IV;"
  14.         'Dim connAs New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLED B.4.0;Data Source=C:\DB; Extended Properties=dBASE IV;")
  15.  
  16.         MessageBox.Show(driver)
  17.         Using database As New System.Data.Odbc.OdbcConnection(driver)
  18.             'Using database As New OleDb.OleDbConnection(driver)
  19.             Try
  20.                 Open()
  21.                 'Status = "Connected"
  22.             Catch ex As Exception
  23.                 MessageBox.Show("DataBase Not Found" & vbCrLf & "Select Another Path" & vbCrLf & ex.Message)
  24.                 'Status = "Error"
  25.             End Try
  26.         End Using
  27.     End Sub
  28.  
  29.     Public Property Status() As String
  30.         Get
  31.             Return status_message
  32.         End Get
  33.         Set(ByVal value As String)
  34.             MessageBox.Show("Llegando")
  35.             status_message = value
  36.         End Set
  37.     End Property
  38.  
  39.     Public Sub Open()
  40.         database.Open()
  41.     End Sub
  42.  
  43.     Public Sub Close()
  44.         database.Close()
  45.     End Sub
  46.  
  47.     Public Sub OpenTable(ByVal tableName As String)
  48.         Try
  49.             Dim data As New System.Data.Odbc.OdbcDataAdapter(select_string & tableName, database)
  50.             'Dim data As New OleDb.OleDbDataAdapter(select_string & tableName, database)
  51.         Catch ex As Exception
  52.             MessageBox.Show(ex.Message)
  53.         End Try
  54.     End Sub
  55.  
  56. End Class
Corregido, no se puede cambiar la instancia en un metodo cuando fue creada en otro, hay que enviarle por parametro el objeto correspondiente

Código Clase Corregida VB 2010:
Ver original
  1. Imports System.Data
  2.  
  3. Public Class Connect_DataBase
  4.  
  5.     Public driver As String
  6.     Public Shared database As System.Data.Odbc.OdbcConnection
  7.     Public status_message As String
  8.     Public select_string = "Select * from "
  9.  
  10.     Public Sub New(ByVal sPath As String)
  11.         driver = "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=" & sPath & ";"
  12.         Using database As New System.Data.Odbc.OdbcConnection(driver)
  13.             Try
  14.                 Open(database)
  15.                 Status = "Connected"
  16.             Catch ex As Exception
  17.                 MessageBox.Show("DataBase Not Found" & vbCrLf & "Select Another Path")
  18.                 Status = "Error"
  19.             End Try
  20.         End Using
  21.     End Sub
  22.  
  23.     Public Property Status() As String
  24.         Get
  25.             Return status_message
  26.         End Get
  27.         Set(ByVal value As String)
  28.             status_message = value
  29.         End Set
  30.     End Property
  31.  
  32.     Public Sub Open(ByVal connection As System.Data.Odbc.OdbcConnection)
  33.         connection.Open()
  34.     End Sub
  35.  
  36.     Public Sub Close(ByVal connection As System.Data.Odbc.OdbcConnection)
  37.         connection.Close()
  38.     End Sub
  39.  
  40.     Public Sub OpenTable(ByVal tableName As String)
  41.         Try
  42.             Dim data As New System.Data.Odbc.OdbcDataAdapter(select_string & tableName, database)
  43.         Catch ex As Exception
  44.             MessageBox.Show(ex.Message)
  45.         End Try
  46.     End Sub
  47.  
  48. End Class

Este mensaje puede cerrarse para evitar futuras respuestas

Gracias por al menos leer mi duda

Última edición por TyraelMX; 22/03/2011 a las 16:01 Razón: Solución encontrada
  #2 (permalink)  
Antiguo 22/03/2011, 17:57
 
Fecha de Ingreso: julio-2006
Mensajes: 2
Antigüedad: 17 años, 8 meses
Puntos: 0
De acuerdo Posible Solución: Object reference not set to an instance of an object

Pues esta es una de las posibles soluciones que he encontrado al estarle moviendo a mis lineas, por ahi si encuentro otras que lo hagan mas rapido y sencillo las expondre de nuevo. Aun tengo un par de lineas que depurar.

Código vb:
Ver original
  1. Imports System.Data
  2.  
  3. Public Class Connect_DataBase
  4.  
  5.     Public driver As String
  6.     Public database As System.Data.Odbc.OdbcConnection
  7.     Public status_message As String
  8.     Public table_status_message As String
  9.  
  10.     Public Sub New(ByVal sPath As String)
  11.         driver = "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=" & sPath & ";"
  12.         'Using database As New System.Data.Odbc.OdbcConnection(driver)
  13.        'Si no ocupamos el Using, podemos conservar el objeto hasta que se destruya la clase. Hasta el momento ha sido la solución efectiva que he encontrado al estarle moviendo a mis lineas, espero poder seguir simplificandolas hasta hacer de la clase mas rapida y efectiva
  14.        database = New System.Data.Odbc.OdbcConnection(driver)
  15.         Try
  16.             Open()
  17.             Status = "Connected"
  18.         Catch ex As Exception
  19.             MessageBox.Show("DataBase Not Found" & vbCrLf & "Select Another Path")
  20.             Status = "Error"
  21.         End Try
  22.         'End Using
  23.    End Sub
  24.  
  25.     Public Property Status() As String
  26.         Get
  27.             Return status_message
  28.         End Get
  29.         Set(ByVal value As String)
  30.             status_message = value
  31.         End Set
  32.     End Property
  33.  
  34.     Public ReadOnly Property Conn() As System.Data.Odbc.OdbcConnection
  35.         Get
  36.             Return database
  37.         End Get
  38.     End Property
  39.  
  40.     Public Sub Open()
  41.         database.Open()
  42.     End Sub
  43.  
  44.     Public Sub Close()
  45.         database.Close()
  46.     End Sub
  47. End Class

Etiquetas: object, reference, set
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 01:54.