Passing calculated textbox values to an access db - vb.net

I'm trying to send some textbox value to an access db using a query. 4 textboxes have calculated numeric values. While passing the value it shows the error Conversion from string to type double is not valid. Textbox 1,2,3,4 Have numeric values. This is my code:
Private Sub ok_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ok_button.Click
provider1 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
dataFile1 = "D:\electricity_billing.accdb"
connString1 = provider1 & dataFile1
myconnection1.ConnectionString = connString1
myconnection1.Open()
Dim str1 As String
str1 = "Insert into 345([ID],[cust_name],[cust_add],[t_p_no],[nic],[email],[total_units],[amount],[adjustments],[due]) values (?,?,?,?,?,?,?,?,?,?)"
Dim cmd1 As OleDbCommand = New OleDbCommand(str1, myconnection1)
cmd1.Parameters.Add(New OleDbParameter("ID", CType(TextBox5.Text, String)))
cmd1.Parameters.Add(New OleDbParameter("cust_name", CType(name_textbox.Text, String)))
cmd1.Parameters.Add(New OleDbParameter("cust_add", CType(add_textbox.Text, String)))
cmd1.Parameters.Add(New OleDbParameter("t_p_no", CType(tp_textbox.Text, String)))
cmd1.Parameters.Add(New OleDbParameter("nic", CType(nic.Text, String)))
cmd1.Parameters.Add(New OleDbParameter("email", CType(mail_textbox.Text, String)))
cmd1.Parameters.Add(New OleDbParameter("total_units", CDbl(TextBox1.Text)))
cmd1.Parameters.Add(New OleDbParameter("amount", CDbl(TextBox2.Text)))
cmd1.Parameters.Add(New OleDbParameter("adjustments", CDbl(TextBox4.Text)))
cmd1.Parameters.Add(New OleDbParameter("due", CDbl(TextBox3.Text)))
Try
cmd1.ExecuteNonQuery()
cmd1.Dispose()
myconnection1.Close()
no_textbox.Clear()
name_textbox.Clear()
add_textbox.Clear()
tp_textbox.Clear()
nic.Clear()
mail_textbox.Clear()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Related

"not allowed to change the 'connectionstring' property. the connection's current state is open"

so im working on a PDS system for my school act and i came in to this error that i didn't know that much i dont even know what kind of error is it
this is the main error of the code :
"
myConnection.ConnectionString = connString
myConnection.Open()
"
and this is the code:
Imports System.Data.SqlClient
Imports System.Data.OleDb
Public Class personalinfo
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection
Private Sub Personal_infoBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles Personal_infoBindingNavigatorSaveItem.Click
Me.Validate()
Me.Personal_infoBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.PDS_GabuteroKenuardPercivalDataSet)
End Sub
Private Sub personalinfo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'PDS_GabuteroKenuardPercivalDataSet.personal_info' table. You can move, or remove it, as needed.
Me.Personal_infoTableAdapter.Fill(Me.PDS_GabuteroKenuardPercivalDataSet.personal_info)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Personal_infoBindingSource.AddNew()
End Sub
Private Sub Personal_infoBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles Personal_infoBindingSource.CurrentChanged
End Sub
Private Sub CS_IDTextBox_TextChanged(sender As Object, e As EventArgs)
End Sub
Private Sub TIN_noTextBox_TextChanged(sender As Object, e As EventArgs) Handles TIN_noTextBox.TextChanged
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
provider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
dataFile = "F:\PDS_GabuteroKenuardPercival.mdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
myConnection.Open()
Dim str As String
str = "Insert in to personal_info([CS_ID]),[Emp_Ln],[Emp_Fn],[Emp_Mi],[Date_Birth],[Place_Birth],[Gender],[Civilstatus],[Height],[Weight],
[Blood_typ],[Gsis_ID],[Pagibig_ID],[Phil_health_no],[SSS_no],[TIN_no],[Agency_emp_no],[Citizenship],[Indicate_country],[Residential_add],
[ZIP_code],[Permanent_add],[ZIP_code_perma_add],[Tell_no],[Mobile_no],[Email_add])"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
cmd.Parameters.Add(New OleDbParameter("CS_ID", CType(CS_IDTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Emp_Ln", CType(Emp_LnTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Emp_Fn", CType(Emp_FnTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Email_add", CType(Emp_MiTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Emp_Mi", CType(Date_BirthDateTimePicker.Value, String)))
cmd.Parameters.Add(New OleDbParameter("Date_Birth", CType(Place_BirthTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Place_Birth", CType(GenderTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Gender", CType(CivilstatusTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Civilstatus", CType(Tell_noTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Height", CType(HeightTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Weight", CType(WeightTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Blood_typ", CType(Blood_typTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Gsis_ID", CType(Gsis_IDTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Pagibig_ID", CType(Pagibig_IDTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Phil_health_no", CType(Phil_health_noTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("SSS_no", CType(SSS_noTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("TIN_no", CType(TIN_noTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Agency_emp_no", CType(Mobile_noTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Citizenship", CType(Agency_emp_noTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Indicate_country", CType(CitizenshipTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Residential_add", CType(Indicate_countryTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("ZIP_code", CType(Residential_addTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Permanent_add", CType(ZIP_codeTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("ZIP_code_perma_add", CType(Permanent_addTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Tell_no", CType(ZIP_code_perma_addTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Mobile_no", CType(Email_addTextBox.Text, String)))
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
myConnection.Close()
CS_IDTextBox.Clear()
Emp_LnTextBox.Clear()
Emp_FnTextBox.Clear()
Emp_MiTextBox.Clear()
Place_BirthTextBox.Clear()
GenderTextBox.Clear()
CivilstatusTextBox.Clear()
Tell_noTextBox.Clear()
HeightTextBox.Clear()
WeightTextBox.Clear()
Blood_typTextBox.Clear()
Gsis_IDTextBox.Clear()
Pagibig_IDTextBox.Clear()
Phil_health_noTextBox.Clear()
SSS_noTextBox.Clear()
TIN_noTextBox.Clear()
Mobile_noTextBox.Clear()
Agency_emp_noTextBox.Clear()
CitizenshipTextBox.Clear()
Indicate_countryTextBox.Clear()
Residential_addTextBox.Clear()
ZIP_codeTextBox.Clear()
Permanent_addTextBox.Clear()
ZIP_code_perma_addTextBox.Clear()
Email_addTextBox.Clear()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
i already try to change and fix the close connection and fixing the "provider" and "datafile" still i don't know what's wrong with it
Example:
Private connectionString As String = ConfigurationManager.ConnectionStrings("AccessConnectionString").ConnectionString
Private using(Optional ByVal cnn As New OleDbConnection(connectionString))
cnn.Open()
Dim sql As String = "select * from Billing where FormNo=" & txtformno.Text & ""
using(OleDbCommand cmd = New OleDbCommand(sql, cnn))
If True Then
using(OleDbDataReader dr = cmd.ExecuteReader())
If True Then
'....
End If
End If
End Sub
Hope this link can be useful to you.
https://www.codeproject.com/Questions/1088503/Not-allowed-to-change-the-connectionstring-propert

Syntax error in INSERT INTO Statement in vb.net using Access database "how can i fix it ?"

Private Sub Opretprofil_Click(sender As Object, e As EventArgs) Handles Opretprofil.Click
Dim connection As New OleDbConnection(My.Settings.PakkeAppDatabaseConnectionString)
If Email.Text = Nothing Or Passwordtillogin.Text = Nothing Or Gentagpassword.Text = Nothing Then
MsgBox("Indtast din Email og dit Password", MsgBoxStyle.Critical)
ElseIf Passwordtillogin.Text <> Gentagpassword.Text Then
MsgBox("Udfyld venligt alle felter korrekt", MsgBoxStyle.Critical)
Else
Try
If connection.State = ConnectionState.Closed Then
connection.Open()
End If
Dim cmd As New OleDbCommand("insert into Table1(Email,Password) Value(?,?)", connection)
cmd.Parameters.AddWithValue("#1", OleDbType.VarChar).Value = Email.Text
cmd.Parameters.AddWithValue("#1", OleDbType.VarChar).Value = Passwordtillogin.Text
cmd.ExecuteNonQuery()
connection.Close()
MsgBox("Profil er nu oprettet", MsgBoxStyle.Information)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
the problem i think is in this part:
Dim cmd As New OleDbCommand("insert into Table1(Email,Password) Value(?,?)", connection)
Is better to identify the parameters and if you want to use AddWithValue you have to give the value :
Dim cmd As New OleDbCommand("insert into Table1(Email,Password) Value(#Email,#Password)", connection)
cmd.Parameters.AddWithValue("#Email", Email.Text)
cmd.Parameters.AddWithValue("#Password", Passwordtillogin.Text)
Hope this help

Updating Access Database with VB 2010

This code does not throw an error but does not work.
Any help would be appreciated!
I am a VB6 programmer migrating to .Net
Other code I have is working to add and delete records, not update them...
Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim cmdUpdate As New OleDbCommand
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
myConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Chr(34) & sAppData & "\Data.mdb" & Chr(34) & _
";Persist Security Info=True;Jet OLEDB:Database Password=1"
myConnection.Open()
Dim str As String
str = "update Data set ([1], [2], [3], [4]) values (?, ?, ?,?)"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
cmd.Parameters.Add(New OleDbParameter("1", CType(TextBox2.Text, String)))
cmd.Parameters.Add(New OleDbParameter("2", CType(txtDesc.Text, String)))
cmd.Parameters.Add(New OleDbParameter("3", CType(TextBox3.Text, String)))
Dim cbState As String = ""
If CheckBox1.Checked = True Then
cbState = "1"
Else
cbState = "0"
End If
cmd.Parameters.Add(New OleDbParameter("4", CType(cbState, String)))
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
myConnection.Close()
Me.Close()
Exit Sub
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
It's possible that through your Me.Close() in the try-catch block, the form is closing before all resources have been cleanly released.
I would do the following:
cmd.ExecuteNonQuery() returns the number of rows updated. Check that this is greater than 0.
If the number of rows updated is > 0, then only then call Me.Close() -- outside of the try/catch block
Finally, I would replace lines such as this:
cmd.Parameters.Add(New OleDbParameter("1", CType(TextBox2.Text, String)))
with the more convenient
cmd.Parameters.AddWithValue("1", TextBox2.Text)
The AddWithValue() function is especially useful when you're passing localizable things like dates and times.

I need help connecting to access using vb.net

My program doesn't use the connection string that i want it to. Instead it finds the access file in this folder and displays the error:
Could not find file 'C:\Users\user\Documents\Visual Studio
2008\Projects\Patientt\Patientt\bin\Debug\db_hospital.accdb'.
here is my code
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Provider = "Provider=Microsoft.ACE.oleDB.12.0;data source=" '& System.Environment.CurrentDirectory.ToString() & "\namu.mdb"
datafile = "\DemsV.accdb"
connString = Provider & datafile
Try
myConnection.ConnectionString = connString
myConnection.Open()
Dim str As String
str = "insert into Member([MemberID],[Name],[Surname],[Date of Birth],[Contacts],[Gender],) Values (?,?,?,?,?,?)"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
cmd.Parameters.Add(New OleDbParameter("MemberID", CType(txtmemberid.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Name", CType(txtname.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Surname", CType(txtsurname.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Date of Birth ", CType(DateTimePicker1.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Contacts", CType(txtcontacts.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Gender", CType(comGender.Text, String)))
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Have you tried something along the lines of this?
Try
Dim dbProvider As String
Dim dbSource As String
mainDBconnection = New OleDb.OleDbConnection
dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
dbSource = ' Path to your database
mainDBconnection.ConnectionString = dbProvider & dbSource
mainDBconnection.Open()
Catch ex As Exception
End Try
It's basically done like this.
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' Requires: Imports System.Data.OleDb
' ensures the connection is closed and disposed
Using connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=""C:\your_path_here\InsertInto.mdb"";" & _
"Persist Security Info=False")
' open connection
connection.Open()
' Create command
Dim insertCommand As New OleDbCommand( _
"INSERT INTO Table1([inputOne] , [inputTwo] , [inputThree]) " & _
"VALUES (#inputOne, #inputTwo, #inputThree);", _
connection)
' Add the parameters with value
insertCommand.Parameters.AddWithValue("#inputOne", TextBox1.Text)
insertCommand.Parameters.AddWithValue("#inputTwo", TextBox2.Text)
insertCommand.Parameters.AddWithValue("#inputThree", TextBox3.Text)
' you should always use parameterized queries to avoid SQL Injection
' execute the command
insertCommand.ExecuteNonQuery()
MessageBox.Show("Insert is done!!")
End Using
End Sub
End Class
Can you adapt that to your specific needs?

how to add datagridview records - closed

when i try to save a record, it wont show in the datagridview until i close the form and run it again
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
'Change the following to your access database location
dataFile = "C:\Users\DELL\Downloads\WindowsApplication1\WindowsApplication1\Database1.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
myConnection.Open()
Dim str As String
str = "insert into tblODR ([ID], [Office], [Elements], [Objectives], [Initiatves]) values (?, ?, ?, ?, ?)"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
cmd.Parameters.Add(New OleDbParameter("UPC", CType(IDTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Item", CType(OfficeTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Cost", CType(ElementsTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Price", CType(ObjectivesTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Quantity", CType(InitiatvesTextBox.Text, String)))
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
myConnection.Close()
IDTextBox.Clear()
OfficeTextBox.Clear()
ElementsTextBox.Clear()
ObjectivesTextBox.Clear()
InitiatvesTextBox.Clear()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
the code below is for deleting purposes, i need a code like this BUT for adding/saving of records.
Try
Dim int As Integer = DataGridView1.SelectedRows(0).Index
DataGridView1.Rows.Remove(DataGridView1.SelectedRows(0))
ds.Tables(0).Rows(int).Delete()
da.Update(ds, "datainfo")
MessageBox.Show("Success!")
Catch ex As Exception
End Try
i already solved it, i've added some codes.. and in the bindingNavigator,, i have to click the plus (+) sign every time I add a record..woo :3 .. sorry for being a newbie
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
'Change the following to your access database location
dataFile = "C:\Users\DELL\Downloads\WindowsApplication1\WindowsApplication1\Database1.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
myConnection.Open()
Dim str As String
str = "insert into tblODR ([ID], [Office], [Elements], [Objectives], [Initiatves]) values (?, ?, ?, ?, ?)"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
cmd.Parameters.Add(New OleDbParameter("ID", CType(IDTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Office", CType(OfficeTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Elements", CType(ElementsTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Objectives", CType(ObjectivesTextBox.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Initiatves", CType(InitiatvesTextBox.Text, String)))
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
myConnection.Close()
Me.Validate()
Me.TblODRBindingSource.EndEdit()
Me.TblODRTableAdapter.Update(Me.Database1DataSet.tblODR)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub