OleDb error: "ExecuteNonQuery() requires an open and available connection" [closed] - vb.net

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've been trying to connect Access and VB 2013, but it keeps giving me this error
ExecuteNonQuery() requires an open and Avaliable connection
Here is my code:
Imports System.Data.OleDb
Imports System.IO
Public Class Form3
Public sEditType As String = String.Empty
Dim cn As New OleDbConnection
Dim constring As String = "Provider=Microsoft.Jet.Oledb.12.0; Data Source=C:\Users\MarkTT\Documents\major.mdb; persist security info = false"
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.KeyPress
End Sub
Private Sub Label4_Click(sender As Object, e As EventArgs)
End Sub
Private Sub TextBox2_TextChanged_1(sender As Object, e As EventArgs)
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
End Sub
Private Sub Label5_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Label6_Click(sender As Object, e As EventArgs) Handles Label6.Click
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cm As New OleDbCommand
Try
Dim sqlquery As String = "INSERT INTO tablea (Name,Cost,Supplier,Quantity) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')"
With cm
.CommandText = sqlquery
.Connection = cn
.ExecuteNonQuery()
End With
MsgBox("Added")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Enabled = True
TextBox2.Enabled = True
TextBox3.Enabled = True
TextBox4.Enabled = True
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
TextBox1.Enabled = False
TextBox2.Enabled = False
TextBox3.Enabled = False
TextBox4.Enabled = False
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub TextBox2_TextChanged_2(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
End Sub
Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
cn.ConnectionString = constring
cn.Open()
End Sub
End Class

Provider=Microsoft.Jet.Oledb.12.0
is not a valid OLEDB provider name. You either need to use
Provider=Microsoft.Jet.OLEDB.4.0
or
Provider=Microsoft.ACE.OLEDB.12.0

Related

Line Suppression State Error BC30451 'dbo' is not declared

I am trying to create a new and delete button. The new button works but I cant get the delete button to work.
Code:
Imports System.Data.SqlClient
Public Class AssetCategory
Dim cn As New SqlConnection("Data Source=DESKTOP-PHILIP\SQLEXPRESS;Initial Catalog=PECS_Asset;Integrated Security=True")
Dim cmd As New SqlCommand
Dim dir As SqlDataReader
Private txtupdatename As Object
Private Sub AssetCategoryBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.TableAdapterManager.UpdateAll(Me.PECS_AssetDataSet)
cmd.Connection = cn
End Sub
Private Sub AssetCategory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'PECS_AssetDataSet.AssetCategory' table. You can move, or remove it, as needed.
Me.AssetCategoryTableAdapter.Fill(Me.PECS_AssetDataSet.AssetCategory)
End Sub
Private Sub ReturnToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ReturnToolStripMenuItem.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub MenuStrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
End Sub
Private Sub AssetsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AssetsToolStripMenuItem.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
Me.Validate()
Me.AssetCategoryBindingSource.AddNew()
Me.TableAdapterManager.UpdateAll(Me.PECS_AssetDataSet)
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
Me.AssetCategoryBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.PECS_AssetDataSet)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs)
If MsgBox("Are you sure you want to delete this record?", MsgBoxStyle.YesNo, Title:="CONFIRM") - vbYes Then
Else
TableAdapterManager.UpdateAll([dbo].[AssetCategory])
End If
End Sub
End Class

Input Strings into String Arrays(Dynamic)

So I have two string arrays, one for my friends, the other for their numbers. I go to my combo box (displays list of names) I click on it and it displays their number on a label. My problem is I want the user to a enter a new name and number in 2 textboxes, then transfer the name on the combo box. Once their name is in the box, I click on it and it display's their number on label. Is there a way to transfer the new items onto my arrays?
Option Explicit On
Module MainModule
Public strPeople() As String = {"Kyle", "John", "Jake", "Donna", "Carly", "Ty", "Mavis"}
Public strPhoneNumbers() As String = {"945-1232", "804-2329", "290-7321", "928-4569", "205-9893", "320-0195", "305-4520"}
Public tempList As New List(Of String)
End Module
Here Is My Main Form
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange(strPeople)
End Sub
Private Sub AboutToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem1.Click
AboutBox1.ShowDialog()
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim strPhoneNums As String = strPhoneNumbers(ComboBox1.SelectedIndex)
Label3.Text = "Phone Number: " & strPhoneNums
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
//Add Contact Button
If TextBox1.Text <> "" Then
ReDim Preserve strPeople(7)
strPeople(7) = TextBox1.Text
ComboBox1.Items.Add(strPeople(7))
End If
If TextBox2.Text <> "" Then
ReDim Preserve strPhoneNumbers(7)
strPhoneNumbers(7) = TextBox2.Text
End If
TextBox1.Clear()
TextBox2.Clear()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub

I'm trying to open a form and getting this error

Okay, here is my code.
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = 1000
ProgressBar1.Value = 1000
Timer1.Interval = 1750
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Form3.Show()
Me.Close()
End Sub
End Class
On the line Form3.Show() I get
InvalidOperationException was unhandled.
An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.
What I have on Form3:
Public Class Form3
Public IPAddress As String = TextBox1.Text
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form4.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Form4.varible1 = True Then
Label1.Text = "IP: " + IPAddress
End If
End Sub
End Class
Any help?
Change on form3
Public IPAddress As String = TextBox1.Text ...."TextBox1.Text is "" thats why you get the error"
To
Public IPAddress As String
End then add IPAddress = TextBox1.Text in you timer
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Form4.varible1 = True Then
IPAddress = TextBox1.Text
Label1.Text = "IP: " + IPAddress
End If
End Sub

Throw New NotImplementedException - VB

I'm making an application that manages the inputs and outputs of a company's customers as well as the creation of the same, however when I "submit" the form of the creation of a new customer to me the following error:
"Throw New NotImplementedException"
I leave here my code, so they can help me.
FORM1.vb
Public Class Form1
Private Property cnt As Object
<SerializableAttribute> _
'<ComVisibleAttribute(True)> _
Public Class NotImplementedException _
'Inherits SystemException
End Class
Private Sub TabPage1_Click(sender As Object, e As EventArgs)
Dim cnt As New OleDb.OleDbConnection
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub GroupBox1_Enter(sender As Object, e As EventArgs)
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
TabControl1.SelectedTab = TabPage2
End Sub
Private Sub TabPage2_Click(sender As Object, e As EventArgs) Handles TabPage2.Click
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'cnt.ConnectionString = "Provinder=Microsoft.Jet.Oledb.40; Data Source=" & Application.StartupPath & "\bdtesteentradas.mdb"
'TODO: This line of code loads data into the 'BdtesteentradasDataSet.empresa' table. You can move, or remove it, as needed.
Me.EmpresaTableAdapter.Fill(Me.BdtesteentradasDataSet.empresa)
End Sub
Private Sub FillByToolStripButton_Click(sender As Object, e As EventArgs) Handles FillByToolStripButton.Click
Try
Me.EmpresaTableAdapter.FillBy(Me.BdtesteentradasDataSet.empresa)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Me.EmpresaBindingSource.Filter = "nif ='" & TextBox1.Text & "'"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox2.Text = ""
TextBox3.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox5.Text = ""
TextBox4.Text = ""
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Dim cnt As New OleDb.OleDbCommand
If Not ConnectionState.Open Then
End If
Cmd.Connection = cnt()
Cmd.CommandText = "INSERT INTO empresa (NIF, Empresa, nCliente, Distrito, NomeContacto, ApelidoContacto, Funcao" &
"VALUES(" & Me.TextBox6.Text & ")"
'Cmd.ExecuteNonQuery()
MsgBox("Dados inseridos.")
End Sub
Cmd.vb
Class Cmd
Shared Property CommandText As String
Shared Property Connection As Object
Shared Sub ExecuteNonQuery()
Throw New NotImplementedException
End Sub
End Class
Error message
Already a big thank you
You got error on your "cmd" variable and "fixed it" this way:
Didn't you? This generated the file Cmd.vb for you.
That is not what you want. Remove Cmd.vb from you project an try this:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'You want to check, what the State of *your* connection is
If Not cnt.State = ConnectionState.Open Then
End If
'Create a Command-Object from the Connection
Using cmd = cnt.CreateCommand()
cmd.CommandText = "INSERT INTO empresa (NIF, Empresa, nCliente, Distrito, NomeContacto, ApelidoContacto, Funcao" &
"VALUES(" & Me.TextBox6.Text & ")"
cmd.ExecuteNonQuery()
MsgBox("Dados inseridos.")
End Using
End Sub

Vb.net Error then use form4.show

I have a little problem with my program. I just maked a little program. I have a problem with code "Form4.Show()".
I put this code on button and then press button I receive error:
An error occurred creating the form. See Exception.IneerException for details. The error is: Object reference not set to an instance of an object.
I don't know what code I put wrong ... this button have only form4.show.
PS: this error is from try catch.
This is button code.
Try
Form4.Show
Catch ex as Exception
MessageBox.Show(ex.Message)
End Try
edit: all form4 code
Public Class Form4
Dim txt As String = Textbox1.Text
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
txt = "450IP-Gift"
Form5.Show()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
txt = "450IP"
Form5.Show()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
txt = "1150IP-Gift"
Form5.Show()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
txt = "1150IP"
Form5.Show()
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
txt = "3150IP-Gift"
Form5.Show()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
txt = "3150IP"
Form5.Show()
End Sub
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
txt = "4800IP-Gift"
Form5.Show()
End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
txt = "4800IP"
Form5.Show()
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
txt = "6300IP-Gift"
Form5.Show()
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
txt = "6300IP"
Form5.Show()
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
txt = "9600IP-Gift"
Form5.Show()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
txt = "9600IP"
Form5.Show()
End Sub
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
Me.Close()
End Sub
End Class
You need to initialize form4
Try
Dim form4 = New Form4()
form4.Show()
Catch ex as Exception
MessageBox.Show(ex.Message)
End Try