Jump to content

Inserting info to Access Database using textbox

- - - - -

  • Please log in to reply
4 replies to this topic

#1
pittpitu

pittpitu

    Newbie

  • Members
  • PipPip
  • 17 posts
Hi guys,

I am having some issues to enter some code to the database using text in a textbox.
I am using VS Studio 2010 Ultimate, MS Access database .mdb

This is what I am using

Private Sub btnData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnData.Click
        Dim con As OleDb.OleDbConnection
        Dim insert As String
        Dim ds As New DataSet
        Dim da As OleDb.OleDbDataAdapter
       
 
Try
   
            Dim myConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
            & "Data Source=C:\Users\Pitt\Documents\trainingbegin11.mdb;"
            '"Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source = C:\Users\Edward\Desktop\db1.mdb;"
 
            con = New OleDb.OleDbConnection(myConnectionString)
            con.Open()
            insert = "INSERT INTO Customerz (CustomerID, CompanyName,City) VALUES (@value1, @value2, @value3) "
            da = New OleDb.OleDbDataAdapter(insert, con)
            Dim myOleDbCommand As New OleDb.OleDbCommand(insert, con)
            myOleDbCommand.Parameters.Add(myOleDbCommand.CreateParameter).ParameterName = "@value1"
            myOleDbCommand.Parameters.Add(myOleDbCommand.CreateParameter).ParameterName = "@value2"
            myOleDbCommand.Parameters.Add(myOleDbCommand.CreateParameter).ParameterName = "@value3"
            myOleDbCommand.Parameters("@value1").Value = TextBox1.Text
            myOleDbCommand.Parameters("@value2").Value = TextBox2.Text
            myOleDbCommand.Parameters("@value3").Value = TextBox3.Text
            MessageBox.Show("the database is now open")
 
            myOleDbCommand = con.CreateCommand
            myOleDbCommand.CommandType = CommandType.Text
            myOleDbCommand.CommandText = insert
            myOleDbCommand.ExecuteNonQuery()
            con.Close()
 
        Catch ex As Exception
            Trace.WriteLine(ex.ToString)
        End Try
    End Sub


What I have tried
1: Declare Variables Value and to add them in the query
'Dim value1, value2, value3 As String
'value1 = TextBox1.Text
'value3 = TextBox3.Text
'value2 = TextBox2.Text
Sqlstring
insert = "INSERT INTO Customerz (CustomerID, CompanyName,City) VALUES (value1, value2, value3) "

2. Using Parameters style1
myOleDbCommand.Parameters.Add(myOleDbCommand.CreateParameter).ParameterName = "@value1"
yOleDbCommand.Parameters("@value1").Value = TextBox1.Text

SQLstring insert = "INSERT INTO Customerz (CustomerID, CompanyName,City) VALUES (@value1, @value2, @value3) "

3. Using Parameters style 2
'myOleDbCommand.CreateParameter.ParameterName = "@value1"
'myOleDbCommand.Parameters.AddWithValue("@value1", TextBox1.Text)

SQLstring insert = "INSERT INTO Customerz (CustomerID, CompanyName,City) VALUES (@value1, @value2, @value3) "

If I put hardcoded info in the query it does update the table
insert = "INSERT INTO Customerz (CustomerID, CompanyName,City) VALUES ('12345', 'Dell Inc' , 'Las Vegas') "

#2
RHankinsJr

RHankinsJr

    Newbie

  • Members
  • Pip
  • 8 posts
Debug.print your insert statement, does it look anything like your hard coded one?

If you are planning on really using this for an internship, I would highly suggest using proper spellings and naming your objects properly. No one is going to take you seriously with a CUSTOMERZ table.

#3
twalk

twalk

    Newbie

  • Members
  • Pip
  • 3 posts
Hello,

The '@' means to refer to a parameter, so I think you should try:

myOleDbCommand.Parameters.Add(myOleDbCommand.Creat eParameter).ParameterName = "value1"
yOleDbCommand.Parameters("value1").Value = TextBox1.Text

and:

SQLstring insert = "INSERT INTO Customerz (CustomerID, CompanyName,City) VALUES (@value1, @value2, @value3) "

#4
pittpitu

pittpitu

    Newbie

  • Members
  • PipPip
  • 17 posts
Sorry I haven't replied for a few days,

@RhankinsJR: This isn't the actual database, I was trying to make a small application of the different things I need to do then to make it all work together So the database is just for testing purposes. I tried the debug.print but didn't see any issues
@twalk: I am going to try your method later on in the day as I am crazy busy with some other non programming stuff and will let you know if it worked.

Thx for the support up till now guys

#5
pittpitu

pittpitu

    Newbie

  • Members
  • PipPip
  • 17 posts
@ twalk

tried your code but it didn't work, tried adding @ infront of the value and taking them all away did not work either


[ATTACH=CONFIG]4484[/ATTACH]

this is the error code I am getting




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users