Results 1 to 4 of 4

Thread: Modify database from web page

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    25

    Modify database from web page

    I am using this code to write data from an access database in C: to an asp textbox (txtNam) in a web page. I´m searching it from a textbox named “txtCode” with a “Go” asp Button inside the same page.


    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
    Dim oConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:WEBMTT.mdb")
    oConnection.Open()
    Dim oCommand As New OleDbCommand("SELECT * FROM Equipment WHERE Code='" & txtCode.Text & "'")
    oCommand.Connection = oConnection
    Dim oRedader As OleDbDataReader
    oRedader = oCommand.ExecuteReader()

    While oRedader.Read
    txtNam.Text = oRedader.Item("Name")
    End While
    oConnection.Close()
    End sub


    How can I use another textbox and another button to change that field “Name” in that database from the web page?. I mean, doing the process backwards. The table’s name is “Equipment”. Thanks in advance
    Last edited by principiante; Aug 14th, 2009 at 10:20 AM. Reason: forgot something

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Modify database from web page

    Something similar, but you'd use ExecuteNonQuery.

    Your statement would be an UPDATE statement

    UPDATE tablename SET fieldname='something' WHERE field1 = 1234

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    25

    OK Modify database from web page OK

    That was the answer

    Protected Sub btnGua_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGua.Click
    Dim oConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:WEBMTT.mdb")
    oConnection.Open()
    Dim oCommand As New OleDbCommand("update Equipment set Name='" & txtCode2.Text & "' where Codigo='" & txtCode.Text & "'")
    oCommand.Connection = oConnection
    oCommand.ExecuteNonQuery()
    oConnection.Close()
    End Sub

  4. #4
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Modify database from web page

    Since you are at it you would like to use parameterized queries rather than building the query all by itself. Something like
    Code:
    Dim cmd As New OleDbCommand()
    cmd.CommandText = "update Equipment set Name=@equipmentName where Codigo= @code"
    Dim param As New OleDbParameter()
    param.ParameterName = "@equipmentName"
    param.Value = txtCode2.Text.Trim()
    param.OleDbType = OleDbType.VarChar
    param.Direction = ParameterDirection.Input
    cmd.Parameters.Add(param)
    Do similarly for the second parameter. The advantages are improved query performance (may not matter here) and avoiding SQL injection.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width