still cant do it(add to database)
-
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
-
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
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
-
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
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. :))
-
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
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 SubThis 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
-
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 SubThis 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
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 FunctionI 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