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.
Related
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)
I'm currently trying to write code for a form that has text boxes for a user to input the required data into which then with the use of button the data in the text boxes will be sent to an access table.
If you need any more information to help solve the problem I'm willing to provide it if you ask (I would upload pictures/screenshots but I need "10 reputation" apparently.
You can do this
Imports System.Data.OleDb
Public Class Form1
Dim AccessConection As OleDbConnection
Private Sub btSave_Click(sender As Object, e As EventArgs) Handles btSave.Click
Dim cmd As New OleDbCommand
Dim mySql As String
mySql = "INSERT INTO Customs (CustomName,Address) VALUES(#Name,#Address)"
Try
cmd.Parameters.AddWithValue("#Name", txName.Text)
cmd.Parameters.AddWithValue("#Address", txAddress.Text)
cmd.Connection = AccessConection
cmd.CommandType = CommandType.Text
cmd.CommandText = mySql
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Whatever you want to say..." & vbCrLf & ex.Message)
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim myDataBasePath As String = "C:\Users\user\Source\Workspaces\......\SOF003\Data.accdb" 'Here you put the full name of the database file (including path)
'The next line is for Access 2003 .mdb files
'Dim CadenaConection As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", myDataBasePath)
Dim CadenaConection As String = String.Format("Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}", myDataBasePath)
AccessConection = New OleDbConnection(CadenaConection)
AccessConection.open()
End Sub
End Class
btSave is the command button.
Customs is the table's name.
CustomName and Address are two fields.
txName and txAddress are two TextBox Control.
Obviously you should be careful with the data types (here I use only strings), validation, etc, etc... But, this is a starting point. If you search, you'll find another ways, more elaborated.
I need your help.
I'm writing code for a shop program and I am using vb.net 2008 with Crystal Report version 10.5.37xxxx
The problem is when I'm trying to install my program on the client computer, everything works but not in my Crystal Report. It always asks for the database login and I did not code my program to ask for the database login.
I just wrote it in simple code like this:
Public Class Form16
Private Sub Form16_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim view As New CrystalReport4
view.SetParameterValue("p_1", Form5.no_faktur_tb_immanuel)
CrystalReportViewer1.ReportSource = view
End Sub
End Class
Can anyone help me with this?
You should be able to manually code the login credentials.
Public Class Form16
Private Sub Form16_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim view As New CrystalReport4
Dim user as string = "Username"
Dim pwd as string = "Password"
view.SetDatabaseLogon(user, pwd)
view.SetParameterValue("p_1", Form5.no_faktur_tb_immanuel)
CrystalReportViewer1.ReportSource = view
End Sub
End Class
Make Sure your Report DataSource Provider is set to Microsoft OLE DB provider for SQL Server and not SQL Server Native Client 10.0
try that open field explorer---> database field --->Right Click -->current Data source --->reports connection----->report ----->property ---->
set Property as---
Data Source: .\Databasename.accdb
and code on viewer form load as
Dim cryRpt As New ReportDocument
Dim Report1 As New rptItemWise
Dim strServerName As String
strServerName = Application.StartupPath
rptItemWise.SetDatabaseLogon("admin", "", strServerName, "dastabasename.accdb", True)
cryRpt.Load(Application.StartupPath + "\rptItemWise.rpt")
also change the report connection same as data source
i think that code work for you ....
This will work for you!
C# Code :
ConnectionInfo boconnectioninfo = new ConnectionInfo ();
boconnectioninfo.ServerName= "D-2818-w2k";
boconnectioninfo.DatabaseName ="NW";
boconnectioninfo.UserID ="sa";
boconnectioninfo.Password ="sa";
CrystalReportViewer1 .ReportSource = Server.MapPath("CrystalReport1.rpt");
TableLogOnInfos botableInfo= CrystalReportViewer1 .LogOnInfo;
foreach (TableLogOnInfo obj2 in botableInfo)
{
obj2.ConnectionInfo =boconnectioninfo;
}
Vb.net Code :
Dim boconnectioninfo As New ConnectionInfo
boconnectioninfo.ServerName = "sa"
boconnectioninfo.DatabaseName = "sa"
boconnectioninfo.UserID = "sa"
boconnectioninfo.Password = "sa"
Me.rptViewer.ReportSource = _ReportPath
Me.rptViewer.SelectionFormula = _SelectionFormula
If Not _ParameterFields Is Nothing Then
Me.rptViewer.ParameterFieldInfo = _ParameterFields
End If
Dim a As TableLogOnInfos = Me.rptViewer.LogOnInfo
' Iterate through the list.
For Each aa As TableLogOnInfo In a
aa.ConnectionInfo = boconnectioninfo
Next
Me.Cursor = Cursors.Default
rptViewer.Refresh()
I need to control a database from grid view.
In another article I have to see how to manipulate a database, but there still used textbox and button for next process. In my case I need to control the data from grid view, so tell me please if you have solution for this problem.
Specifications:
application form
visual basic .NET 2010
SQL Server database
A quick search online found this: link. Below is the example code from that link showing how to display data.
If you want to do CRUD, please take a look at the Microsoft example here.
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="Your .mdb path";"
Dim sql As String = "SELECT * FROM Authors"
Dim connection As New OleDbConnection(connectionString)
Dim dataadapter As New OleDbDataAdapter(sql, connection)
Dim ds As New DataSet()
connection.Open()
dataadapter.Fill(ds, "Authors_table")
connection.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Authors_table"
End Sub
End Class
I have a form set up where I want to run the function PopulateGrid on it's Form_Load event. I have initialized the DB connection as follows:
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadConfigFile()
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & config("DatabasePath") & ";")
cn.Open()
End Sub
Now, I want to run the PopulateGrid function directly after I connect to the database as shown above. I'm confused as to how I'd use a database query in a module when the database is initialized in my main form. Would simple setting the variable cn to public work? Or do I have to do something more complex?
Thanks.
You should modify your module to accept the OleDbConnection as a parameter and then just pass the cn variable to the query method. Setting cn to public would not be a good design because it would be introducing a dependency on the form/ui in the module that is unnecessary.