Newbie with Database Connection Error

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
 
G

Guest

You need to set a breakpoint (or write out the values) to check the value
being passed in to your setString method. If the combobox has not had its
value changed yet (even if you populated the combobox), it will have a -1
SelectedIndex value.

If you do not have anything fancy happening in the SelectedIndexChanged
event of your combobox, you can programatically set the combobox to have a
valid default option "selected" just in case someone clicks the button.

Something like this after you initialize your combobox:

If Me.cboSelectDB.Items.Count > 0 Then Me.cboSelectDB.SelectedIndex = 0;

HTH,

John Scragg
 

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

Similar Threads


Top