Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 12th, 2004, 08:45 PM
Registered User
 
Join Date: Sep 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to update a database with data in textbox

I try to load a student record from database

every field of record will load into a textbox... then i try to modify the content of some textbox, and then try to click on the edit button to update them... but i never successfully modify any recrod.. would any one can help.. the following is my code..

in page load event i load the select record in the page... have edit event to update the record.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim studentID As String = Session("stduentID")
        cn = New OleDbConnection
        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & Server.MapPath("College.mdb")
         Dim MySQL As String = "SELECT * FROM Students WHERE StudentId= " & studentID & " "

        da = New OleDbDataAdapter(MySQL, cn)

        ds = New DataSet
        da.Fill(ds)
        txtStudentID.Text = ds.Tables(0).Rows(0).Item(0)
        txtFirstName.Text = ds.Tables(0).Rows(0).Item(1)
        txtLastName.Text = ds.Tables(0).Rows(0).Item(2)
        txtAddress.Text = ds.Tables(0).Rows(0).Item(3)
        txtCity.Text = ds.Tables(0).Rows(0).Item(4)
        txtProvince.Text = ds.Tables(0).Rows(0).Item(5)
        txtPostalCode.Text = ds.Tables(0).Rows(0).Item(6)
        txtPhoneNumber.Text = ds.Tables(0).Rows(0).Item(7)
        txtEmailName.Text = ds.Tables(0).Rows(0).Item(8)
        txtMajor.Text = ds.Tables(0).Rows(0).Item(9)
        txtStudentNumber.Text = ds.Tables(0).Rows(0).Item(10)
        txtPassword.Text = ds.Tables(0).Rows(0).Item(11)

    End Sub



    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click

        Dim studentID As String = Session("stduentID")
        Dim strsql As String

        cn = New OleDbConnection
        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & Server.MapPath("College.mdb")
        strsql = "Update students set FirstName=?, LastName=?, Address=?, City=?," & _
       "StateOrProvince=?, PostalCode=?, PhoneNumber=?, EmailName=?," & _
       "Major=?, StudentNumber=?, StudentPassword=? Where StudentId= ? "

        Dim cmd As New OleDbCommand("update students set FirstName = ? where StudentId = ?")
           cmd.Connection = cn
        cmd.Connection.Open()
        cmd.Parameters.Add("fn", txtFirstName.Text)
        'cmd.Parameters.Add(New OleDbParameter("LastName", txtLastName.Text))
        'cmd.Parameters.Add(New OleDbParameter("Address", txtAddress.Text))
        'cmd.Parameters.Add(New OleDbParameter("City", txtCity.Text))
        'cmd.Parameters.Add(New OleDbParameter("Province", txtProvince.Text))
        'cmd.Parameters.Add(New OleDbParameter("PostalCode", txtPostalCode.Text))
        'cmd.Parameters.Add(New OleDbParameter("PhoneNumber", txtPhoneNumber.Text))
        'cmd.Parameters.Add(New OleDbParameter("EmailName", txtEmailName.Text))
        'cmd.Parameters.Add(New OleDbParameter("Major", txtMajor.Text))
        'cmd.Parameters.Add(New OleDbParameter("studentNumber", txtStudentNumber.Text))
        'cmd.Parameters.Add(New OleDbParameter("password", txtPassword.Text))
        'cmd.Parameters.Add("id", txtStudentID.Text)

        cmd.ExecuteNonQuery()
        cn.Close()

        Response.Redirect("edit.aspx")


    End Sub


Hui Deng
 
Old October 13th, 2004, 10:09 AM
Authorized User
 
Join Date: Sep 2004
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default

please use if !Page.IsPostBack and the populate the textfields...

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here


 
Old October 13th, 2004, 10:39 PM
Registered User
 
Join Date: Sep 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Pran your idea only work on the page..

i still can't update the database.. since my code can't detect the change in any textbox.. which i store all the fields of a record.

Hui Deng





Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving data in database from textbox jainin_shah9 C# 2005 1 February 21st, 2008 08:34 AM
Cannot update textbox.text John Leo ASP.NET 2.0 Basics 0 March 27th, 2007 08:45 AM
Populate textbox with data retrieved from database hoailing22 ASP.NET 1.0 and 1.1 Basics 5 November 30th, 2006 12:13 AM
Displaying Data in TextBox From Database(Urgent) gadhiav ASP.NET 1.0 and 1.1 Basics 2 June 17th, 2004 12:20 AM
Displaying Data in Textbox from Database(URGENT) gadhiav Classic ASP Databases 1 June 16th, 2004 02:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.