Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. still cant do it(add to database)

still cant do it(add to database)

Scheduled Pinned Locked Moved Visual Basic
csharpdatabasevisual-studiodebugginghelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    peteyshrew
    wrote on last edited by
    #1

    found out the problem with the code...i think. when i click to add it to the database the msgbox comes up and basically says it cant find the path to the database... i don't know why, the database file is in the debug in the same project. message:- 'could not find file 'U:\visual studio 2005\projects\manager login\manager login\bin\debug\PCBank' Imports System.Data.OleDb Public Class cust Public conn As OleDbConnection Public comm As OleDbCommand Public dr As OleDbDataReader Public da As OleDbDataAdapter Dim icount As Integer Dim str As String Private Sub cust_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load conn = New OleDbConnection 'establishes that conn is a new connection conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PCBank.mdb" 'the name of the connection (name of the database on the end) conn.Open() 'opens the connection comm = New OleDbCommand 'establishes that comm is a new command comm.Connection = conn 'the connection for the command is the connection specified comm.CommandType = CommandType.Text 'the commands are in text End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Try conn = New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=PCBank") conn.Open() comm = New OleDbCommand("insert into customers(CustomerID,CustomerSurname,CustomerForename) values('" & (tbID.Text) & "','" & (tbSur.Text) & "','" & (tbFor.Text) & "')", conn) icount = comm.ExecuteNonQuery() MsgBox("record inserted") Catch ex As Exception MsgBox(ex.Message) End Try conn.Close() End Sub End Class

    P K 2 Replies Last reply
    0
    • P peteyshrew

      found out the problem with the code...i think. when i click to add it to the database the msgbox comes up and basically says it cant find the path to the database... i don't know why, the database file is in the debug in the same project. message:- 'could not find file 'U:\visual studio 2005\projects\manager login\manager login\bin\debug\PCBank' Imports System.Data.OleDb Public Class cust Public conn As OleDbConnection Public comm As OleDbCommand Public dr As OleDbDataReader Public da As OleDbDataAdapter Dim icount As Integer Dim str As String Private Sub cust_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load conn = New OleDbConnection 'establishes that conn is a new connection conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PCBank.mdb" 'the name of the connection (name of the database on the end) conn.Open() 'opens the connection comm = New OleDbCommand 'establishes that comm is a new command comm.Connection = conn 'the connection for the command is the connection specified comm.CommandType = CommandType.Text 'the commands are in text End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Try conn = New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=PCBank") conn.Open() comm = New OleDbCommand("insert into customers(CustomerID,CustomerSurname,CustomerForename) values('" & (tbID.Text) & "','" & (tbSur.Text) & "','" & (tbFor.Text) & "')", conn) icount = comm.ExecuteNonQuery() MsgBox("record inserted") Catch ex As Exception MsgBox(ex.Message) End Try conn.Close() End Sub End Class

      P Offline
      P Offline
      peteyshrew
      wrote on last edited by
      #2

      don't worry about it. finally managed to figure it out. it was something so simple its stupid. CustomerForename should of been CustomerForenames PCBank needed .mdb at the end. i feel like i've been wasting my entire life(or a couple of weeks) thanks for all your help. i'll probably be bugging you again soon

      D 1 Reply Last reply
      0
      • P peteyshrew

        found out the problem with the code...i think. when i click to add it to the database the msgbox comes up and basically says it cant find the path to the database... i don't know why, the database file is in the debug in the same project. message:- 'could not find file 'U:\visual studio 2005\projects\manager login\manager login\bin\debug\PCBank' Imports System.Data.OleDb Public Class cust Public conn As OleDbConnection Public comm As OleDbCommand Public dr As OleDbDataReader Public da As OleDbDataAdapter Dim icount As Integer Dim str As String Private Sub cust_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load conn = New OleDbConnection 'establishes that conn is a new connection conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PCBank.mdb" 'the name of the connection (name of the database on the end) conn.Open() 'opens the connection comm = New OleDbCommand 'establishes that comm is a new command comm.Connection = conn 'the connection for the command is the connection specified comm.CommandType = CommandType.Text 'the commands are in text End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Try conn = New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=PCBank") conn.Open() comm = New OleDbCommand("insert into customers(CustomerID,CustomerSurname,CustomerForename) values('" & (tbID.Text) & "','" & (tbSur.Text) & "','" & (tbFor.Text) & "')", conn) icount = comm.ExecuteNonQuery() MsgBox("record inserted") Catch ex As Exception MsgBox(ex.Message) End Try conn.Close() End Sub End Class

        K Offline
        K Offline
        klaydze
        wrote on last edited by
        #3

        dude try to put your database in the folder "BIN" of your project. i encounter that problem too and i fix it when i put my database in the folder "BIN" of my project. if the error still prompt, post it again.

        Don't block the drive way of all the newbies in programming. :))

        1 Reply Last reply
        0
        • P peteyshrew

          don't worry about it. finally managed to figure it out. it was something so simple its stupid. CustomerForename should of been CustomerForenames PCBank needed .mdb at the end. i feel like i've been wasting my entire life(or a couple of weeks) thanks for all your help. i'll probably be bugging you again soon

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          peteyshrew wrote:

          it was something so simple its stupid.

          That happens alot in this business, even to the Pro's! :-> I noticed you're using the same connection string and just retyping it over and over when you need it, at least twice anyway. You might want to consider moving your database code to a seperate layer, or at least moving the connection code to a Shared (static in C#) method, like this:

          Public Class SqlHelpers
          Public Shared GetConnection(ByVal OleDbFileName As String) As OleDbConnection
          ' Your connection string goes here. Whether it's hard coded (bad idea!), or
          ' stored in the registry or in an app.config file, this method should
          ' retrieve it and create a new connection object out of it.
          '
          ' This example will see if the specified database exists in the same path
          ' the .EXE was launched from, and if so, create a new OleDbConnection out of it.
          Dim FullPath As String = Path.Combine(Application.StartupPath, OleDbFileName)
          If File.Exists(FullPath) Then
          Dim connString As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", FullPath)
          Return New OleDbConnection(connString)
          Else
          Throw New FileNotFoundException("Unable to find the database file " & _
          OleDbFileName & " in the application startup path!")
          End If
          End Sub

          .
          . other helper methods...
          .
          

          End Class

          When you want to get a new connection to the database, just do this:

          Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
          Try
          Dim conn As OleDbConnection = SqlHelpers.GetConnection("PCBank.mdb")
          Dim comm As New OleDbCommand("insert into customers CustomerID, CustomerSurname, CustomerForename) " & _
          "values('" & (tbID.Text) & "','" & (tbSur.Text) & "','" & (tbFor.Text) & "')", conn)
          icount = comm.ExecuteNonQuery()
          MsgBox("record inserted")
          Catch ex As Exception
          MsgBox(ex.Message)
          Finally
          If Not IsNothing(conn) AndAlso conn.State <> ConnectionState.Closed Then
          conn.Close()
          End If
          End Try
          End Sub

          This is nowhere near the "perfect" implementation, but it gives you an idea of what should be going on.

          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic

          P 1 Reply Last reply
          0
          • D Dave Kreskowiak

            peteyshrew wrote:

            it was something so simple its stupid.

            That happens alot in this business, even to the Pro's! :-> I noticed you're using the same connection string and just retyping it over and over when you need it, at least twice anyway. You might want to consider moving your database code to a seperate layer, or at least moving the connection code to a Shared (static in C#) method, like this:

            Public Class SqlHelpers
            Public Shared GetConnection(ByVal OleDbFileName As String) As OleDbConnection
            ' Your connection string goes here. Whether it's hard coded (bad idea!), or
            ' stored in the registry or in an app.config file, this method should
            ' retrieve it and create a new connection object out of it.
            '
            ' This example will see if the specified database exists in the same path
            ' the .EXE was launched from, and if so, create a new OleDbConnection out of it.
            Dim FullPath As String = Path.Combine(Application.StartupPath, OleDbFileName)
            If File.Exists(FullPath) Then
            Dim connString As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", FullPath)
            Return New OleDbConnection(connString)
            Else
            Throw New FileNotFoundException("Unable to find the database file " & _
            OleDbFileName & " in the application startup path!")
            End If
            End Sub

            .
            . other helper methods...
            .
            

            End Class

            When you want to get a new connection to the database, just do this:

            Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
            Try
            Dim conn As OleDbConnection = SqlHelpers.GetConnection("PCBank.mdb")
            Dim comm As New OleDbCommand("insert into customers CustomerID, CustomerSurname, CustomerForename) " & _
            "values('" & (tbID.Text) & "','" & (tbSur.Text) & "','" & (tbFor.Text) & "')", conn)
            icount = comm.ExecuteNonQuery()
            MsgBox("record inserted")
            Catch ex As Exception
            MsgBox(ex.Message)
            Finally
            If Not IsNothing(conn) AndAlso conn.State <> ConnectionState.Closed Then
            conn.Close()
            End If
            End Try
            End Sub

            This is nowhere near the "perfect" implementation, but it gives you an idea of what should be going on.

            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic

            P Offline
            P Offline
            Psycho Coder Extreme
            wrote on last edited by
            #5

            The way I work with connection strings is I store them in the app.config file then I have a function to retrieve it like this:

            Public Shared Function GetConnectionString(ByVal strConnection As String) As String
            Dim strReturn As New String("")
            If Not String.IsNullOrEmpty(strConnection) Then
            strReturn = ConfigurationManager.ConnectionStrings(strConnection).ConnectionString
            Else
            strReturn = ConfigurationManager.ConnectionStrings("YourConnectionStringName").ConnectionString
            End If
            Return strReturn
            End Function

            I do it this way that way I can have multiple connection strings and just pass the name of the connection string, otherwise it defaults to the default connection.

            SELECT * FROM Users WHERE Clue > 0 ERROR: 0 Rows Returned

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups