I need help connecting to access using vb.net - 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?

Related

How do I make it so everyone has a separate account when they login

I am trying to create a login system for a revision app. However, I was wondering if there was a way where everyone can have separate accounts.
Private Sub Btnlogin_Click(sender As Object, e As EventArgs) Handles Btnlogin.Click
Dim sqlstring As String
sqlstring = "select * FROM login where username = '" & txtusername.Text & "'"
connection.Open()
dataadapter = New OleDb.OleDbDataAdapter(sqlstring, connection)
dt.Clear()
dataadapter.Fill(dt)
connection.Close()
If dt.Rows.Count = 0 Then
MsgBox("no such user")
Exit Sub
End If
If dt.Rows(0)(2) = Txtpassword.Text Then
Flashcard.Show()
Else
Txtpassword.Text = ""
txtusername.Text = ""
MsgBox("Invalid username and password combination")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Register.Show()
End Sub
That's the code I have if a user already has an account.
Imports System.Data.OleDb
Public Class Register
Dim pro As String
Dim connstring As String
Dim command As String
Dim myconnection As OleDbConnection = New oledbconnection
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
pro = "provider=microsoft.ACE.OLEDB.12.0;Data Source=flashcard login.accdb"
connstring = pro
myconnection.ConnectionString = connstring
myconnection.Open()
command = " insert into login ([username],[password]) values ('" & TextBox1.Text & "','" & TextBox2.Text & "')"
Dim cmd As OleDbCommand = New OleDbCommand(command, myconnection)
cmd.Parameters.Add(New OleDbParameter("username", CType(TextBox1.Text, String)))
cmd.Parameters.Add(New OleDbParameter("password", CType(TextBox1.Text, String)))
MsgBox("You have successfully signed up!")
Form1.Show()
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
myconnection.Close()
TextBox1.Clear()
TextBox2.Clear()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
That's the code if the user presses the register button (does not have an account).
This code works where the user can succesfully login or register, but the problem is that everyone will have the same windows form. So, I was wondering if there was a way to make each windows form unique to each user?
You can declare and initialize a variable on the same line. The only information you need is if the userName and password exist in the database. Don't pull down data your don't need. You can get this with Count.
Always declare and dispose the connection and command it the method where they are used. Both of these objects need to be disposed so that their unmanaged resources can be released. Using... End Using blocks handle this.
Always use parameters to avoid sql injection. In OleDb the order that the parameters appear in the sql string must match the order that they are added to the parameters collection.
Don't open the connection until directly before the .Execute...
Private ConStr As String = "provider=microsoft.ACE.OLEDB.12.0;Data Source=flashcard login.accdb" '"Your connection string"
Private Sub Btnlogin_Click(sender As Object, e As EventArgs) Handles Btnlogin.Click
Dim rowCount As Integer
Dim sqlstring = "select Count(*) FROM login where [username] = #UserName ANd [password] = #password;"
Using connection As New OleDbConnection(ConStr),
cmd As New OleDbCommand(sqlstring, connection)
cmd.Parameters.Add("#UserName", OleDbType.VarChar).Value = txtusername.Text
connection.Open()
rowCount = CInt(cmd.ExecuteScalar)
End Using
If rowCount = 0 Then
MsgBox("no such user")
Else
Flashcard.Show()
End If
End Sub
Same ideas for the insert. A text box's Text property is always a String so you don't have to convert it.
You are not doing the parameters correctly. You are not using these parameters because the don't appear in the CommandText
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Command = " insert into login ([username],[password]) values (#UserName, #Password);"
Try
Using myconnection As New OleDbConnection(ConStr),
cmd As OleDbCommand = New OleDbCommand(Command, myconnection)
cmd.Parameters.Add("#UserName", OleDbType.VarChar).Value = TextBox1.Text
cmd.Parameters.Add("#Password", OleDbType.VarChar).Value = TextBox2.Text
myconnection.Open()
cmd.ExecuteNonQuery()
End Using
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
MsgBox("You have successfully signed up!")
Form1.Show()
TextBox1.Clear()
TextBox2.Clear()
End Sub
Finally, and very important. You should never store passwords as plain text. Look into salting and encrypting.

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.

Connection property has not been initialized - VB.net ExecuteNonQuery Error [duplicate]

Getting an error here not sure why its not opening the connection. Hoping someone can help me.
Protected Sub Btn_Submit_Click(ByVal sender As System.Object, e As System.EventArgs) Handles Btn_Submit.Click
Dim Sqlstr As String
Dim con As SqlConnection
Dim connectionString As String = "Data Source=DB\TEST;Initial Catalog=Orders;Integrated Security=True"
Dim cmdInsert As New SqlCommand(Sqlstr, con)
Sqlstr = "insert into customers(FirstName,LastName,Email,Phone,Address,City,State,Zip) values (#FirstName,#LastName,#Email,#Phone,#Address,#City,#State,#Zip)"
Try
Using connection As New SqlConnection(connectionString)
connection.Open()
cmdInsert.Parameters.Add("#FirstName", Data.SqlDbType.NVarChar).Value = FirstName.Text()
cmdInsert.Parameters.Add("#LastName", Data.SqlDbType.NVarChar).Value = LastName.Text
cmdInsert.Parameters.Add("#Email", Data.SqlDbType.NVarChar).Value = Email.Text
cmdInsert.Parameters.Add("#Phone", Data.SqlDbType.NChar).Value = Phone.Text
cmdInsert.Parameters.Add("#Address", Data.SqlDbType.NVarChar).Value = Address.Text
cmdInsert.Parameters.Add("#City", Data.SqlDbType.NVarChar).Value = City.Text
cmdInsert.Parameters.Add("#State", Data.SqlDbType.NVarChar).Value = State.Text
cmdInsert.Parameters.Add("#Zip", Data.SqlDbType.NChar).Value = Zip.Text
cmdInsert.ExecuteNonQuery()
connection.Close()
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
You have two SqlConnection objects - one in Dim con As SqlConnection and then another in your using statement. Create the SqlCommand after the using statement and pass the connection in to the constructor.
You have the wrong scope -- you're instantiating your SqlCommand before instantiating and opening the actual SQL connection you're trying to use to execute the command.
I believe this is what will fix your code (I moved the insert call into the using scope):
Protected Sub Btn_Submit_Click(ByVal sender As System.Object, e As System.EventArgs) Handles Btn_Submit.Click
Dim Sqlstr As String
Dim connectionString As String = "Data Source=DB\TEST;Initial Catalog=Orders;Integrated Security=True"
Sqlstr = "insert into customers(FirstName,LastName,Email,Phone,Address,City,State,Zip) values (#FirstName,#LastName,#Email,#Phone,#Address,#City,#State,#Zip)"
Try
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim cmdInsert As New SqlCommand(Sqlstr, connection) <----- **** Moved this here, changed the connection
cmdInsert.Parameters.Add("#FirstName", Data.SqlDbType.NVarChar).Value = FirstName.Text()
cmdInsert.Parameters.Add("#LastName", Data.SqlDbType.NVarChar).Value = LastName.Text
cmdInsert.Parameters.Add("#Email", Data.SqlDbType.NVarChar).Value = Email.Text
cmdInsert.Parameters.Add("#Phone", Data.SqlDbType.NChar).Value = Phone.Text
cmdInsert.Parameters.Add("#Address", Data.SqlDbType.NVarChar).Value = Address.Text
cmdInsert.Parameters.Add("#City", Data.SqlDbType.NVarChar).Value = City.Text
cmdInsert.Parameters.Add("#State", Data.SqlDbType.NVarChar).Value = State.Text
cmdInsert.Parameters.Add("#Zip", Data.SqlDbType.NChar).Value = Zip.Text
cmdInsert.ExecuteNonQuery()
connection.Close()
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
Protected Sub Btn_Submit_Click(ByVal sender As System.Object, e As System.EventArgs) Handles Btn_Submit.Click
Dim Sqlstr As String
' =================================================
' This is a declaration, not an instantiation
' =================================================
Dim con As SqlConnection
Dim connectionString As String = "Data Source=DB\TEST;Initial Catalog=Orders;Integrated Security=True"
' =================================================
' con is Nothing here
' =================================================
Dim cmdInsert As New SqlCommand(Sqlstr, con)
Sqlstr = "insert into customers(FirstName,LastName,Email,Phone,Address,City,State,Zip) values (#FirstName,#LastName,#Email,#Phone,#Address,#City,#State,#Zip)"
Try
' =================================================
' connection is not what you passed to cmdInsert
' =================================================
Using connection As New SqlConnection(connectionString)
connection.Open()
Connection Sting Property Error always occur when the connection is not declared/renewed
the solution is simple
dim con as new SqlConnection
con.connectionString = "Provide your SQL connection"
con.open
'write code or action you want to perform
con.close
Hope it helps.

Register Form Issues VB.Net

I am having an issue when I am setting up this Register form.
My current code is this:
Public Class Form2
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim MyDocumentsFolder As String
Dim TheDatabase As String
Dim FullDatabasePath As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim DBTest1 As String
Dim DBTestP1 As String
Dim cmd As New OleDbCommand(sql, con)
Dim connStr As String
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connection As New OleDb.OleDbConnection(connStr)
dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
TheDatabase = "\Robocopy_Test.accdb"
MyDocumentsFolder = "C:\Users\Dan\Desktop\WindowsApplication2"
FullDatabasePath = MyDocumentsFolder & TheDatabase
dbSource = "Data Source = C:\Users\Dan\Desktop\WindowsApplication2\Robocopy_Testaccdb1.accdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT * FROM Robocopy"
da = New OleDb.OleDbDataAdapter(sql, con)
'da.Fill(ds, "Robocopy")
MessageBox.Show("Databse is Open")
DBTest1 = DBTest.Text
DBTestP1 = DBTestP.Text
'DBTest.Text = ds.Tables("Robocopy").Rows(0).Item(1)
'DBTestP.Text = ds.Tables("Robocopy").Rows(0).Item(2
sql = "INSERT INTO Robocopy(username,password) VALUES('" & DBTest1 & "','" & DBTestP1 & "')"
cmd.Connection = connection
connection.Open()
cmd.CommandText = sql
da.InsertCommand = cmd
da.InsertCommand.ExecuteNonQuery()
connection.Close()
'With cmd.Parameters
'.AddWithValue("usernamer", DBTest.Text)
'.AddWithValue("password", DBTestP.Text)
'.AddWithValue("email", txtsub.text)
'.AddWithValue("contactnum", txtau.text)
'End With
'cmd.ExecuteNonQuery()
End Sub
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
con.Close()
MessageBox.Show("Database Is now Closed")
End Sub
End Class
I am having the issue at connection.open(). The error that I am having is
The ConnectionString property has not been initialized.
I have been trying for the past hour to find different ways to write to the database but to no prevail and I cannot figure this out.
[In response to Steve
My code after editing and still the same error
Imports System.Data.OleDb
Public Class Form2
Dim connection As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim MyDocumentsFolder As String
Dim TheDatabase As String
Dim FullDatabasePath As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim DBTest1 As String
Dim DBTestP1 As String
Dim cmd As New OleDbCommand(sql, connection)
Dim connStr As String
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connection As New OleDb.OleDbConnection(connStr)
dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
TheDatabase = "\Robocopy_Test.accdb"
MyDocumentsFolder = "C:\Users\Dan\Desktop\WindowsApplication2"
FullDatabasePath = MyDocumentsFolder & TheDatabase
dbSource = "Data Source = C:\Users\Dan\Desktop\WindowsApplication2\Robocopy_Testaccdb1.accdb"
Me.connection.ConnectionString = dbProvider & dbSource
Me.connection.Open()
sql = "SELECT * FROM Robocopy"
da = New OleDb.OleDbDataAdapter(sql, connection)
'da.Fill(ds, "Robocopy")
MessageBox.Show("Databse is Open")
DBTest1 = DBTest.Text
DBTestP1 = DBTestP.Text
'DBTest.Text = ds.Tables("Robocopy").Rows(0).Item(1)
'DBTestP.Text = ds.Tables("Robocopy").Rows(0).Item(2
sql = "INSERT INTO Robocopy(username,password) VALUES('" & DBTest1 & "','" & DBTestP1 & "')"
cmd.Connection = connection
connection.Open()
cmd.CommandText = sql
da.InsertCommand = cmd
da.InsertCommand.ExecuteNonQuery()
connection.Close()
'With cmd.Parameters
'.AddWithValue("usernamer", DBTest.Text)
'.AddWithValue("password", DBTestP.Text)
'.AddWithValue("email", txtsub.text)
'.AddWithValue("contactnum", txtau.text)
'End With
'cmd.ExecuteNonQuery()
End Sub
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
connection.Close()
MessageBox.Show("Database Is now Closed")
End Sub
End Class
Global variables could be very ....evil. Expecially if you name them with the same name of a local variable.
Me.connection is not the same variable connection declared as local variable inside the sub. You set the connection string on the global variable then use the local variable without any connection string
Change these two lines
Me.connection.ConnectionString = dbProvider & dbSource
Me.connection.Open()
removing the Me.
connection.ConnectionString = dbProvider & dbSource
connection.Open()
and don't open the connection two times.
In any case, you don't need the adapter at all to execute an insert command
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
dbSource = "Data Source = C:\Users\Dan\Desktop\WindowsApplication2\Robocopy_Testaccdb1.accdb"
Dim connStr = dbProvider & dbSource
DBTest1 = DBTest.Text
DBTestP1 = DBTestP.Text
sql = "INSERT INTO Robocopy(username,[password]) VALUES('" & DBTest1 & "','" & DBTestP1 & "')"
Using connection = New OleDb.OleDbConnection(connStr)
Using cmd = new OleDb.OleDbCommand(sql, connection )
connection.Open()
cmd.ExecuteNonQuery()
'With cmd.Parameters
'.AddWithValue("usernamer", DBTest.Text)
'.AddWithValue("password", DBTestP.Text)
'.AddWithValue("email", txtsub.text)
'.AddWithValue("contactnum", txtau.text)
'End With
'cmd.ExecuteNonQuery()
End Using
End Using
End Sub
I see also that you have commented out the Parameterized approach to your query. Please do yourself a favour and restore as soon as possible the parameters logic. It is a lot more safe and avoids numerous errors
Finally Password is a reserved keyword in Access.Use square brakets around it otherwise you will see an unexplicable "Syntax Error" in your insert command

ExecuteNonQuery: Connection property has not been initialized VB

Getting an error here not sure why its not opening the connection. Hoping someone can help me.
Protected Sub Btn_Submit_Click(ByVal sender As System.Object, e As System.EventArgs) Handles Btn_Submit.Click
Dim Sqlstr As String
Dim con As SqlConnection
Dim connectionString As String = "Data Source=DB\TEST;Initial Catalog=Orders;Integrated Security=True"
Dim cmdInsert As New SqlCommand(Sqlstr, con)
Sqlstr = "insert into customers(FirstName,LastName,Email,Phone,Address,City,State,Zip) values (#FirstName,#LastName,#Email,#Phone,#Address,#City,#State,#Zip)"
Try
Using connection As New SqlConnection(connectionString)
connection.Open()
cmdInsert.Parameters.Add("#FirstName", Data.SqlDbType.NVarChar).Value = FirstName.Text()
cmdInsert.Parameters.Add("#LastName", Data.SqlDbType.NVarChar).Value = LastName.Text
cmdInsert.Parameters.Add("#Email", Data.SqlDbType.NVarChar).Value = Email.Text
cmdInsert.Parameters.Add("#Phone", Data.SqlDbType.NChar).Value = Phone.Text
cmdInsert.Parameters.Add("#Address", Data.SqlDbType.NVarChar).Value = Address.Text
cmdInsert.Parameters.Add("#City", Data.SqlDbType.NVarChar).Value = City.Text
cmdInsert.Parameters.Add("#State", Data.SqlDbType.NVarChar).Value = State.Text
cmdInsert.Parameters.Add("#Zip", Data.SqlDbType.NChar).Value = Zip.Text
cmdInsert.ExecuteNonQuery()
connection.Close()
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
You have two SqlConnection objects - one in Dim con As SqlConnection and then another in your using statement. Create the SqlCommand after the using statement and pass the connection in to the constructor.
You have the wrong scope -- you're instantiating your SqlCommand before instantiating and opening the actual SQL connection you're trying to use to execute the command.
I believe this is what will fix your code (I moved the insert call into the using scope):
Protected Sub Btn_Submit_Click(ByVal sender As System.Object, e As System.EventArgs) Handles Btn_Submit.Click
Dim Sqlstr As String
Dim connectionString As String = "Data Source=DB\TEST;Initial Catalog=Orders;Integrated Security=True"
Sqlstr = "insert into customers(FirstName,LastName,Email,Phone,Address,City,State,Zip) values (#FirstName,#LastName,#Email,#Phone,#Address,#City,#State,#Zip)"
Try
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim cmdInsert As New SqlCommand(Sqlstr, connection) <----- **** Moved this here, changed the connection
cmdInsert.Parameters.Add("#FirstName", Data.SqlDbType.NVarChar).Value = FirstName.Text()
cmdInsert.Parameters.Add("#LastName", Data.SqlDbType.NVarChar).Value = LastName.Text
cmdInsert.Parameters.Add("#Email", Data.SqlDbType.NVarChar).Value = Email.Text
cmdInsert.Parameters.Add("#Phone", Data.SqlDbType.NChar).Value = Phone.Text
cmdInsert.Parameters.Add("#Address", Data.SqlDbType.NVarChar).Value = Address.Text
cmdInsert.Parameters.Add("#City", Data.SqlDbType.NVarChar).Value = City.Text
cmdInsert.Parameters.Add("#State", Data.SqlDbType.NVarChar).Value = State.Text
cmdInsert.Parameters.Add("#Zip", Data.SqlDbType.NChar).Value = Zip.Text
cmdInsert.ExecuteNonQuery()
connection.Close()
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
Protected Sub Btn_Submit_Click(ByVal sender As System.Object, e As System.EventArgs) Handles Btn_Submit.Click
Dim Sqlstr As String
' =================================================
' This is a declaration, not an instantiation
' =================================================
Dim con As SqlConnection
Dim connectionString As String = "Data Source=DB\TEST;Initial Catalog=Orders;Integrated Security=True"
' =================================================
' con is Nothing here
' =================================================
Dim cmdInsert As New SqlCommand(Sqlstr, con)
Sqlstr = "insert into customers(FirstName,LastName,Email,Phone,Address,City,State,Zip) values (#FirstName,#LastName,#Email,#Phone,#Address,#City,#State,#Zip)"
Try
' =================================================
' connection is not what you passed to cmdInsert
' =================================================
Using connection As New SqlConnection(connectionString)
connection.Open()
Connection Sting Property Error always occur when the connection is not declared/renewed
the solution is simple
dim con as new SqlConnection
con.connectionString = "Provide your SQL connection"
con.open
'write code or action you want to perform
con.close
Hope it helps.