Click here to Skip to main content
15,890,512 members
Home / Discussions / Visual Basic
   

Visual Basic

 
QuestionExtracting a single record from a SQL database Pin
aqzman_19-Jul-07 1:43
aqzman_19-Jul-07 1:43 
AnswerRe: Extracting a single record from a SQL database Pin
Colin Angus Mackay19-Jul-07 1:54
Colin Angus Mackay19-Jul-07 1:54 
GeneralRe: Extracting a single record from a SQL database Pin
aqzman_19-Jul-07 2:10
aqzman_19-Jul-07 2:10 
AnswerRe: help Pin
kubben19-Jul-07 1:46
kubben19-Jul-07 1:46 
Jokemore meaningful subject Pin
Luc Pattyn19-Jul-07 2:43
sitebuilderLuc Pattyn19-Jul-07 2:43 
GeneralRe: more meaningful subject Pin
kubben19-Jul-07 2:51
kubben19-Jul-07 2:51 
QuestionError when connecting to Ms access database Pin
Mekong River19-Jul-07 0:34
Mekong River19-Jul-07 0:34 
AnswerRe: Error when connecting to Ms access database Pin
Dave Kreskowiak19-Jul-07 3:44
mveDave Kreskowiak19-Jul-07 3:44 
There's a few problems with this...

First, this:
Private WithEvents cnnDonorDb As New System.Data.OleDb.OleDbConnection( _
    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\DICDonorDb\DICDornorDb.mdb';" & _
    "Persist Security Info=False")

doesn't need the With Events clause. Well, no unless you're going to handle any of the three events that it exposes. Just about noone does...

Second, in your SQL statement, you specified the name of the database in your table spec. Actually, you treated the name of the table as a database name:
SELECT * FROM tblOrgType.OrgType ORDER BY tblOrgType.OrgType;

assuming that tblOrgType is a table and OrgType is a column in that table:
SELECT OrgType FROM tblOrgType ORDER BY OrgType


Altogether, your code should read more like this: Do NOT hold open a database connection object for the life of your application.
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:\DICDonorDb\DICDornorDb.mdb""; Persist Security Info=False;"
Dim conn As New OldDbConnection(connString)
Dim cmdOrgType As New OleDbCommand

With cmdOrgType
    .Connection = cnnDonorDb
    .CommandType = CommandType.Text
    .CommandText = "SELECT OrgType FROM tblOrgType ORDER BY OrgType"
End With

' Open the connection.
cmdOrgType.Connection.Open()

' Execute the command to the return the list of organization type.
Dim drdResult As OleDbDataReader
drdResult = cmdOrgType.ExecuteReader()

' Get the list of organization type and put it in the list box.
While drdResult.Read
    For colOrgType As Integer = 0 To drdResult.FieldCount - 1
        lsbOrgInfo.Items.Add(drdResult(colOrgType).ToString)
    Next
End While

' Close the result.
drdResult.Close()

' Close the connection
cmdOrgType.Connection.Close()

This, of course, does NOT mean this is good code. There's no exception handling in your code and it makes a bunch of assumptions where it's running in a "perfect world" where everything work the way it should.






A guide to posting questions on CodeProject[^]

Dave Kreskowiak
Microsoft MVP
Visual Developer - Visual Basic
     2006, 2007


GeneralRe: Error when connecting to Ms access database Pin
Mekong River19-Jul-07 17:44
Mekong River19-Jul-07 17:44 
GeneralRe: Error when connecting to Ms access database Pin
Dave Kreskowiak20-Jul-07 2:28
mveDave Kreskowiak20-Jul-07 2:28 
GeneralRe: Error when connecting to Ms access database Pin
Mekong River22-Jul-07 15:39
Mekong River22-Jul-07 15:39 
QuestionHow To AOP IN VB.NET Pin
tomaka18-Jul-07 23:43
tomaka18-Jul-07 23:43 
AnswerRe: How To AOP IN VB.NET Pin
Dave Kreskowiak19-Jul-07 3:29
mveDave Kreskowiak19-Jul-07 3:29 
QuestionProblem on Executing application path in VB6.0 Pin
Srinivas Kaparthi18-Jul-07 23:31
Srinivas Kaparthi18-Jul-07 23:31 
AnswerRe: Problem on Executing application path in VB6.0 Pin
Navneet Hegde19-Jul-07 2:38
Navneet Hegde19-Jul-07 2:38 
GeneralRe: Problem on Executing application path in VB6.0 Pin
Srinivas Kaparthi21-Jul-07 1:02
Srinivas Kaparthi21-Jul-07 1:02 
QuestionAccess form1 from form2. Pin
KOKEMO18-Jul-07 23:23
KOKEMO18-Jul-07 23:23 
AnswerRe: Access form1 from form2. Pin
Colin Angus Mackay18-Jul-07 23:39
Colin Angus Mackay18-Jul-07 23:39 
AnswerRe: Access form1 from form2. Pin
Naji El Kotob19-Jul-07 0:07
Naji El Kotob19-Jul-07 0:07 
AnswerRe: Access form1 from form2. Pin
Dave Kreskowiak19-Jul-07 3:13
mveDave Kreskowiak19-Jul-07 3:13 
GeneralRe: Access form1 from form2. Pin
Paul Conrad19-Jul-07 9:34
professionalPaul Conrad19-Jul-07 9:34 
GeneralRe: Access form1 from form2. Pin
Dave Kreskowiak19-Jul-07 15:55
mveDave Kreskowiak19-Jul-07 15:55 
JokeRe: Access form1 from form2. Pin
Paul Conrad19-Jul-07 16:02
professionalPaul Conrad19-Jul-07 16:02 
Questioneasy way to print datagrid ? Pin
Mr.Kode18-Jul-07 23:16
Mr.Kode18-Jul-07 23:16 
AnswerRe: easy way to print datagrid ? Pin
Dave Kreskowiak19-Jul-07 3:09
mveDave Kreskowiak19-Jul-07 3:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.