how to search and update data into ms access - vb.net

my problem is how to search data and update it in ms access and vb.net
my code:
Imports System.Data.OleDb
Public Class Form5
Dim provider As String
Dim dataFile As String
Dim connString As String
Public myConnection As OleDbConnection = New OleDbConnection
Public dr As OleDbDataReader
Private Sub Form5_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "C:\Users\Admin\Desktop\project database\Database5.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
myConnection.Open()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
TextBox7.Clear()
TextBox8.Clear()
TextBox9.Clear()
TextBox10.Clear()
TextBox11.Clear()
Dim str As String
str = "SELECT * FROM registration WHERE (Name = '" & TextBox1.Text & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
While dr.Read()
TextBox2.Text = dr("No").ToString
TextBox3.Text = dr("Name").ToString
TextBox4.Text = dr("Age").ToString
TextBox5.Text = dr("Gender").ToString
TextBox6.Text = dr("Phone").ToString
TextBox7.Text = dr("Address").ToString
TextBox8.Text = dr("Desease").ToString
TextBox9.Text = dr("RoomNo").ToString
TextBox10.Text = dr("Building").ToString
TextBox11.Text = dr("RoomType").ToString
End While
myConnection.Close()
End Sub
End Class
the error I'm getting:
"No value given for one or more required parameters."}

Isn't the issue here that when you click button3 you clear the textbox1 where I assume you type the name you wish to search. So its throwing you an error that there is nothing in that field. And even if you type something after wards, and click the button again, it will clear it again before it starts searching. so basically, you're searching for nothing.

Related

Failed to convert parameter value from a String to a Int32 while trying to add a value to an SQL database

Here's the code:
Private m_cn As New SqlConnection
Private m_DA As SqlDataAdapter
Private m_CB As SqlCommandBuilder
Private m_DataTable As New DataTable
Private m_intRowPosition As Integer = 0
Private Sub InsertDatabaseItem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
m_cn.ConnectionString = "Data Source=TREVOR-PC\SQLSERVEREXPRESS;Initial Catalog=Milk Convience Products;Integrated Security=True"
m_cn.Open()
m_DA = New SqlDataAdapter("Select * From ProductIndex", m_cn)
m_CB = New SqlCommandBuilder(m_DA)
txtBarcode.Focus()
End Sub
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
Dim cmd As New SqlCommand(("INSERT INTO ProductIndex VALUES(" &
"#ID," &
"#Name," &
"#Price," &
"#Desc)" &
"#Barcode)"), m_cn)
cmd.Parameters.Add("#ID", SqlDbType.Int)
cmd.Parameters("#ID").Value = txtID.Text
cmd.Parameters.Add("#Name", SqlDbType.VarChar)
cmd.Parameters("#Name").Value = txtName.Text
cmd.Parameters.Add("#Price", SqlDbType.Money)
cmd.Parameters("#Price").Value = txtPrice.Text
cmd.Parameters.Add("#Desc", SqlDbType.VarChar)
cmd.Parameters("#Desc").Value = txtDesc.Text
cmd.Parameters.Add("#Barcode", SqlDbType.BigInt)
cmd.Parameters("#Barcode").Value = txtBarcode.Text
cmd.ExecuteNonQuery()
MsgBox("Success!", MsgBoxStyle.Information, "SUCCESS")
Me.Hide()
txtID.Clear()
txtName.Clear()
txtPrice.Clear()
txtDesc.Clear()
txtBarcode.Clear()
m_cn.Close()
m_cn.Dispose()
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.Hide()
End Sub
After I click the btnOK to enter a new database item, an exception occurs and it says : Failed to convert parameter value from a String to a Int32.
I did some debugging and found that the error occurs after the "cmd.Parameters("#Barcode").Value = txtBarcode.Text" line of code
cmd.Parameters.Add("#Barcode", SqlDbType.BigInt)
cmd.Parameters("#Barcode").Value = txtBarcode.Text
You are attempting to cast the txtBarcode.Text as a Big integer. This should be done by explicitly converting the value. It is likely that your test case is a empty string or some other string value that cannot be converted.
Note re-reading your code this is probably also an issue for Id, and Price parameters as well.

Error: "No value given for one or more required parameters"

Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim cnnOLEDB As New OleDbConnection
Dim cmdOLEDB As New OleDbCommand
Dim cmdInsert As New OleDbCommand
Dim cmdDelete As New OleDbCommand
Dim cmdUpdate As New OleDbCommand
Dim conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\Steven\Desktop\Database.accdb"
Private Sub searchbtn_Click(sender As Object, e As EventArgs) Handles searchbtn.Click
If ssearch.Text <> "" Then
cmdOLEDB.CommandText = "SELECT StudentID from Students Where TP = " & CInt(ssearch.Text)
cmdOLEDB.Connection = cnnOLEDB
Dim rd As OleDbDataReader = cmdOLEDB.ExecuteReader()
If rd.Read = True Then
Form2.Show()
Form2.TextBox1.Text = rd(0).ToString
Else
MessageBox.Show("Information not found in Database")
End If
rd.Close()
End If
Me.Hide()
Form2.Show()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cnnOLEDB.ConnectionString = conn
cnnOLEDB.Open()
End Sub
Private Sub ssearch_TextChanged(sender As Object, e As EventArgs) Handles ssearch.TextChanged
End Sub
Private Sub insertbtn_Click(sender As Object, e As EventArgs) Handles insertbtn.Click
If ssearch.Text <> "" Then
cmdInsert.CommandText = "INSERT INTO Students (StudentID) VALUES (" & ssearch.Text & ");"
cmdInsert.CommandType = CommandType.Text
cmdInsert.Connection = cnnOLEDB
cmdInsert.ExecuteNonQuery()
MsgBox(ssearch.Text & " " & "Record inserted.")
ssearch.Text = ""
Else
MsgBox("Please Enter Student ID")
End If
cmdInsert.Dispose()
End Sub
End Class
As you can see i can insert data into the database but I can't search it. When I run it it show errors on Dim rd As OleDbDataReader = cmdOLEDB.ExecuteReader()
saying:
No value given for one or more required parameters visual basic error
What was my mistake?

Syntax error in UPDATE statement vb.net in access database

i'm getting an error when i'm trying to update my database
Syntax error in UPDATE statement
item:In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.
error code: -2147217900
errors: {System.Data.OleDb.OleDbErrorCollection}
Imports System.Data.OleDb
Public Class Ubah_Password
Dim kns As New OleDbConnection("provider=microsoft.ace.oledb.15.0;data source =" & Application.StartupPath & "\database.accdb")
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Dim sql As String
Sub tampilkan() 'menampilkan semua user
ds.Clear()
sql = "SELECT * FROM USERLIST"
da.SelectCommand = New OleDbCommand(sql, kns)
da.Fill(ds, "USERLIST")
End Sub
Sub cariid() 'mencari id
ds.Clear()
sql = "select * from USERLIST where USERNAME = '" & LOGIN.USERNAME.Text & "'"
da.SelectCommand = New OleDbCommand(sql, kns)
da.Fill(ds, "USERLIST")
End Sub
Sub gantiid() 'mengganti password
sql = "update USERLIST set USERNAME = '" & LOGIN.USERNAME.Text & "', PASSWORD= '" & renew.Text & "' where USERNAME ='" & LOGIN.USERNAME.Text & "'"
kns.Open()
da.UpdateCommand = New OleDbCommand(sql, kns)
da.UpdateCommand.ExecuteNonQuery()
kns.Close()
End Sub
Private Sub Ubah_Password_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
MAIN_MENU.Panel1.Visible = True
End Sub
Private Sub Ubah_Password_Load(sender As Object, e As EventArgs) Handles MyBase.Load
tampilkan()
MAIN_MENU.Panel1.Visible = False
End Sub
Private Sub oldpass_TextChanged(sender As Object, e As EventArgs) Handles oldpass.TextChanged
cariid()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
cariid()
If ds.Tables("USERLIST").Rows.Count = 0 Then
MessageBox.Show("You Have Enter Wrong Password", "Warning!")
oldpass.Clear()
newpass.Clear()
renew.Clear()
oldpass.Focus()
ElseIf ds.Tables("USERLIST").Rows.Count = 1 And newpass.Text <> renew.Text Then
MessageBox.Show("New Password And Confirmation Must Be Same!", "Warning!")
newpass.Clear()
renew.Clear()
newpass.Focus()
ElseIf ds.Tables("USERLIST").Rows.Count = 1 And newpass.Text = renew.Text Then
gantiid()
MessageBox.Show("Password Change Is Successful!", "Notice")
Me.Close()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class

Vb.net 2010 connectivity with MS Access 2007

I am using a form having 10 text boxes,1 register button & 1 exit button. I have created the database in MS Access 2007 and saved the file in desktop. I have connected by "Add New Data Source". Provider & Path is:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\sipl\Desktop\Cust_Dtl.mdb. But when I click register the following error is showing:
con.Open() - Doesn't have a valid file name.
Here is my code:
Public Class Form2
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Users\sipl\Desktop\Cust_Dtl.mdb" & System.IO.Directory.GetCurrentDirectory & "Cust_Dtl.mdb"
Dim insertcmd As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand
Dim con As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection
con.ConnectionString = constring
insertcmd.CommandType = CommandType.Text
insertcmd.CommandText = String.Format("INSERT INTO {0} VALUES ('{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}')", "Table1", TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text, TextBox7.Text, TextBox8.Text, TextBox9.Text, TextBox10.Text)
insertcmd.Connection = con
con.Open()
Try
insertcmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
con.Close()
End Sub
End Class
Please help to fix this problem. Thanks.
You are calling the database string twice:
Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Users\sipl\Desktop\Cust_Dtl.mdb" & System.IO.Directory.GetCurrentDirectory & "Cust_Dtl.mdb"
This gives the string "C:\Users\sipl\Desktop\Cust_Dtl.mdbC:\Users\sipl\Desktop\Cust_Dtl.mdb"
Remove either C:\Users\sipl\Desktop\Cust_Dtl.mdb"
or & System.IO.Directory.GetCurrentDirectory & "Cust_Dtl.mdb

The ConnectionString property has not been initialized help databases

I have been trying to work this out for the last 4 weeks and cannot find the answer I've just been going around in circles. I have had multiple errors with con As New OleDb.OleDbConnection and connection string and ISAM's
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim da As OleDb.OleDbDataAdapter
Public sql As String
Dim ds As New DataSet
Dim dbPassword As String
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "C:\Users\Edward\Desktop\Edward\DataBase Login Window\Users.mdb;Jet OLEDB:Database"
con.ConnectionString = dbProvider & dbSource
con.Open()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sql = "SELECT * FROM users WHERE accUsername='" & TextBox1.Text & "'AND accPass='" & TextBox2.Text & "'"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "users")
If ds.Tables("users").Rows.Count = 1 Then
Dim isAdmin As Boolean = ds.Tables("users").Rows(0).Item(4)
If isAdmin = True Then
MsgBox("Logged in.")
Form4.Show()
Form4.Username = TextBox1.Text
Form4.Password = TextBox2.Text
ElseIf isAdmin = False Then
MsgBox("Logged in user.")
Form6.Show()
Form6.Username = TextBox1.Text
Form6.Password = TextBox2.Text
End If
Else
MsgBox("Incorrect Username or Password")
End If
Me.Hide()
ds.Clear()
Me.Refresh()
End Sub
Private Sub createanaccount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles createanaccount.Click
Form2.ShowDialog()
End Sub
End Class
in the button you should set the connectionString here is an example.
dim con as new SQLConnection/OLEDBConnection
con.ConnectionString="Provide your connection string"
con.open
I hope this will definitely solve your problem