How can I clear previous values of the combobox - vb.net

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()

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

Search and save data to database

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()

.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()

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