Newbie with Database Connection Problem


N

No_So_Clever

Hi,

Wonder if Somebody could help me? I`ve been searching all over for a
solution but am totally lost.

I have a Class called Connection as below:

Public Class Connection
Private Shared mConnectionsstring As String =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
Private Shared mLocation As String
Private Shared connArray As ArrayList
Public Shared Sub setString(ByVal ind As Integer)
Dim connArray As New ArrayList
connArray.Add("J:\Database\MasterDB.mdb")
connArray.Add("J:\Database\TestDB.mdb")
mLocation = connArray(ind).ToString
End Sub
Public Shared ReadOnly Property [String]() As String
Get
Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mLocation & ";"
End Get
End Property
End Class

On my Main Form I have my Database Connection Setup as:

Dim UP As New OleDb.OleDbCommand()
Dim Con As New OleDb.OleDbConnection()
Connection.setString(Me.cboSelectDB.SelectedIndex)
Con.ConnectionString = Connection.String
UP.Connection = Con
UP.CommandType = CommandType.Text
UP.CommandText = "Select * From Passwords Where Users_Name=? and
Users_Password=?"
End Sub

But when I run my App it Errors on line "mLocation =
connArray(ind).ToString" with the following error: "Additional information:
Index was out of range. Must be non-negative and less than the size of the
collection"

Anybody point me in the right direction?
Many Thanks in Advance
Regards
Not_So_Clever
 
Ad

Advertisements

N

No_So_Clever

Hi

ind is the location of my Database.i think,

To be honest im a newbie and i took th code from another prog, to try nd use
in my app. So im a little unaware and now vey confused.

Many Thanks
NSC

W.G. Ryan - MVP said:
What is the value of Ind?
No_So_Clever said:
Hi,

Wonder if Somebody could help me? I`ve been searching all over for a
solution but am totally lost.

I have a Class called Connection as below:

Public Class Connection
Private Shared mConnectionsstring As String =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
Private Shared mLocation As String
Private Shared connArray As ArrayList
Public Shared Sub setString(ByVal ind As Integer)
Dim connArray As New ArrayList
connArray.Add("J:\Database\MasterDB.mdb")
connArray.Add("J:\Database\TestDB.mdb")
mLocation = connArray(ind).ToString
End Sub
Public Shared ReadOnly Property [String]() As String
Get
Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mLocation & ";"
End Get
End Property
End Class

On my Main Form I have my Database Connection Setup as:

Dim UP As New OleDb.OleDbCommand()
Dim Con As New OleDb.OleDbConnection()
Connection.setString(Me.cboSelectDB.SelectedIndex)
Con.ConnectionString = Connection.String
UP.Connection = Con
UP.CommandType = CommandType.Text
UP.CommandText = "Select * From Passwords Where Users_Name=? and
Users_Password=?"
End Sub

But when I run my App it Errors on line "mLocation =
connArray(ind).ToString" with the following error: "Additional
information: Index was out of range. Must be non-negative and less than
the size of the collection"

Anybody point me in the right direction?
Many Thanks in Advance
Regards
Not_So_Clever
 
W

W.G. Ryan - MVP

Put a breakpoint right before the line that references ind and see what the
value is. Or use Debug.WriteLine(ind.ToString()); and see what that tells
you. Based on teh exception I think that may be the problem.
No_So_Clever said:
Hi

ind is the location of my Database.i think,

To be honest im a newbie and i took th code from another prog, to try nd
use in my app. So im a little unaware and now vey confused.

Many Thanks
NSC

W.G. Ryan - MVP said:
What is the value of Ind?
No_So_Clever said:
Hi,

