Change connection string of access database in visual studio 2022 - vb.net

Ok so, basically i want to connect an access database (.mdb file) to my project. Now yes i have indeed connected it through the "data origin" wizard but the problem is that when i transfer the exe AND the database to another pc, it doesn't find it (obviously).
Dim appPath As String = My.Application.Info.DirectoryPath
Dim txtconnesione As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & appPath & "\Magazzino.mdb"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TableMagazzinoBindingSource.DataSource = txtconnesione
Me.TableMagazzinoTableAdapter.Fill(Me.MagazzinoDataSet.TableMagazzino)
End Sub
appPath is basically where the program gets started.
With this code it actually shows me the whole grid but it's empty, just like this:
i didn't used a connection string and i didn't copy it in the project (sorry for bad english, if you don't understand i'll try to explain better)

Related

Crystal report Viewer keeps on Asking for Logon ID and Password at runtime

Situation:
I made application using vb.net ,ms access and crystal report
When I run it my computer, I don't encounter any problem at all , but when I install and run it on other computer, crystal report asks for login ID and password
I have 2 tables in the report
The main report and the sub report
the Datasource for my rpt file is:
Datasource:C:\User\Documents\report.accdb
some suggests doing something like this:
myreport.SetDatabaseLogon.("user","password")
But I dont know how to use it and where to input the code
Anyone Familiar with this ? Thank you
I just wanted to provide you with the code that I used when I needed to connect to crystal reports embedded within my Visual Studio project.
Please disregard if you are not using the same method.
In my example, I connect to a SQL database so that the stored procedure my crystal report is using can be loaded.
I always used these subroutines to create a crystal report connection and configure the report with proper settings.
I'm not sure if you can set the connectioninfo with just a userID and password, but I figured this code might give you some ideas about what your code is missing.
If you need more help, please provide code snippets so we can help further diagnose.
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
'Crystal Report Here
Private mycrystalreport1 As CrystalReport1
Private Sub ConfigureCrystalReports()
Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()
mycrystalreport1 = New CrystalReport1()
Dim reportPath As String = Application.StartupPath & "\" & "CrystalReport1.rpt"
mycrystalreport1.Load(reportPath)
CrystalReportViewer1.ReportSource = mycrystalreport1
myConnectionInfo.ServerName = "SQL" 'OR WHATEVER SERVER NAME IS
myConnectionInfo.DatabaseName = "DATABASE_NAME"
myConnectionInfo.UserID = "USER_ID"
myConnectionInfo.Password = "PASSWORD"
SetDBLogonForReport(myConnectionInfo, mycrystalreport1)
End Sub
Private Sub SetDBLogonForReport(ByVal myConnectionInfo As ConnectionInfo, ByVal myReportDocument As ReportDocument)
Try
Dim myTables As Tables = myReportDocument.Database.Tables
For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In myTables
Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo
myTableLogonInfo.ConnectionInfo = myConnectionInfo
myTable.ApplyLogOnInfo(myTableLogonInfo)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ConfigureCrystalReports()
'CAN SET PARAMETERS FOR A STORED PROCEDURE OR LOCAL VARIABLES IN CRYSTAL HERE
End Sub
Hopefully, this can provide you with some insight as to how crystal reports can take in a username and password.
Change your report to use ODBC. Makes it easier to check the database connection on each PC.
I'm guessing that the 2nd PC doesn't have the required database drivers to connect to accdb, but that's just a guess.

How can i store file name and file path to the database in DataGridview?

