Dim dt As New DataTable
With dt
.Columns.Add("Itemname")
.Columns.Add("Quantity")
.Columns.Add("Price")
.Columns.Add("Total")
End With
For Each dr As DataGridViewRow In Me.DataGridView1.Rows
dt.Rows.Add(dr.Cells("Itemname").Value, dr.Cells("Quantity").Value, dr.Cells("Price").Value, dr.Cells("Total").Value)
Next
Dim rptdoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
rptdoc = New ordersprint
rptdoc.SetDataSource(dt)
can somebody help me with this ? here's my code in printing the data in datagridview ?
Failed to load database is when your VB.net is not able to connect to database itself. The query execution comes later. Check if connection string is proper and MySQL db is able to listen to connection request from outside localhost.
Related
How do I change connection and execute some query in my form load during run time? it's more like opening multiple connections simultaneously but how do I do that?
Example of the Database info:
EDIT: I do have the Query but I don't know how to open the other connection during run time. the structure of their database is the same so there's no problem when executing the query. The problem is just how do I change the connection during runtime without pressing anything.
Based on #jmcilhinney answer in an external forum. The adapter is using one connection for the Select and another connection for the Insert. The trick is setting the .AcceptChangesDuringFill to False The default changes the Added to Unchanged.
Private Sub UpdateADifferentDatabase()
Using cnSource As New MySqlConnection("First connection string"), cnDestination As New MySqlConnection("Second connection string")
Using selectCommand As New MySqlCommand("Select statement here", cnSource), insertCommand As New MySqlCommand("Insert statement here ", cnDestination)
Using da As New MySqlDataAdapter()
da.SelectCommand = selectCommand
da.InsertCommand = insertCommand
'The following allows the .DataRowState to remain as Added (normally it is changed to Unchanged by the .Fill method)
da.AcceptChangesDuringFill = False
Dim dt As New DataTable
da.Fill(dt)
da.Update(dt)
End Using
End Using
End Using
End Sub
Firstly, as far as I know sharing the username/password should NOT be done as I'm attempting to make a user register/login program. My server is remote, so I connect to it with an IP address. I have been trying for a very long time but I always run into errors.
Even when providing the MySQL Username and password, it fails entirely.
Some stuff I've tried:
Dim connection As New MySqlConnection("datasource=(ip);port=22;username=root;password=(pwd);database=(db)")
Dim connection As New MySqlConnection("datasource=(ip);port=3306;username=root;password=(pwd);database=(db)")
Dim connection As New MySqlConnection("server=(ip);port=22;username=root;password=(pwd);database=(db)")
And then I realised another thing about my server, I require the SSH option when connecting so I tried this. However I still encounter the same errors on opening the MySQLConnection (portFwld.Start & Client.Connect is successful)
Dim ConnectionInfo = New PasswordConnectionInfo("(IP)", "root", "(PWD)")
Dim ConString = New MySqlConnectionStringBuilder
ConnectionInfo.Timeout = TimeSpan.FromSeconds(7)
Dim client = New SshClient(ConnectionInfo)
client.Connect()
Dim portFwld = New ForwardedPortLocal("127.0.0.1", Convert.ToUInt32("3306"), "(IP)", Convert.ToUInt32("22"))
client.AddForwardedPort(portFwld)
portFwld.Start()
ConString.Server = "(IP)"
ConString.Port = "22"
ConString.UserID = "root"
ConString.Password = "(PWD)"
ConString.Database = "(DB)"
Dim Connection = New MySqlConnection(ConString.ToString)
Connection.Open()
The errors I encountered were mostly these:
{"Reading from the stream has failed."}
and:
{"Unable to connect to any of the specified MySQL hosts."}
I literally have no idea why nothing is working whatsoever. I've searched and searched and I cannot find the solution. Also a reminder; I'm pretty sure I don't want the password revealed so blatantly in a string, let alone revealed at all.
Please be considerate as I'm completely new to this and just want to setup a community program just like any other program/site (like accounts here on Stackoverflow).
Thanks in advance.
The issue was that I needed to go to my server's cPanel and go to the allowed IPs area and add %, after that connection was all successful.
I am able to connect to a remote .accdb file to write content into a table as long as the target database is not currently open, but I am forced to write into that database even while it is open. Doing copy and paste manually from database1.tableA to database2.tableA while both are open works fine. But why does VBA not get the job done? Here is what I've tried so far:
Way 1:
...
Dim dbTarget As Database
Set dbTarget = DBEngine.Workspaces(0).OpenDatabase("X:\path\to\file.accdb")
...
Error (translated from German):
... failed with error (3029): No valid account name or no valid password.
Way 2:
...
Dim wrkDev As Workspace
Dim dbTarget As Database
Set wrkDev = CreateWorkspace("", "Admin", "", dbUseJet) ' adopted from microsoft docs
Set dbTarget = wrkDev.OpenDatabase("X:\path\to\file.accdb", True)
...
Error (translated from German):
... failed with error (3029): No valid account name or no valid password.
Why is that and how to fix?
There are several ways to open databases and database connections in Access. You've shared several, but can attempt others too. I'm assuming you're using DAO recordsets to do the inserting.
Open a database in a separate Access application, and create a reference to the database object
Dim A As New Access.Application
A.OpenCurrentDatabase "X:\path\to\file.accdb"
Dim db As Database
Set db = A.CurrentDb()
Open a recordset on a remote database using the SQL IN statement in the FROM clause
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset ("SELECT * FROM MyTable IN ""X:\path\to\file.accdb""")
I have been working on this for hours I am sure it is something simple I am missing. I have a remote server that gets updated form from another pc once the sever gets updated I would like my PC to get the changes from the server I cant seem to get it done the closest I have been is shown below any help would be greatly appreciated. Connection string no issue just trying to update the PC database to Match the server database
Sub updateRecord()
Try
Dim dbConnection As New MySqlConnection("server=" & My.Settings.ServerIPAddress & ";user id=root;password=T#ttleT#le102;persistsecurityinfo=True;database=pmdatabase")
dbConnection.Open()
Using Adapter As New MySqlDataAdapter("Select * From tblpmdata", dbConnection)
Using Table As New DataTable
Adapter.Fill(Table) 'test to see if table is correct
Adapter.Fill(Me.PmdatabaseDataSet.tblpmdata)
Me.PmdatabaseDataSet.AcceptChanges()
Me.TableAdapterManager.UpdateAll(Me.PmdatabaseDataSet)
MsgBox(Table.Rows.Count)only shows 5 records when the server has 8 records
End Using
End Using
dbConnection.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
I want a program that lets the user create his/her own query to retrieve data from the database. What is the best method to achieve such task? Any links or sample code would be much appreciated. Thank you.
I would go for :
Create a form, name it formSample
add a textbox, name it txbSample
add a datagridview, name it dgvSample
add a button, name it btnSample
txbSample will be used by the client to input queries
btnSample is where you would put the code.
dgvSample is the data, not really recommended but it's just used to show the data you have retrieved.
assuming you have connected to the database, the code on btnSample should look like
NOTE: not a complete code
Dim sql As String
sql = txbSample.text
Using cmd As New MySqlCommand
Try
'open the connection here
cmd.Connection = con
cmd.CommandText = sql
Dim dt As New DataTable
da = New MySqlDataAdapter(cmd)
da.Fill(dt)
'close the connection here
dgvSample.DataSource = dt
Catch ex As Exception
MessageBox.Show("Error while fetching data.")
'close the connection here
End Try
End Using
dt is a datatable where the data are stored. You should do something to be able to use it:)