-
Imports System.Data.OleDb
-
Public Class Form1
-
Dim conn As OleDbConnection
-
Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Denden\Desktop\DB.mdb" 'i remove my db on desktop
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
-
conn = New OleDbConnection(constring)
-
Try
-
If conn.State <> ConnectionState.Open Then
-
conn.Open()
-
MsgBox("connected")
-
Else
-
MsgBox("failed")
-
End If
-
Catch ex As Exception
-
ex.Message.ToString()
-
End Try
-
-
conn.Close()
-
-
End Sub
-
End Class
-
i intentionally remove the database on my desktop just to try if my else code will execute but to my surprise the else statement didnt prompt.. Why that happens?
10 1631 Rabbit 12,516
Recognized Expert Moderator MVP
It's because you're checking if the state is not open. It's never going to be open before the check so it will never run the else.
i opened the connection before message will appear.
Rabbit 12,516
Recognized Expert Moderator MVP
Yes, you open it before the message appears. The problem is you open it after you check if it's open. Therefore, it is never open before you check.
@Rabbit
so is this the correct way? - If conn.State = ConnectionState.Closed Then
-
conn.Open()
-
MsgBox("Open")
-
Else
-
MsgBox("Close")
-
-
End If
-
-
Rabbit 12,516
Recognized Expert Moderator MVP
No, what I'm saying is you're testing a condition that will only ever go down one path. The if else is pointless. Basically, what you are doing is similar to this: - x = 0
-
-
If x = 0 Then
-
x = 1
-
MsgBox(x)
-
Else
-
MsgBox("else part of code")
-
End If
The code will never ever ever run the Else portion of the code because x will never not be 0. That's what your code is doing. Your connection will never be open because the only time you try to open the connection is after you do the check.
got it sir. i must open the connection before using the if statement :) it often see codes goes that way. Thats for enlightening my mind. Just a newbie. - Dim x As Integer
-
x = 1
-
If x = 0 Then
-
MsgBox(x)
-
Else
-
MsgBox("else part of code")
-
End If
-
Frinavale 9,735
Recognized Expert Moderator Expert
Check out the documentation for the OleDBConnection class. It contains a lot of useful information and code examples.
For example, the suggest utilizing the using statement to help manage resources.
Like this: -
Using connection As New OleDbConnection(connectionString)
-
' The insertSQL string contains a SQL statement that
-
' inserts a new row in the source table.
-
Dim command As New OleDbCommand(insertSQL)
-
-
' Set the Connection to the new OleDbConnection.
-
command.Connection = connection
-
-
' Open the connection and execute the insert command.
-
Try
-
connection.Open()
-
command.ExecuteNonQuery()
-
Catch ex As Exception
-
Console.WriteLine(ex.Message)
-
End Try
-
' The connection is automatically closed when the
-
' code exits the Using block.
-
End Using
Notice how they have a Try...Catch block around opening the connection and executing SQL commands with it?
That's because an exception will be thrown if a connection to the database cannot be established or if something else went wrong executing the SQL commands.
The catch section is the section you are interested in...
-Frinny
@frinavale
i am reading the articles @home&learn website and i found it different from yours, here the way they did on how to update data on database - Dim cb As New OleDb.OleDbCommandBuilder(da)
-
ds.Tables("Denden").Rows(inc).Item(1) = TextBox1.Text
-
ds.Tables("Denden").Rows(inc).Item(2) = TextBox2.Text
-
da.Update(ds, "Denden")
-
MsgBox("Data updated")
-
-
so how it is different from yours? what would be the best option between those.
i also checked the documentation
here what it is looks like - Public Sub OpenConnection(ByVal connectionString As String)
-
-
Using connection As New OleDbConnection(connectionString)
-
Try
-
connection.Open()
-
Console.WriteLine("DataSource: {0} Database: {1}", _
-
connection.DataSource, connection.Database)
-
Catch ex As Exception
-
Console.WriteLine(ex.Message)
-
End Try
-
' The connection is automatically closed when the
-
' code exits the Using block.
-
End Using
-
End Sub
-
' Define other methods and classes here
i was confused seeing that, would you mind explain this line of code - Console.WriteLine("DataSource: {0} Database: {1}", _
-
connection.DataSource, connection.Database)
and lastly can i substitute this line - Public Sub OpenConnection(ByVal connectionString As String)
to something like this - Public Sub OpenConnection()
-
dim connectionString as string
-
many thanks sir.
Frinavale 9,735
Recognized Expert Moderator Expert
Hi Denden,
...how it is different from yours? what would be the best option between those.
It looks like this code is using a DataSet and a DataAdapter to update the database. You don't need to use a DataSet nor do you need to use a DataAdapter to execute an insert/update/delete command; however, using these controls do have their advantages especially if you are planning on updating a whole bunch of records at the same time. - Dim cb As New OleDb.OleDbCommandBuilder(da)
-
ds.Tables("Denden").Rows(inc).Item(1) = TextBox1.Text
-
ds.Tables("Denden").Rows(inc).Item(2) = TextBox2.Text
-
da.Update(ds, "Denden")
-
MsgBox("Data updated")
-
-
The code that I posted simply creates a connection, opens the connection and executes an insert command. -
Using connection As New OleDbConnection(connectionString)
-
' The insertSQL string contains a SQL statement that
-
' inserts a new row in the source table.
-
Dim command As New OleDbCommand(insertSQL)
-
-
' Set the Connection to the new OleDbConnection.
-
command.Connection = connection
-
-
' Open the connection and execute the insert command.
-
Try
-
connection.Open()
-
command.ExecuteNonQuery()
-
Catch ex As Exception
-
Console.WriteLine(ex.Message)
-
End Try
-
' The connection is automatically closed when the
-
' code exits the Using block.
-
End Using
-
So, to answer your question, I would recommend using the OleDbDataAdapter solution if you are planning on inserting/updating/deleting a bunch of records at the same time.
... would you mind explain this line of code - Console.WriteLine("DataSource: {0} Database: {1}", _
-
connection.DataSource, connection.Database)
This code prints the DataSource property and the Database property of the OleDBConnection object onto the screen.
and lastly can i substitute this line - Public Sub OpenConnection(ByVal connectionString As String)
to something like this - Public Sub OpenConnection()
-
dim connectionString as string
-
Of course you can!
You just need some way to get the connection string...It could even be stored in a .config file.
-Frinny
wow. thumbs up for the explanation. by the way, im using datareader in reading data, is that the best to use.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Bina Desai |
last post by:
I get the following error: Error Type:
Microsoft VBScript runtime (0x800A01A8)
Object required: ''
/myweb4/authorised_user_page.asp, line 70
and line 70 of my code is: <% rsCheckUser1.Open...
|
by: Barnes |
last post by:
I have an asp form that sends data to a redirect page with SQL using JScript. There is a function that I'm using called replace(). The form submits without errors and goes to the redirect page but...
|
by: ABC |
last post by:
Follow my project, the connection database will not use Trused Connection
from web server to database server. How to set DAAB's connection string
with No Credentials Access?
|
by: amber |
last post by:
Hello,
Not sure what is going wrong here.
I just upgraded to .NET 2005 and SQL Server Express 2005 from .NET 2003 and
MSDE.
I've been working on a huge project for ages, which I 'converted' to...
|
by: Ike |
last post by:
Is anyone aware of a means of connection pooling (to MySQL, say) in php?
Thanks, Ike
|
by: OutdoorGuy |
last post by:
Greetings,
I am attempting to use VB.NET 2003 to loop through all of the tables in
a SQL Server 2005 database. However, I have yet to figure this out.
Does anyone have any suggestions? My...
|
by: bylum |
last post by:
help find error that connection database and jsp..
<%
String connectionURL = "jdbc:mysql://localhost:3306/webaudio?user=root";
Connection connection = null;
Statement statement = null;...
|
by: bylum |
last post by:
help find error that connection database and jsp..
<%
String connectionURL = "jdbc:mysql://localhost:3306/webaudio?user=root";
Connection connection = null;
Statement statement = null;...
|
by: =?Utf-8?B?ZGF2aWQ=?= |
last post by:
When I start the Visual Studio .NET 2005 on client machine, I select Tools
and database connection (Database: SQL Server, security: windows
authentication. Server: Server Machine name (not in the...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |