Cannot write to the Access Database - vb.net-2010

When I press the button I want Sname and SNo to be written to the database. Here is my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\cerns1\Local Settings\Application Data\Temporary Projects\WindowsApplication1\testdb.accdb"
Dim cnn As New OleDbConnection(ConnString)
' Create a new row.
Dim newStudentRow As testdbDataSet.StudentRow
newStudentRow = Me.TestdbDataSet.Student.NewStudentRow()
' Save the new row to the database
Dim i As Integer = 2
Dim SNo As Integer = 1
Do While i > 1
cnn.Open()
'StudentTableAdapter.Insert("WOW", SNo)
Me.TestdbDataSet.Student.Rows.Add(newStudentRow)
Me.StudentTableAdapter.Update(Me.TestdbDataSet.Student)
SNo = SNo + 1
i = i + 1
cnn.Close()
Loop
End Sub
After executing, no information is added or changed in the Access database.

I found that if I look in the debug folder the changes are saved there. To change that I had to manually change the connection string to my WindowsApplication1 Folder.

Related

how to read a random entity in a database

I was just wondering how I would display a random entity in an access column.
Imports System.Data.OleDb
Public Class ReviseFlashcards
Dim connection As New OleDb.OleDbConnection(
"provider=microsoft.ACE.OLEDB.12.0;Data Source=flashcard login.accdb")
Dim dt As New DataTable
Dim dataadapter As OleDb.OleDbDataAdapter
'contains the current row number
Dim rownumber As Integer = 0
'Data table to contain all the records
Private Sub ReviseFlashcards_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
Dim sqlstring As String = "select * from flashcards"
connection.Open()
dataadapter = New OleDb.OleDbDataAdapter(sqlstring, connection)
dt.Clear()
dataadapter.Fill(dt)
txtFront.Text = dt.Rows(0)(2)
txtBack.Text = dt.Rows(0)(3)
connection.Close()
End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) _
Handles btnDisplay.Click
End Sub
End Class
The current code displays the first row in the database which is shown below. I was wondering if there was a way to display a random row by clicking a button with the front and back matching each other.
The access database
Add a random generator to the form. Make shure to declare it as Shared, so that this generator will be created only once. This ensures that it generates unique random number sequencess (see: Why isn't this Random number generation code working?).
Private Shared rand As New Random()
Then create a row index by respecting the actual row count:
Dim index = rand.Next(dt.Rows.Count) ' generates index in the range 0 .. Count - 1
txtFront.Text = dt.Rows(index)(2).ToString()
txtBack.Text = dt.Rows(index)(3).ToString()

Data type mismatch VB.net and Access Database

On my Delete button click event I am getting a Additional information: No value given for one or more required parameters. In my Access Database CustNo is a DataType of Short Text
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Dim custNumber As String
custNumber = txtCustNo.Text
Dim command As OleDbCommand
Dim deleteQuery As String = "DELETE FROM TblLabelCustom_copy WHERE [CustNo]= #custNo"
command = New OleDbCommand(deleteQuery)
command.Parameters.AddWithValue("#custNo", OleDbType.VarChar).Value = custNumber
ExecuteQuery(deleteQuery)
loadGrid()
End Sub

vb.net and crystal report responding so slow

Hi good day can anyone help me for my project i have created a vb.net application that produced a 473052 record in the report and i take 3-5 minutes before the report loaded here's my code:
---my function
Public Function staffbyyear(ByVal dates As String) As DataTable
Dim db = New database
Dim dt As New DataTable
With db
.sqlStr = "SELECT date, attendant, subtotal FROM transactions where year(date)= '" & dates & "' and finish = 1" 'query string
.sqlDa.SelectCommand = .sqlcm(.sqlStr, .connect) 'execute command
.sqlDa.Fill(.sqlDt) 'get results and store in sqldt
.close()
End With
dt = db.sqlDt
Return dt
End Function
--to fill my report
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Private rptstr as string
Private Sub fillreportstaffbyyear(ByVal d As String)
Dim cryRpt As New ReportDocument
Dim rptPath = Application.StartupPath.Remove(Application.StartupPath.IndexOf("bin"), Len(Application.StartupPath) - Application.StartupPath.IndexOf("bin"))
cryRpt.Load(rptPath & "reportstaffbyyear.rpt") '
cryRpt.SetDataSource(staffbyyear(cmbyear.Text))
'---modified here
crviewer.crv1.ReportSource = cryRpt
rptstr = vbNullString
End Sub
--to show the report
Private Sub ButtonX5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX5.Click
Dim strname As String = cmbyear.Text.Trim
fillreportstaffbyyear(strname)
crviewer.Show()
End sub

How to browse MDB file to DataGridView and TIF Images to ImageViewer

Public Class CARD_DECK_ENTRY
Public myConString As String
Dim con As New OleDb.OleDbConnection
Dim myStream As Stream = Nothing
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
End
Operator_ID.Close()
End Sub
Private Sub OpenFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFileToolStripMenuItem.Click
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "mdb files (*.mdb)|*.mdb|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
myConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & openFileDialog1.FileName
Dim x As String = openFileDialog1.FileName.Substring(0, openFileDialog1.FileName.Length - 20) & "Images"
con.ConnectionString = myConString
con.Open()
datagridshow()
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
End Sub
Private Sub CARD_DECK_ENTRY_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = myConString
con.Open()
datagridshow()
End Sub
Private Sub datagridshow()
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("Select * from Data001", con)
da.Fill(dt)
DataGridView1.DataSource = dt.DefaultView
con.Close()
End Sub
I get it now Tim, but now i'm about to load the images into a imageViewer with same time on browsing mdb. As you can see in the above codes, there was ("Select * from Data001", con) because it was a table i will be showing on the DataGridView. On the database there was a two table, those are "CardDeck" and "Data001." In CardDeck there was a "Image001" column. This column contains tif image names. On the folder where the database was found there was another folder on the same path(path of the database folder) named "Images". The column value of "Image001" will be found there. My point is how can I browse those "Image001" column value to a ImageViewer where those value are located in Folder "Images"? Please help. :'(
You want an integer division, VB.NET has it's own operator for this \:
Dim result As Int32 = 25 \ 9 + 1
So the common /-operator returns a double with the result of the division. In C# one of both need to be floating point types to avoid integer division(dropping the remainder).
Another approach is to use Math.Ceiling:
result = CInt(Math.Ceiling(25 / 9))
There's a difference in both approaches. The first will always add +1 to the result whereas the Math.Ceiling will return only the next integer if there is a remainder.
I guess you want get the round the output into the next Integer If yes then try this
Dim dbCount As Integer
Dim val, val1 As Integer
Dim intrslt As Integer
Dim dblrslt As Double
val = 20
val1 = 7
intrslt = (val / val1)' output : 3
dblrslt = (val / val1)' output : 2.85
or
use
Dim dbCount As Double = CInt(imgCount / batSz)
Dim rslt As Double
rslt = CInt((20 / 7)) ' output : 3
rslt = Convert.ToInt32(Int(20 / 7)) ' output :2
Demo

Updating records requires a valid InsertCommand when passed DataRow collection with new rows

I'm trying to add data from a form back into an Access table but I keep getting this error message:
Update requires a valid InsertCommand when passed DataRow collection
with new rows.
And for the life of me I can't work out what I need to do.
Here's the code for the button click that's supposed to update the records.
Public Class Orders
Dim ClientOrderConnection As New OleDb.OleDbConnection
Dim Provider As String
Dim dbSource As String
Dim sqlQuery As String
Dim dsClientOrder As New DataSet
Dim daClientOrder As New OleDb.OleDbDataAdapter
Dim dtOrders As New Data.DataTable
Dim Booking As New ArrayList
Dim RowNumber As Integer
Dim Counter As Integer = 0
Dim NumberOfRows As Integer
Private Sub Orders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Provider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
dbSource = "Data Source = A2ComputingDatabase.accdb"
ClientOrderConnection.ConnectionString = Provider & dbSource
ClientOrderConnection.Open()
sqlQuery = "SELECT * FROM TblClientOrder"
daClientOrder = New OleDb.OleDbDataAdapter(sqlQuery, ClientOrderConnection)
daClientOrder.Fill(dsClientOrder, "ClientOrder")
ClientOrderConnection.Close()
NumberOfRows = dsClientOrder.Tables("ClientOrder").Rows.Count
Private Sub btnSubmit_Click(sender As System.Object, e As System.EventArgs) Handles btnSubmit.Click
If RowNumber <> -1 Then
Dim cbClientOrder As New OleDb.OleDbCommandBuilder
Dim dsClientNewRow As DataRow
dsClientNewRow = dsClientOrder.Tables("ClientOrder").NewRow()
dsClientNewRow.Item("ClientOrderNumber") = txtOrderNo.Text
dsClientNewRow.Item("ClientTelNo") = txtClientTelNo.Text
dsClientOrder.Tables("ClientOrder").Rows.Add(dsClientNewRow)
daClientOrder.Update(dsClientOrder, "ClientOrder")
MsgBox("New Reocrd added to the Database")
End If
End Sub
Any help is much appreciated.
You actually have to instantiate the OledbCommandBuilder from your DataAdapter:
cbClientOrder = New OleDb.OleDbCommandBuilder(daClientOrder)
Dim cbClientOrder with your DataAdapter then put that somewhere after the daClientOrder.Fill() call and you should be good to go. And of course get rid of the other declaration of cbClientOrder in your btnSubmit_Click event.