i am trying to store file name and file path to the database by opening a OpenFileDiologue. here my codes.
NOTE: DB is already connected. properly showing data on datagridview and database is created with ms access named database1.accdb.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim showdialog1 As New OpenFileDialog
showdialog1.Filter = "Supported Video Files. | *.mp4; *.avi; *.wmv; *.mpg; *.mpeg; *.mov; *.webm; *.flv; *.mkv"
showdialog1.Title = "Add Supported Video Files."
If showdialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
AxWindowsMediaPlayer1.URL = showdialog1.FileName
Else
Exit Sub
End If
End Sub
here is a pic of my design. this will help you to understand me.
Click to View the pic
help me to store file name and file path to the database. hope i will get the easiest method.
It's simple :
Public sub Button1_Click
'your connection string
connection.open
Dim cmd as new Oledbcommand("Insert into
[tablename](columnname) values (#mycolumn)",yourconnectionstringname)
If openfiledialog1.dialogresult=dialogresult.ok then
cmd.parametres.add("#mycolumn",sqldbtype.varchar).value=openfiledialog1.filename
cmd.executenonquery()
connection.close
I hope this would help to achieve your goal

Unrecognized database format when filling DataAdapter

I am trying to use listboxes to categorize data and I am trying to use SQL to do so.
form picture
that link is what the form looks like know and what i'm trying to do - to use the listboxes to view the records by student year.
For the first list box here is the code for the first listbox to sort the data by year:
Imports System.Data.OleDb
Public Class viewStudent
Private Sub viewStudent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ProjDataSet1.Details' table. You can move, or remove it, as needed.
Me.DetailsTableAdapter1.Fill(Me.ProjDataSet1.Details)
' OleDbDataAdapter1.Fill(DataSet11)
End Sub
Private Sub lstYear_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstYear.SelectedIndexChanged
Dim Year, SQLString As String
Dim dtDetails As New DataTable()
Dim dbDataAdapter As OleDbDataAdapter
Dim ConnectString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = proj.accdb"
Year = lstYear.Text
SQLString = "SELECT * FROM Details WHERE Year = " & "'" _
& Year & "'" & ""
dbDataAdapter = New OleDbDataAdapter(SQLString, ConnectString)
dbDataAdapter.Fill(dtDetails)
grdRecords.DataSource = dtDetails
End Sub
End Class
But i get the error in the link below:
error
Can someone help to fix this?
Thank you!
The "Microsoft.Jet.OLEDB.4.0" provider is the older 32-bit provider that can only work with .mdb database files. To work with an .accdb database you need to use the newer "Microsoft.ACE.OLEDB.12.0" provider.
Since you have the 64-bit version of Access 2013 installed you already have the 64-bit version of the ACE provider. All you need to do is
Modify the properties of your .NET project to run as a 64-bit application (ref: here), and
Change the connection string in your code to use Provider=Microsoft.ACE.OLEDB.12.0.

How to generate a new MS access file programmatically

I have looked far and wide, in the deepest darkest corners of the internet, but for the life of me, I can not find the correct way to open a NEW Access file and then using vb.net to write data in the database..
The keywords here are NEW database, I don't want to open an existing file.
Is this even possible?
Thanks in advance!
I have finally found the way, thanks to a co-worker of mine
Neither ADO.NET nor ActiveX Data Object (ADO) provides the means to create Microsoft
Access Database. However, we can create Access databases by using the Microsoft Jet OLE DB
Provider and Microsoft ADO Ext. 2.7 for DDL and Security (ADOX) with the COM Interop
layer. To do so, select References from the Project Menu, choose the COM tab, and add a
reference to Microsoft ADO Ext. 2.7 for DDL and Security; then you can use this function.
When you have done this, use the following snippet to create a database
Public Class Form1
Private Sub btnLoad_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnLoad.Click
CreateAccessDatabase("C:\test\testDB.mdb")
MsgBox("Database created")
End Sub
Public Function CreateAccessDatabase( ByVal DatabaseFullPath As String) As Boolean
Dim bAns As Boolean
Dim cat As New ADOX.Catalog()
Try
Dim sCreateString As String
sCreateString =_
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
DatabaseFullPath
cat.Create(sCreateString)
bAns = True
Catch Excep As System.Runtime.InteropServices.COMException
bAns = False
Finally
cat = Nothing
End Try
Return bAns
End Function
End Class

How do I discover the user's Desktop folder?

I'm making a little application in visual studio which loads a ROM in an emulator.
I have two emulators and 20 ROMs.
I made a form and added a few buttons. When you click the Button it opens a new form and closes the old one. Then on the new form I have four buttons: each one loads a different ROM in an emulator. So when you press Button1 this code is triggered:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yellow.Click
Shell("C:\Users\shifty\Desktop\pokemon games\Emulator\VBA\VisualBoyAdvance.exe ""C:\Users\shifty\Desktop\pokemon games\Roms\Yellow\Pokemon Yellow.gb""", vbNormalFocus)
End Sub
It works fine - I click it and it loads the game in the emulator. The bit im having trouble with is the file paths. If I send this application to a friend, it would still look for "C:\Users\shifty\Desktop\" - but that's on my computer, not his.
Is there a way to make the application look for the file on his computer (without changing the file path to (C:\Users\""his user name""\Desktop))
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
This will resolve to be the desktop folder for the current user.
It will even work between XP, vista and Windows 7 properly.
Old post but I have to side with Mc Shifty. You can't assume that everyone is a coding expert. If they were then they wouldn't be here asking questions like that.
None of the answers given above were complete
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)) <<< includes and extra )
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)); <<< extra ) and the ; is C or java not VB which he is obviously using by his example code.
Both of those only give you half of the required code to generate something usable.
Dim s As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
The above code will give you the result needed, c:\users\shifty\desktop
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yellow.Click
Dim s As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Shell(s & "\Desktop\pokemon games\Emulator\VBA\VisualBoyAdvance.exe " & s & "\pokemon games\Roms\Yellow\Pokemon Yellow.gb""", vbNormalFocus)
End Sub
There's a mechanism to get the current user's Desktop directory, using Environment.SpecialFolder.
Usage:
Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
I had problems using the Environment.GetFolderPath method from previous answers.
The following works in VB 2012, My.Computer.FileSystem.SpecialDirectories.Desktop
So, if you have a file on a users desktop named "contacts.txt", the following will display the full path,
' Desktop path
Dim desktopPath = My.Computer.FileSystem.SpecialDirectories.Desktop
' Concatenate desktop path and file name
filePath = desktopPath & "/contacts.txt"
MsgBox(filePath)
Documentation
Really old post at this point, but hey, found what I was looking for.
MC SH1FTY, I assume you have figured this out already, but to do what you are trying to do:
1) Call in that code that Spence wrote as a variable (I'd declare it Globally, but that's my preference. To do that:
Public userDesktopLoc As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
2) Either use this DIRECTLY in your code, or make another string to concatenate a directory:
Option A)
Public emulatorPath As String = userDesktopLoc & "pokemon games\Emulator\VBA\VisualBoyAdvance.exe "
Public romPath As String = userDesktopLoc & "pokemon games\Roms\Yellow\Pokemon Yellow.gb"
Then, within your Subroutine, replace your current Shell statement with:
Shell(emulatorPath & romPath, vbNormalFocus)
Or, Option B, which is thedsz's answer:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yellow.Click
Dim s As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Shell(s & "\Desktop\pokemon games\Emulator\VBA\VisualBoyAdvance.exe " & s & "\pokemon games\Roms\Yellow\Pokemon Yellow.gb""", vbNormalFocus)
End Sub
By using that you guarantee that the emulator is on the users desktop. This is not always the case. I know I move things around that I download or a friend sends to me. It's better to use App.Path and make sure your emulator.exe is in the directory with your little front end program (usually the case).
the answer is simple.
put this at the top of the form
"Public thepath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)"
that ensures that the file is on their desktop!
then" click on your button or whatever you used to open the emu and type
"Process.Start(thepath + "the emulator.exe "+ "the rom you want")
You need to use a file open dialog to choose your path for the two files. Here is an example.
You then use the two paths in your code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yellow.Click
Shell(emulatorPath + "\"" + romPath + "\"", vbNormalFocus)
End Sub