none
Problems with DB connection on Form_Load RRS feed

  • Question

  • Hi,

    This is my first post here (please excuse my bad English).

    I'm starting with VB 2010 and DB connections, and have a problem in my first Form. I want to check if there is any record in a table of the DB counting the records (perhaps it's not the best way to do), but I have 2 problems with this:

    1.- The DB File is NOT in that path, but there is no error when I run the code, and that has no sense.

    2.- The code after "ConexionDB.Open()" never executes, so the if-else clause never checks, and I don't know why.

    Thanks in advance.

    This is my sample code:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        Dim CadenaConexion As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\test.accdb;Persist Security Info=True;Jet OLEDB:Database Password=MyPass"
        Dim ConexionDB As New OleDbConnection(CadenaConexion)
        Dim AdaptadorDB As New OleDbDataAdapter("SELECT * FROM usuarios", ConexionDB)
        Dim TablaVirtual As New DataTable
        Dim CuentaFilas As Integer
    
        Using ConexionDB
    
        ConexionDB.Open()
    
        TablaVirtual.Clear()
        AdaptadorDB.Fill(TablaVirtual)
        CuentaFilas = TablaVirtual.Rows.Count
    
        ConexionDB.Close()
    
        End Using
    
        If CuentaFilas <> 0 Then
          Form2.Show()
          Me.Close()
        Else
          TextBoxUsuario.Focus()
        End If
    End Sub
    
    Tuesday, February 8, 2011 3:25 AM

Answers

  • Hi Pollillo,

    Welcome to ado.net managed providers forum.

    Please check the database connect track, it seems which do not connect with the database success.

    There is my sample code for following and I have had the record:

     OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Persist Security Info = False;Data    Source=\"D:\\C_Code\\Z_DB\\AccessDB\\Database.accdb\"");
                conn.Open();
                string sql = "select * from Customer";

                OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);

                DataTable table = new DataTable();
                adapter.Fill(table);

                if (table.Rows.Count > 0)
                {
                    Console.Write("Success.");
                }
               
               
                conn.Close();

    I hope that would be help you and Are there any issues please let me know.

    Have a good day.


    If it's helpful for you, Please vote or mark. Thank you!

    David Peng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 10, 2011 8:52 AM
    Moderator
  • Unless you are changing the AppDomain setting for |DataDirectory| the database should be in your application folder. Did you verify that there is no database in the application folder? If you are not getting an exception then I would suspect there is.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Thursday, February 10, 2011 12:59 PM

All replies

  • I would recommend enclosing your code in a Try...Case block. Also, try setting a breakpoint and stepping through the code in Debug to see if you can determine what is happening.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Tuesday, February 8, 2011 4:13 PM
  • Thanks for the answer.

    I've done it several times, but it never stops in the breakpoint, and when I step throug the code with F8 the form shows with no errors.

    I don't understand because in the same form I have a button and in the button code I connect with the same DB again, but there I have an error when the file is not present in the path or opened, but not in the form_load, and I have the same path copy-pasted.

    I'm totally lost with this.

    Tuesday, February 8, 2011 8:48 PM
  • Hi Pollillo,

    Welcome to ado.net managed providers forum.

    Please check the database connect track, it seems which do not connect with the database success.

    There is my sample code for following and I have had the record:

     OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Persist Security Info = False;Data    Source=\"D:\\C_Code\\Z_DB\\AccessDB\\Database.accdb\"");
                conn.Open();
                string sql = "select * from Customer";

                OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);

                DataTable table = new DataTable();
                adapter.Fill(table);

                if (table.Rows.Count > 0)
                {
                    Console.Write("Success.");
                }
               
               
                conn.Close();

    I hope that would be help you and Are there any issues please let me know.

    Have a good day.


    If it's helpful for you, Please vote or mark. Thank you!

    David Peng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 10, 2011 8:52 AM
    Moderator
  • Unless you are changing the AppDomain setting for |DataDirectory| the database should be in your application folder. Did you verify that there is no database in the application folder? If you are not getting an exception then I would suspect there is.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Thursday, February 10, 2011 12:59 PM