5 Replies - 426 Views - Last Post: 31 January 2013 - 06:38 PM Rate Topic: -----

#1 jonbon004  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 12-July 12

Why I can't connect to database?

Posted 29 January 2013 - 05:32 PM

Here is my code, and when I connect to database it shows me the message box that I'm unable to connect to database.



Imports System.Data.OleDb
Public Class registration_form
    Dim conn As OleDbConnection
    Dim myqry As String = Nothing
    Dim mycmd As OleDbCommand
    Dim mydr As OleDbDataReader
    Dim str As String = Nothing

    Private Sub registration_form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call ConntoDB()
    End Sub

    
#Region "Connection"
    Sub ConntoDB()
        Try
            With conn
                If .State = ConnectionState.Open Then .Close()
                .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=votersAccounts.accdb"
                .Open()
            End With
        Catch ex As Exception
            MessageBox.Show("Unable to Connect", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Application.Exit()
        End Try
    End Sub
#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Then
            MessageBox.Show("Please enter your password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            myqry = "INSERT INTO accounts(Passwords,Usernames) "
            myqry = myqry + "VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "')"
            mycmd = New OleDbCommand
            With mycmd
                .CommandText = myqry
                .Connection = conn
                .ExecuteNonQuery()
            End With

        End If
    End Sub
End ClassImports System.Data.OleDb
Public Class registration_form
    Dim conn As OleDbConnection
    Dim myqry As String = Nothing
    Dim mycmd As OleDbCommand
    Dim mydr As OleDbDataReader
    Dim str As String = Nothing

    Private Sub registration_form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call ConntoDB()
    End Sub

    
#Region "Connection"
    Sub ConntoDB()
        Try
            With conn
                If .State = ConnectionState.Open Then .Close()
                .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=votersAccounts.accdb"
                .Open()
            End With
        Catch ex As Exception
            MessageBox.Show("Unable to Connect", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Application.Exit()
        End Try
    End Sub
#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Then
            MessageBox.Show("Please enter your password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            myqry = "INSERT INTO accounts(Passwords,Usernames) "
            myqry = myqry + "VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "')"
            mycmd = New OleDbCommand
            With mycmd
                .CommandText = myqry
                .Connection = conn
                .ExecuteNonQuery()
            End With

        End If
    End Sub
End Class






Please help me! :((

Is This A Good Question/Topic? 0
  • +

Replies To: Why I can't connect to database?

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

Re: Why I can't connect to database?

Posted 29 January 2013 - 06:44 PM

Remove the Try..Catch block to allow any error message(s) to be shown then let us know the error message(s) you receive.
Was This Post Helpful? 0
  • +
  • -

#3 jonbon004  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 12-July 12

Re: Why I can't connect to database?

Posted 29 January 2013 - 07:35 PM

View Postandrewsw, on 29 January 2013 - 06:44 PM, said:

Remove the Try..Catch block to allow any error message(s) to be shown then let us know the error message(s) you receive.

ok, i'll try it sir.
Was This Post Helpful? 0
  • +
  • -

#4 jonbon004  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 12-July 12

Re: Why I can't connect to database?

Posted 29 January 2013 - 07:51 PM

View Postandrewsw, on 29 January 2013 - 06:44 PM, said:

Remove the Try..Catch block to allow any error message(s) to be shown then let us know the error message(s) you receive.

Sir! there is an error at

IF .State = ConnectionState.Open Then .Close()
`It says that NullExceptionRefrenceUnhandled

Object refrence not set to an instance of an object.

TroubleShooting Tips:

Use the "new" keyword to create an object instance.
Check to determine if the object is null before calling the method.
Get general help for this exception.
and so on. .


#Region "Connection"
    Sub ConntoDB()
        Try
            With conn
                If .State = ConnectionState.Open Then .Close()
                .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=votersAccounts.accdb"
                .Open()
            End With
        Finally
        End Try
    End Sub
#End Region




Sir! please help me! =((
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Why I can't connect to database?

Posted 29 January 2013 - 09:19 PM

It appears you have not initialized the conn object. Not a great mystery use the New keyword like it states in the error.

Dim co As New ClassObject 'example of initialization.


This is a fundamental knowledge point for all your future works, please get a book and learn the basics - it will serve you well.
Was This Post Helpful? 1
  • +
  • -

#6 jonbon004  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 12-July 12

Re: Why I can't connect to database?

Posted 31 January 2013 - 06:38 PM

View Post_HAWK_, on 29 January 2013 - 09:19 PM, said:

It appears you have not initialized the conn object. Not a great mystery use the New keyword like it states in the error.

Dim co As New ClassObject 'example of initialization.


This is a fundamental knowledge point for all your future works, please get a book and learn the basics - it will serve you well.


THANK YOU SIR! I did it! I did it! I did it! Hooray! Thank you again! =D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1