0

I dont know how i Can fixed the wrong in my program...the problem is con.open()

Private Sub btnadd1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnadd1.Click Dim cmd As New OleDb.OleDbCommand Dim con As New OleDb.OleDbConnection Dim Printlist1 As New DataTable If Not con.State = ConnectionState.Open Then con.Open() cmd.Connection = con End If If Me.text1.Tag & "" = "" Then cmd.CommandText = "INSERT INTO Datagrid1(StickerCode, Description, Company, Department, Location, User, SerialNumber, DatePurchased, Tagable, Quantity, Brand, Model ) " & _ " VALUES(" & Me.text1.Text & ",'" & Me.text2.Text & "','" & _ Me.text3.Text & "','" & Me.text4.Text & "','" & Me.text5.Text & "','" & _ Me.text6.Text & "','" & Me.text7.Text & "','" & Me.text8.Text & "','" & _ Me.text9.Text & "','" & Me.text10.Text & "','" & Me.text11.Text & "','" & _ Me.text12.Text & "')" cmd = New OleDbCommand(cmd.CommandText, con) cmd.ExecuteNonQuery() Else cmd.CommandText = "UPDATE Form4 " & _ " SET StickerCode='" & Me.text1.Text & _ ", Description='" & Me.text2.Text & "'" & _ ", Company='" & Me.text3.Text & "'" & _ ", Department='" & Me.text4.Text & "'" & _ ", Location='" & Me.text5.Text & "'" & _ ", User='" & Me.text6.Text & "'" & _ ", SerialNumber='" & Me.text7.Text & "'" & _ ", DatePurchased='" & Me.text8.Text & "'" & _ ", Tagable='" & Me.text9.Text & "'" & _ ", Quantity='" & Me.text10.Text & "'" & _ ", Brand='" & Me.text11.Text & "'" & _ ", Model='" & Me.text12.Text & "'" & _ " WHERE text1=" & Me.text1.Tag cmd.ExecuteNonQuery() End If RefreshData() Me.btnclear1.PerformClick() con.Close() End Sub 
2
  • 3
    SQL Injection alert - you should not concatenate together your SQL statements - use parametrized queries instead to avoid SQL injection. STOP right now, don't go any further, first learn what parametrized queries are and how to use them, before you get used to this terrible, bad, horribly dangerous habit of concatenating together your SQL statements in code! Commented Dec 2, 2013 at 16:48
  • 1
    This code is practically begging to get hacked. Commented Dec 2, 2013 at 17:55

2 Answers 2

1

You haven't passed a connection string to your connection object so it has no idea which database to open or where to find it.

Try this:

Dim con As New OleDb.OleDbConnection("My connection string") 
Sign up to request clarification or add additional context in comments.

Comments

1

You are not giving the OleDbConnection object a connection string. The connection string tells the OleDbConnection object to which database you want it to connect. You can give it the connection string in the constructor, like this:

Dim con As New OleDb.OleDbConnection(connectionString) 

Or, you can set it afterwards, via the ConnectionString property, like this:

Dim con As New OleDb.OleDbConnection() con.ConnectionString = connectionString 

The format for the actual connection string varies depending on the database engine and version. There are plenty of resources online, though, which will show you the correct format to use and what options it can include. Just as an example, it might look something like this:

Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\MyDb.mdb" 

UPDATE

Sorry if I've confused you by discussing the steps out of order. Basically, all you need to do is to give the connection string to the OleDbConnection object. You need to do so before you call its Open method. For instance, you can change the following line:

Dim con As New OleDb.OleDbConnection 

To this:

Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\MyDb.mdb") 

Obviously, however, you need to change the connection string to whatever it ought to be for your particular situation. The one above is merely an example of what a valid connection string looks like.

Or, if you don't want to give the connection string to the OleDbConnection object right away, you can wait to give it to it later, as long as you set it before calling Open. For instance, you could change the following lines:

Dim cmd As New OleDb.OleDbCommand Dim con As New OleDb.OleDbConnection Dim Printlist1 As New DataTable If Not con.State = ConnectionState.Open Then con.Open() ' ... 

To this:

Dim cmd As New OleDb.OleDbCommand Dim con As New OleDb.OleDbConnection Dim Printlist1 As New DataTable If Not con.State = ConnectionState.Open Then con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\MyDb.mdb" con.Open() ' ... 

The original example, which seems to have confused you, first stored the connection string in a variable, and then passed the value from that variable to the connection object, like this:

Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\MyDb.mdb" Dim con As New OleDb.OleDbConnection(connectionString) 

4 Comments

sir i didnt use the the code to connect database,because i dont know how to use that..im using add database only in VB.net.
@user3049808 I'm sorry. I'm having trouble understanding you. Are you still having the problem? I was just referring to the code you posted. Are you saying that the code you posted is auto-generated? It doesn't look like it to me. If it is, there's go to be a property somewhere in the designer where you can specify the connection string. Otherwise, you just need to set the connection string, as I demonstrated, before calling con.Open.
Yes sir.sorry if you cant understand me.. Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\MyDb.mdb" i dont know how to use this code..
@user3049808 I updated my answer. Hopefully that clears it up for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.