Click here to Skip to main content
15,917,795 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Cross-thread operation not valid: Pin
Luc Pattyn29-Aug-07 6:44
sitebuilderLuc Pattyn29-Aug-07 6:44 
GeneralRe: Cross-thread operation not valid: Pin
Cory Kimble29-Aug-07 9:27
Cory Kimble29-Aug-07 9:27 
GeneralRe: Cross-thread operation not valid: Pin
Luc Pattyn29-Aug-07 9:32
sitebuilderLuc Pattyn29-Aug-07 9:32 
GeneralRe: Cross-thread operation not valid: Pin
Cory Kimble29-Aug-07 10:03
Cory Kimble29-Aug-07 10:03 
GeneralRe: Cross-thread operation not valid: Pin
Luc Pattyn29-Aug-07 11:22
sitebuilderLuc Pattyn29-Aug-07 11:22 
GeneralRe: Cross-thread operation not valid: [modified] Pin
Cory Kimble30-Aug-07 4:08
Cory Kimble30-Aug-07 4:08 
QuestionGet file ext. from database Pin
jds120729-Aug-07 3:46
jds120729-Aug-07 3:46 
AnswerRe: Get file ext. from database Pin
Dave Kreskowiak29-Aug-07 7:49
mveDave Kreskowiak29-Aug-07 7:49 
OK. You've got a huge spaghetti problem here. For example, just look at the single line of code in your Form's Load event handler.
GetConnectionString()

Now, I'm all for self-documenting code and the name you chose for this method is pretty decent, but I would expect a method with that name to actually return a connection string. Your GetConnectionString method does everything BUT return a connection string! Why do you have a method with this name creating a DataTable, creating columns in it, opening a connection, executing an SQL command, filling that DataTable, and NOT returning a connection string?

You have this problem throughout this bowl of noodles you call code and it's this very problem that's causing your issues now that you want to change one little piece of functionality.

This stuff should be broken down into more methods, seperating and encapsulating functionality. You got the first method name correct, GetConnectionString. Now make it actually return a connection string!
Public Class DatabaseLayer
    Private Shared Function GetConnectionString(ByVal filename As String) As String
        ' Build the full path to the specified database file and see if this
        ' file exists
        Dim fullPath As String = Path.Combine(Application.StartupPath, filename)
        If Not File.Exists(fullPath) Then
            Throw New FileNotFoundException("Unable to locate the database file {0} in the application folder.")
        End If
 
        ' Get the connection string template
        Dim connStringTemplate As String = My.Settings.ConnectionString
 
        ' Build and return the completed connection string
        Return String.Format(connStringTemplate, Path.Combine(Application.StartupPath, filename))
    End Function
 
    Private Shared Function CreateDatabaseConnection(ByVal filename As String) As OleDbConnection
        ' Get a connection string for the specified database file
        Dim connString As String = GetConnectionString(filename)
 
        ' Return an OleDbConnection object
        Return New OleDbConnect(connString)
    End Function
 
    Private Shared Function GetFileExcludeTableSelectCommand() As OleDbCommand
        Dim selectSql As String = "SELECT " & _
                "ExcludeID, ExcludePath, ExcludeFileName FROM tblExclude"
        Return New OleDbCommand(selectSql)
    End Function
 
    Private Shared Function CreateFileExcludeTableDataAdapter(ByVal selectCommand As OleDbCommand, ByVal connection As OleDbConnection) As OleDbDataAdatper
        Dim da As New OleDbDataAdapter(comm, conn)
        ' Put code here to add any command parameters and use the OleDbCommandBuilder
        ' to finish building the DataAdpters UPDATE, DELETE, and INSERT commands.
        Return da
    End Function
 
    Public Shared Function GetFileExcludeTable() As DataTable
        Dim dt As New DataTable("FileExcludeTable")
        Dim conn As OleDbConnection = CreateDatabaseConnection("File.mdb")
        Dim comm As OleDbCommand = GetFileExcludeTableSelectCommand()
 
        Using da As OleDbDataAdapter = CreateFileExcludeTableDataAdapter(comm, conn)
            da.Fill(dt)
        End Using
 
        Return dt
    End Function

This is just a small SAMPLE of a data layer, meant to demonstrate encapsulation and modularization.


A guide to posting questions on CodeProject[^]

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


GeneralRe: Get file ext. from database Pin
jds120729-Aug-07 8:15
jds120729-Aug-07 8:15 
GeneralRe: Get file ext. from database Pin
Dave Kreskowiak29-Aug-07 8:30
mveDave Kreskowiak29-Aug-07 8:30 
Questionhelp Pin
somunu29-Aug-07 1:49
somunu29-Aug-07 1:49 
AnswerRe: help Pin
Christian Graus29-Aug-07 2:04
protectorChristian Graus29-Aug-07 2:04 
QuestionProblem Deploying my Application with Access with the Correct Mappings Pin
Vimalsoft(Pty) Ltd29-Aug-07 0:18
professionalVimalsoft(Pty) Ltd29-Aug-07 0:18 
AnswerRe: Problem Deploying my Application with Access with the Correct Mappings Pin
Dave Kreskowiak29-Aug-07 6:54
mveDave Kreskowiak29-Aug-07 6:54 
GeneralRe: Problem Deploying my Application with Access with the Correct Mappings Pin
Vimalsoft(Pty) Ltd29-Aug-07 20:09
professionalVimalsoft(Pty) Ltd29-Aug-07 20:09 
GeneralRe: Problem Deploying my Application with Access with the Correct Mappings Pin
Dave Kreskowiak30-Aug-07 1:42
mveDave Kreskowiak30-Aug-07 1:42 
GeneralRe: Problem Deploying my Application with Access with the Correct Mappings Pin
Vimalsoft(Pty) Ltd30-Aug-07 3:30
professionalVimalsoft(Pty) Ltd30-Aug-07 3:30 
QuestionCrystal Report crystaldecisions.vsdesigner error Pin
aqui_i28-Aug-07 23:31
aqui_i28-Aug-07 23:31 
QuestionDeleting an element from an array Pin
KOTO-G28-Aug-07 21:38
KOTO-G28-Aug-07 21:38 
AnswerRe: Deleting an element from an array Pin
Csharp™28-Aug-07 22:08
Csharp™28-Aug-07 22:08 
AnswerRe: Deleting an element from an array Pin
Christian Graus29-Aug-07 0:16
protectorChristian Graus29-Aug-07 0:16 
AnswerRe: Deleting an element from an array Pin
SHatchard29-Aug-07 3:59
SHatchard29-Aug-07 3:59 
Questionhow to Add ListviewItem in ListViewGroup ? Pin
Rizwan Bashir28-Aug-07 20:48
Rizwan Bashir28-Aug-07 20:48 
AnswerRe: how to Add ListviewItem in ListViewGroup ? Pin
Dave Kreskowiak29-Aug-07 4:39
mveDave Kreskowiak29-Aug-07 4:39 
GeneralRe: how to Add ListviewItem in ListViewGroup ? Pin
Rizwan Bashir29-Aug-07 23:15
Rizwan Bashir29-Aug-07 23:15 

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.