Search and save data to database - vb.net

I am using vb.net with OleDb database.
Following is my code, although i am to search database but not able to save after making changes.
It displays following error at Command.ExecuteNonQuery level :
"OleDbException was unhandled, Data type mismatch in criteria expression."
Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Src_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Src.Click
Dim Connection1 As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Satyam\Documents\Database2.accdb;" & "Persist Security Info=False;" & "Jet OLEDB:Database Password=" & "your pass" & ";")
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Try
Connection1.Open()
cmd = New OleDbCommand("SELECT * from Table1 WHERE ID=" & IDtxt.Text & "", Connection1)
dr = cmd.ExecuteReader
If dr.Read Then
Me.IDtxt.Text = dr("ID")
Me.Hbtxt.Text = dr("Hb")
Me.TCtxt.Text = dr("TC")
Me.DCtxt.Text = dr("DC")
dr.Close()
Else
MsgBox("No Record")
End If
Catch
End Try
Connection1.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Connection1 As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Satyam\Documents\Database2.accdb;")
Connection1.Open()
Dim ID : ID = Me.IDtxt.Text
Dim Hb : Hb = Me.Hbtxt.Text
Dim TC : TC = Me.TCtxt.Text
Dim DC : DC = Me.DCtxt.Text
Dim command As New OleDbCommand("UPDATE Table1 SET Hb = '" & Hb & "',TC = '" & TC & "',DC = '" & DC & "' WHERE ID = '" & ID & "'", Connection1)
Command.ExecuteNonQuery()
Connection1.Close()
End Sub

use parameter in your code..like this..
Dim command As New OleDbCommand("UPDATE Table1 SET Hb = #hb,TC = #tc,DC = #DC WHERE ID = #id", Connection1)
Command.parameters.add(new sqlparameter("#hb",Hb))
Command.parameters.add(new sqlparameter("#tc",tc))
Command.parameters.add(new sqlparameter("#dc",dc))
Command.parameters.add(new sqlparameter("#id",id))
Command.ExecuteNonQuery()

Related

Disable delete when you got only one 1 existing file with specific name

How i can hide delete.button and change it with another button with warning that you only have 1 operation manager you cannot delete again. when i have only one (Position= Operation manager) in my datagridview
Imports System.Data.OleDb
Public Class Form5
Dim conn As New OleDbConnection
Dim comm As New OleDbCommand
Dim dr As OleDbDataReader
Dim query As String
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.LogInTableAdapter.Fill(Me.LogInDataSet.LogIn)
If conn.State = ConnectionState.Open Then conn.Close()
conn.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0; " & _
"Data Source = C:\Users\69neers\Desktop\Database\Program\Program\bin\Debug\LogIn.accdb; " & _
"Persist Security Info=false"
conn.Open()
query = "SELECT * FROM LogIn WHERE Uname = '" & UnameTextBox.Text & "'"
comm.CommandText = query
comm.Connection = conn
dr = comm.ExecuteReader
dr.Read()
If LogInBindingSource.Count = 1 And dr("Pos") = PosComboBox.Text Then
btndelete.Enabled = False
btndelete.Hide()
btndelalter.Show()
btndelete.Enabled = False
Else
btndelete.Enabled = True
btndelalter.Hide()
btndelete.Show()
End If
dr.Close()
End Sub

How can I clear previous values of the combobox

Private Sub ComboBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
lblbrand.Text = ComboBox1.SelectedValue.ToString()
strb = lblbrand.Text
connetionString = "Data Source=HRS\SQLEXPRESS;AttachDbFilename='E:\My project\forms\1\1\mobile shop management.mdf';Integrated Security=True"
con = New SqlConnection(connetionString)
con.Open()
command = "select Model_id from mob where Brand ='" & lblbrand.Text & "'"
cmd = New SqlCommand(command, con)
Dim rd As SqlDataReader = cmd.ExecuteReader()
If rd.HasRows = True Then
While rd.Read()
ComboBox2.Items.Add(rd("Model_id"))
End While
End If
End Sub
Simple:
ComboBox2.Items.Clear()

Select and update data from database

I have ID, FullName , Age, Sex as textbox and similar column in database.
Below is my code for search from database by ID.
But i don;t know how to update after search these data.
Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Src_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Src.Click
Form2.Show()
Dim Connection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Satyam\Documents\Satyam.accdb;" & "Persist Security Info=False;" & "Jet OLEDB:Database Password=" & "your pass" & ";")
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Try
Connection.Open()
cmd = New OleDbCommand("SELECT * from DailyWorkLoadRegister WHERE ID=" & txtID.Text & "", Connection)
dr = cmd.ExecuteReader
If dr.Read Then
Form2.txtID.Text = dr("ID")
Form2.txtAge.Text = dr("Age")
Form2.txtSex.Text = dr("Sex")
Form2.txtFullName.Text = dr("FullName")
dr.Close()
Else
MsgBox("No Record")
End If
Catch
End Try
Connection.Close()
End Sub
End Class
You would need to execute a non-query method which returns:
For UPDATE, INSERT, and DELETE statements, the return value is the
number of rows affected by the command. For all other types of
statements, the return value is -1. If a rollback occurs, the return
value is also -1.
Microsoft documentation -> Here
Which is as simple as:
Dim ID : ID = Form2.txtID.Text
Dim Age : Age = Form2.txtAge.Text
Dim Sex : Sex = Form2.txtSex.Text
Dim FN : FN = Form2.txtFullName.Text
Dim command As New OleDbCommand("UPDATE YOUR_TABLE_NAME SET _
FullName = '" & FN & "',_
Age = '" & Age & "',_
Sex = '" & Sex & "'_
WHERE ID = '" & ID & "'", Connection)
command.ExecuteNonQuery()

.net: how to evaluate this error

I am new to vb.net and I am trying to create a login form but on running this code i get error msg("ExecuteReader: Connection property has not been initialized.")
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim con As New System.Data.OleDb.OleDbConnection
Dim cmd As System.Data.OleDb.OleDbCommand
Dim da As System.Data.OleDb.OleDbDataAdapter
Dim sdr As System.Data.OleDb.OleDbDataReader
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dbprovider As String = "Provider=Microsoft.jet.OleDb.4.0;"
Dim dbsource As String = "Data Source=C:\Users\RAJKRI\Documents\Visual Studio 2008\Projects\Banking System.mdb"
con.ConnectionString = dbprovider & dbsource
con.Open()
cmd.Connection = con
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\RAJKRI\Documents\Visual Studio 2008\Projects\Banking System.accdb")
cmd = New OleDb.OleDbCommand("SELECT * FROM Login WHERE Empid = '" & TextBox1.Text & "' AND password = '" & TextBox2.Text & "' , con")
sdr = cmd.ExecuteReader()
If (sdr.Read() = True) Then
Form2.Show()
TextBox1.Text = ""
TextBox2.Text = ""
Else
MessageBox.Show("Invalid Employee Id/Password")
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
End Class
You are appending your connection object to your query as text. Change
cmd = New OleDb.OleDbCommand("SELECT * FROM Login WHERE Empid = '" & TextBox1.Text & "' AND password = '" & TextBox2.Text & "' , con")
to
cmd = New OleDb.OleDbCommand("SELECT * FROM Login WHERE Empid = '" & TextBox1.Text & "' AND password = '" & TextBox2.Text & "'" , con)
(the difference is at the end)

declare "data source" in a connection string as 'variable' in VB.NET

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim bs As New BindingSource
m_DataAdapter = New OleDbDataAdapter("SELECT * FROM Table1 ORDER BY ID", "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Users\rebel23\Desktop\sampledata.mdb")
m_DataAdapter.Fill(m_DataSet)
bs.DataSource = m_DataSet.Tables(0)
BindingNavigator1.BindingSource = bs
txtAnimal.DataBindings.Add("Text", bs, "TextBox6.text")
txtSpecies.DataBindings.Add("Text", bs, "TextBox7.text")
In the above code i want to change the "data source" and declare it as TextBox1.text
So that the user will decide the data source at run time...
Also i want the same for 'Table1' and 'ID'
but how to do that??
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim bs As New BindingSource
Dim myTableName As String
Dim myOrderingColumn As String
Dim mybind1 As String
Dim mybind2 As String
myTableName = TextBox3.Text
myOrderingColumn = TextBox4.Text
mybind1 = TextBox6.Text
mybind2 = TextBox7.Text
m_DataAdapter = New OleDbDataAdapter("SELECT * FROM " & myTableName & " ORDER BY " & myOrderingColumn, "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & TextBox5.Text)
m_DataAdapter.Fill(m_DataSet)
bs.DataSource = m_DataSet.Tables(0)
BindingNavigator1.BindingSource = bs
txtAnimal.DataBindings.Add("Text", bs, "" & TextBox6.Text & "")
txtSpecies.DataBindings.Add("Text", bs, "" & TextBox7.Text & "")
End Sub
hey i got the code man !!!! its working .... thanks a lot for your support and suggestions #matzone
I did it in different way ..
Dim myConn = New OleDb.OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\rebel23\Desktop\sampledata.mdb;Jet OLEDB:Database Password=mypass")
Dim cmd As OleDbCommand
Dim ds as DataSet = New DataSet
myConn.Open() '--> open conn
cmd = New OleDbCommand("SELECT * FROM Table1 ORDER BY ID", myConn)
cmd.Fill(ds, "predator") ' ---> named dataset as Predator
txtAnimal.DataBindings.Add("Text", ds.Tables("predator"), "'" & TextBox6.text & "'")
txtSpecies.DataBindings.Add("Text",ds.Tables("predator"), "'" & TextBox7.text & "'")
' ....
' .....
cmd.Dispose()
ds.Dispose()
cnn.Close()