Wonder if Somebody could help me? I`ve been searching all over for a
solution but am totally lost.

I have a Class called Connection as below:

Public Class Connection
Private Shared mConnectionsstring As String =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
Private Shared mLocation As String
Private Shared connArray As ArrayList
Public Shared Sub setString(ByVal ind As Integer)
Dim connArray As New ArrayList
connArray.Add("J:\Database\MasterDB.mdb")
connArray.Add("J:\Database\TestDB.mdb")
mLocation = connArray(ind).ToString
End Sub
Public Shared ReadOnly Property [String]() As String
Get
Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mLocation & ";"
End Get
End Property
End Class

On my Main Form I have my Database Connection Setup as:

Dim UP As New OleDb.OleDbCommand()
Dim Con As New OleDb.OleDbConnection()
Connection.setString(Me.cboSelectDB.SelectedIndex)
Con.ConnectionString = Connection.String
UP.Connection = Con
UP.CommandType = CommandType.Text
UP.CommandText = "Select * From Passwords Where Users_Name=? and
Users_Password=?"
End Sub

But when I run my App it Errors on line "mLocation =
connArray(ind).ToString" with the following error: "Additional
information: Index was out of range. Must be non-negative and less than
the size of the collection"

Anybody point me in the right direction?
Many Thanks in Advance
Regards
Not_So_Clever
 
N

No_So_Clever

Hi there Thanks again for your help but ....Sorry I don`t understand what
you mean:(

Like I said im a newbie - would I put:

Dim connArray As New ArrayList
connArray.Add("J:\Database\MasterDB.mdb")
connArray.Add("J:\Database\TestDB.mdb")
Try
mLocation = connArray(ind).ToString
Catch indException As System.Exception
Throw indException
End Try

Cheers
NSC

W.G. Ryan - MVP said:
Put a breakpoint right before the line that references ind and see what
the value is. Or use Debug.WriteLine(ind.ToString()); and see what that
tells you. Based on teh exception I think that may be the problem.
No_So_Clever said:
Hi

ind is the location of my Database.i think,

To be honest im a newbie and i took th code from another prog, to try nd
use in my app. So im a little unaware and now vey confused.

Many Thanks
NSC

W.G. Ryan - MVP said:
What is the value of Ind?
Hi,

Wonder if Somebody could help me? I`ve been searching all over for a
solution but am totally lost.

I have a Class called Connection as below:

Public Class Connection
Private Shared mConnectionsstring As String =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
Private Shared mLocation As String
Private Shared connArray As ArrayList
Public Shared Sub setString(ByVal ind As Integer)
Dim connArray As New ArrayList
connArray.Add("J:\Database\MasterDB.mdb")
connArray.Add("J:\Database\TestDB.mdb")
mLocation = connArray(ind).ToString
End Sub
Public Shared ReadOnly Property [String]() As String
Get
Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mLocation &
";"
End Get
End Property
End Class

On my Main Form I have my Database Connection Setup as:

Dim UP As New OleDb.OleDbCommand()
Dim Con As New OleDb.OleDbConnection()
Connection.setString(Me.cboSelectDB.SelectedIndex)
Con.ConnectionString = Connection.String
UP.Connection = Con
UP.CommandType = CommandType.Text
UP.CommandText = "Select * From Passwords Where Users_Name=? and
Users_Password=?"
End Sub

But when I run my App it Errors on line "mLocation =
connArray(ind).ToString" with the following error: "Additional
information: Index was out of range. Must be non-negative and less
than the size of the collection"

Anybody point me in the right direction?
Many Thanks in Advance
Regards
Not_So_Clever
 
W

W.G. Ryan - MVP

Ok, but what value does ind have in this line?
mLocation = connArray(ind).ToString
if this is a winforms application, stick this in the line before it:

MessageBox.Show(ind.ToString())

No_So_Clever said:
Hi there Thanks again for your help but ....Sorry I don`t understand what
you mean:(

Like I said im a newbie - would I put:

Dim connArray As New ArrayList
connArray.Add("J:\Database\MasterDB.mdb")
connArray.Add("J:\Database\TestDB.mdb")
Try
mLocation = connArray(ind).ToString
Catch indException As System.Exception
Throw indException
End Try

Cheers
NSC

W.G. Ryan - MVP said:
Put a breakpoint right before the line that references ind and see what
the value is. Or use Debug.WriteLine(ind.ToString()); and see what that
tells you. Based on teh exception I think that may be the problem.
No_So_Clever said:
Hi

ind is the location of my Database.i think,

To be honest im a newbie and i took th code from another prog, to try nd
use in my app. So im a little unaware and now vey confused.

Many Thanks
NSC

What is the value of Ind?
Hi,

Wonder if Somebody could help me? I`ve been searching all over for a
solution but am totally lost.

I have a Class called Connection as below:

Public Class Connection
Private Shared mConnectionsstring As String =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
Private Shared mLocation As String
Private Shared connArray As ArrayList
Public Shared Sub setString(ByVal ind As Integer)
Dim connArray As New ArrayList
connArray.Add("J:\Database\MasterDB.mdb")
connArray.Add("J:\Database\TestDB.mdb")
mLocation = connArray(ind).ToString
End Sub
Public Shared ReadOnly Property [String]() As String
Get
Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mLocation &
";"
End Get
End Property
End Class

On my Main Form I have my Database Connection Setup as:

Dim UP As New OleDb.OleDbCommand()
Dim Con As New OleDb.OleDbConnection()
Connection.setString(Me.cboSelectDB.SelectedIndex)
Con.ConnectionString = Connection.String
UP.Connection = Con
UP.CommandType = CommandType.Text
UP.CommandText = "Select * From Passwords Where Users_Name=? and
Users_Password=?"
End Sub

But when I run my App it Errors on line "mLocation =
connArray(ind).ToString" with the following error: "Additional
information: Index was out of range. Must be non-negative and less
than the size of the collection"

Anybody point me in the right direction?
Many Thanks in Advance
Regards
Not_So_Clever
 
Ad

Advertisements

N

No_So_Clever

Hi there,
I`ve added the line in and depending on what I select in my Combo Box
(Database 1 or Database 2) it gives me a 0 or a 1. Now tho my App Fails on

Con.Open

with the following Error:

Additional information: No error information available:
DB_SEC_E_AUTH_FAILED(0x80040E4D).

My Code is:

Dim UP As New OleDb.OleDbCommand
Dim Con As New OleDb.OleDbConnection
Dim Read As OleDb.OleDbDataReader
Dim Plus As Integer
Public CurrentForm As System.Windows.Forms.Form
Private Sub Login_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Connection string
Con.ConnectionString = Connection.String
'Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=J:\Database\MasterDB.mdb"
UP.Connection = Con
UP.CommandType = CommandType.Text
UP.CommandText = "Select * From Passwords Where Users_Name=? and
Users_Password=?"
End Sub
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnLogin.Click
Connection.setString(Me.cboSelectDB.SelectedIndex)
If txtUserName.Text.Trim = "" Then
MessageBox.Show("Please fill your User Name !", "Password",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtUserName.Focus()
Exit Sub
End If
If txtPassword.Text.Trim = "" Then
MessageBox.Show("Please fill your Password !", "Password",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtPassword.Focus()
Exit Sub
End If
'Admin
UP.Parameters.Add("user", Data.OleDb.OleDbType.Variant)
UP.Parameters.Add("password", Data.OleDb.OleDbType.Variant)
UP.Parameters("user").Value = txtUserName.Text
UP.Parameters("password").Value = txtPassword.Text
'Open database connection
Con.Open()
' Read information from database
Read = UP.ExecuteReader
ar()
' Close database connection
Con.Close()
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub

Many Thanks
Si


W.G. Ryan - MVP said:
Ok, but what value does ind have in this line?
mLocation = connArray(ind).ToString
if this is a winforms application, stick this in the line before it:

MessageBox.Show(ind.ToString())

No_So_Clever said:
Hi there Thanks again for your help but ....Sorry I don`t understand what
you mean:(

Like I said im a newbie - would I put:

Dim connArray As New ArrayList
connArray.Add("J:\Database\MasterDB.mdb")
connArray.Add("J:\Database\TestDB.mdb")
Try
mLocation = connArray(ind).ToString
Catch indException As System.Exception
Throw indException
End Try

Cheers
NSC

W.G. Ryan - MVP said:
Put a breakpoint right before the line that references ind and see what
the value is. Or use Debug.WriteLine(ind.ToString()); and see what that
tells you. Based on teh exception I think that may be the problem.
Hi

ind is the location of my Database.i think,

To be honest im a newbie and i took th code from another prog, to try
nd use in my app. So im a little unaware and now vey confused.

Many Thanks
NSC

What is the value of Ind?
Hi,

Wonder if Somebody could help me? I`ve been searching all over for a
solution but am totally lost.

I have a Class called Connection as below:

Public Class Connection
Private Shared mConnectionsstring As String =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
Private Shared mLocation As String
Private Shared connArray As ArrayList
Public Shared Sub setString(ByVal ind As Integer)
Dim connArray As New ArrayList
connArray.Add("J:\Database\MasterDB.mdb")
connArray.Add("J:\Database\TestDB.mdb")
mLocation = connArray(ind).ToString
End Sub
Public Shared ReadOnly Property [String]() As String
Get
Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mLocation &
";"
End Get
End Property
End Class

On my Main Form I have my Database Connection Setup as:

Dim UP As New OleDb.OleDbCommand()
Dim Con As New OleDb.OleDbConnection()
Connection.setString(Me.cboSelectDB.SelectedIndex)
Con.ConnectionString = Connection.String
UP.Connection = Con
UP.CommandType = CommandType.Text
UP.CommandText = "Select * From Passwords Where Users_Name=? and
Users_Password=?"
End Sub

But when I run my App it Errors on line "mLocation =
connArray(ind).ToString" with the following error: "Additional
information: Index was out of range. Must be non-negative and less
than the size of the collection"

Anybody point me in the right direction?
Many Thanks in Advance
Regards
Not_So_Clever
 
Ad

Advertisements

W

W.G. Ryan - MVP

So it fails when you try to open the connection? The first thing to do then
is Try/catch the open statement and see what the Exception tells you if
anything. Verify the connection string, the values of the paramaters etc
and just try to pick off the low hanging fruit . If it's 0 or 1, then
you're going to connect to different databases. so check the Connection
String property ( you acn use a File.Exists as well for the path /file of
the *mdb file to make sure it exists). Make sure the file does exist and
that you have permission to acccess it. Also, just to help narrow thigns
down, use a For loop to access each fo the values in the command's
parameters collection, make sure the values are what you expect them to be,
it

For the file exists stuff, use this:


' At the beginning of your code file, use Imports System.Diagnostics
Imports System.IO 'If they aren't already there
Debug.Assert (File.Exists("J:\Database\MasterDB.mdb"),
"J:\Database\MasterDB.mdb "& " does not appear to exist")
Debug.Assert (File.Exists("J:\Database\TestDB.mdb"), "J:\Database\TestDB.mdb
"& " does not appear to exist")


For i as Int32 = 0 to UP.Parameters.Count -1
Debug.WriteLine(UP.Parameters(i).ToString())
Next i

Also, can you isolate what is causing the value of ind to change?
No_So_Clever said:
Hi there,
I`ve added the line in and depending on what I select in my Combo Box
(Database 1 or Database 2) it gives me a 0 or a 1. Now tho my App Fails on

Con.Open

with the following Error:

Additional information: No error information available:
DB_SEC_E_AUTH_FAILED(0x80040E4D).

My Code is:

Dim UP As New OleDb.OleDbCommand
Dim Con As New OleDb.OleDbConnection
Dim Read As OleDb.OleDbDataReader
Dim Plus As Integer
Public CurrentForm As System.Windows.Forms.Form
Private Sub Login_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Connection string
Con.ConnectionString = Connection.String
'Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=J:\Database\MasterDB.mdb"
UP.Connection = Con
UP.CommandType = CommandType.Text
UP.CommandText = "Select * From Passwords Where Users_Name=? and
Users_Password=?"
End Sub
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnLogin.Click
Connection.setString(Me.cboSelectDB.SelectedIndex)
If txtUserName.Text.Trim = "" Then
MessageBox.Show("Please fill your User Name !", "Password",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtUserName.Focus()
Exit Sub
End If
If txtPassword.Text.Trim = "" Then
MessageBox.Show("Please fill your Password !", "Password",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtPassword.Focus()
Exit Sub
End If
'Admin
UP.Parameters.Add("user", Data.OleDb.OleDbType.Variant)
UP.Parameters.Add("password", Data.OleDb.OleDbType.Variant)
UP.Parameters("user").Value = txtUserName.Text
UP.Parameters("password").Value = txtPassword.Text
'Open database connection
Con.Open()
' Read information from database
Read = UP.ExecuteReader
ar()
' Close database connection
Con.Close()
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub

Many Thanks
Si


W.G. Ryan - MVP said:
Ok, but what value does ind have in this line?
mLocation = connArray(ind).ToString
if this is a winforms application, stick this in the line before it:

MessageBox.Show(ind.ToString())

No_So_Clever said:
Hi there Thanks again for your help but ....Sorry I don`t understand
what you mean:(

Like I said im a newbie - would I put:

Dim connArray As New ArrayList
connArray.Add("J:\Database\MasterDB.mdb")
connArray.Add("J:\Database\TestDB.mdb")
Try
mLocation = connArray(ind).ToString
Catch indException As System.Exception
Throw indException
End Try

Cheers
NSC

Put a breakpoint right before the line that references ind and see what
the value is. Or use Debug.WriteLine(ind.ToString()); and see what
that tells you. Based on teh exception I think that may be the
problem.
Hi

ind is the location of my Database.i think,

To be honest im a newbie and i took th code from another prog, to try
nd use in my app. So im a little unaware and now vey confused.

Many Thanks
NSC

What is the value of Ind?
Hi,

Wonder if Somebody could help me? I`ve been searching all over for a
solution but am totally lost.

I have a Class called Connection as below:

Public Class Connection
Private Shared mConnectionsstring As String =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
Private Shared mLocation As String
Private Shared connArray As ArrayList
Public Shared Sub setString(ByVal ind As Integer)
Dim connArray As New ArrayList
connArray.Add("J:\Database\MasterDB.mdb")
connArray.Add("J:\Database\TestDB.mdb")
mLocation = connArray(ind).ToString
End Sub
Public Shared ReadOnly Property [String]() As String
Get
Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mLocation &
";"
End Get
End Property
End Class

On my Main Form I have my Database Connection Setup as:

Dim UP As New OleDb.OleDbCommand()
Dim Con As New OleDb.OleDbConnection()
Connection.setString(Me.cboSelectDB.SelectedIndex)
Con.ConnectionString = Connection.String
UP.Connection = Con
UP.CommandType = CommandType.Text
UP.CommandText = "Select * From Passwords Where Users_Name=? and
Users_Password=?"
End Sub

But when I run my App it Errors on line "mLocation =
connArray(ind).ToString" with the following error: "Additional
information: Index was out of range. Must be non-negative and less
than the size of the collection"

Anybody point me in the right direction?
Many Thanks in Advance
Regards
Not_So_Clever
 

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