Hack Forums
[.NET] Searched The Whole Internet For This ACCESS DATABASE And VB.NET Connect ERROR!! - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Searched The Whole Internet For This ACCESS DATABASE And VB.NET Connect ERROR!! (/showthread.php?tid=3505784)



Searched The Whole Internet For This ACCESS DATABASE And VB.NET Connect ERROR!! - scoder - May 26, 2013

[Image: 31vSJ.png]

Source :

PHP Code

<?php 
Dim database 
As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & System.Environment.CurrentDirectory & "\test.accdb")
  
Dim cmdadd As New OleDb.OleDbCommand
  cmdadd
.CommandText = "INSERT INTO table (ID, name) VALUES (@ID, @name);"
  
cmdadd.Connection = database
  cmdadd
.Parameters.AddWithValue("ID", txtid.Text)
  
cmdadd.Parameters.AddWithValue("name", txtname.Text)
  
database.Open()
  
cmdadd.ExecuteNonQuery()
  
database.Close() 

and Tested This :

PHP Code

<?php 
If Me.txtidsamad.Tag & "" = "" Then
    
'add new 
    '
add data to table
    cmd
.CommandText = "INSERT INTO table([iD], [name]) VALUES (" & Me.txtid.Text & "," & Me.txtname.Text & ")"
    
cmd.ExecuteReader()
    
cnn.Close()
    
MsgBox("Added Successfully")
    Else
    
msgbox(ex.message)
    
End If

  
End If 

Working on Access Database (Office 2007)

[Image: 31vXq.png]

Any Help ?
asked Friends in HF and google, Yahoo, Bing And everything!!

Thanks in advance.


* - C3ol - May 26, 2013

"Insert Into db (name, id) Values('a', '1')"


* - Cody8295 - May 26, 2013

Yeah, try putting single quotes before/after the double quotes before your variables. Like this:

cmd.CommandText = "INSERT INTO table([iD], [name]) VALUES ('" & Me.txtid.Text & "','" & Me.txtname.Text & "')"


* - x64 - May 26, 2013

(May 26, 2013 08:15 AM)Cody8295 Wrote:  Yeah, try putting single quotes before/after the double quotes before your variables. Like this:

cmd.CommandText = "INSERT INTO table([iD], [name]) VALUES ('" & Me.txtid.Text & "','" & Me.txtname.Text & "')"

No that's a very bad habit. Just use parameters for inserting variables.


* - scoder - May 26, 2013

(May 26, 2013 07:15 AM)C3ol Wrote:  "Insert Into db (name, id) Values('a', '1')"

Got the Same Error another solution ?..
Thanks in advance


(May 26, 2013 08:15 AM)Cody8295 Wrote:  Yeah, try putting single quotes before/after the double quotes before your variables. Like this:

cmd.CommandText = "INSERT INTO table([iD], [name]) VALUES ('" & Me.txtid.Text & "','" & Me.txtname.Text & "')"

ExecuteNonQuery Error Same Error ..
Thanks For Helping anyways..🤨


(May 26, 2013 08:27 AM)Lambik Wrote:  No that's a very bad habit. Just use parameters for inserting variables.

Hey man can you help me with that..?
Thank you


* - x64 - May 26, 2013

(May 26, 2013 08:49 AM)scoder Wrote:  Got the Same Error another solution ?..
Thanks in advance



ExecuteNonQuery Error Same Error ..
Thanks For Helping anyways..🤨

When using parameters you're a supposed to replace the word you've added.

So not :

Code
cmdadd.Parameters.AddWithValue("ID", txtid.Text)
  cmdadd.Parameters.AddWithValue("name", txtname.Text)

But

Code
cmdadd.Parameters.AddWithValue("@ID", txtid.Text)
  cmdadd.Parameters.AddWithValue("@name", txtname.Text)



* - scoder - May 26, 2013

(May 26, 2013 08:52 AM)Lambik Wrote:  When using parameters you're a supposed to replace the word you've added.

So not :

Code
cmdadd.Parameters.AddWithValue("ID", txtid.Text)
  cmdadd.Parameters.AddWithValue("name", txtname.Text)

But

Code
cmdadd.Parameters.AddWithValue("@ID", txtid.Text)
  cmdadd.Parameters.AddWithValue("@name", txtname.Text)
So the Code Will Be Like This :
PHP Code

<?php 
Dim database 
As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & System.Environment.CurrentDirectory & "\test.accdb")
  
Dim cmdadd As New OleDb.OleDbCommand
  cmdadd
.CommandText = "INSERT INTO table (ID, name) VALUES (@ID, @name);"
  
cmdadd.Connection = database
  cmdadd
.Parameters.AddWithValue("@ID", txtid.Text)
  
cmdadd.Parameters.AddWithValue("@name", txtname.Text)
  
database.Open()
  
cmdadd.ExecuteNonQuery()
  
database.Close() 

But Still Getting The Same Error.. :/


* - Cody8295 - May 26, 2013

(May 26, 2013 08:27 AM)Lambik Wrote:  No that's a very bad habit. Just use parameters for inserting variables.

... This isn't PHP, you can't do it like that.


* - x64 - May 26, 2013

(May 26, 2013 09:53 AM)Cody8295 Wrote:  ... This isn't PHP, you can't do it like that.

I know it's not PHP. When using queries in ODB, you can use parameters for data. Look at my previous post
It's injection safe.