By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,888 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,888 IT Pros & Developers. It's quick & easy.

Please help FormView Problem!! editItemTemplate controls retain there values...

P: 8
Hi guys,

These question is for all the experts...

Please help me before my brain explodes

The problem is again with the formview control..

I have a formview and i have to use it that when it displays it shows values from the database and i have a link button edit on the ItemTemplate that changes the mode to edit mode and allows to edit the values which can be edited...

Note that i have not bound it to a datasource control and have programmitically set the FormView1.DataSource property to a datatable in a dataset and all the fields in the ITemTemplate have been manually set to the required fields with <%Eval("")%> and <#bind> in the EditItemTemplate....

so basically i want to have my own edit template that allows for dropdownlists and checkbox controls and then it displays the values using these binding expressions.

.i would use say the SelectedValue property in a dropdownlist to a Bind() and it would show the value of the corressponding column....everything was working fine but then this huge problem appears....

The only thing that needs to be done was to create my own UPDATE query in the FormView1_ItemUpdating event and call it to update the data......

I used the findControl method to find the control say firstname and then use it in the update query as

dim x as TextBox

x=Ctype(FormView1.FindControl("first_name_txtBox") ,TextBox)

and use x because x would be holding the new value which the user enters.....but the control on examining retains its old value which was data bound and not the new value whichanyone enters....so this makes my update query almost useless.......it doesnt show any error and works fine...but since the fields retain their old value everything fails



here is the partial source code of my Codefile

Expand|Select|Wrap|Line Numbers
  1. Imports Microsoft.VisualBasic
  2. Imports System
  3. Imports System.Data
  4. Imports System.Data.OleDb
  5. Imports System.Globalization
  6.  
  7. Partial Class _Default
  8.     Inherits System.Web.UI.Page
  9.     Dim con As OleDbConnection
  10.     Dim cmd As OleDbCommand
  11.     Dim da As OleDbDataAdapter
  12.     Dim param As OleDbParameter
  13.     Dim ds As DataSet
  14.     Dim DTF As DateTimeFormatInfo
  15.     Dim dob As DateTime
  16.  
  17.  
  18.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  19.                 Call displayform()
  20.     End Sub
  21. Sub displayform()
  22.  
  23.         con = New OleDbConnection()
  24.         con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & HttpContext.Current.Server.MapPath("sandesh.mdb")
  25.         cmd = New OleDbCommand("select * from [general] where user_id=@USERID", con)
  26.         con.Open()
  27.         param = New OleDbParameter("USERID", OleDbType.VarChar, 20)
  28.         cmd.Parameters.Add(param)
  29.         param.Value = "pankajsingh5k"
  30.         da = New OleDbDataAdapter(cmd)
  31.         'da.MissingSchemaAction = MissingSchemaAction.AddWithKey
  32.         ds = New DataSet
  33.         'da.FillSchema(ds, SchemaType.Source, "general")
  34.         da.Fill(ds, "General")
  35.         con.Close()
  36.         FormView1.DataSource = ds.Tables(0)
  37.         FormView1.DataBind()
  38.         If FormView1.CurrentMode = FormViewMode.Edit Then
  39.             Call populateYear()
  40.         End If
  41.     End Sub
  42. Protected Sub FormView1_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewModeEventArgs) Handles FormView1.ModeChanging
  43.         If e.NewMode = FormViewMode.Edit Then
  44.  
  45.             FormView1.ChangeMode(e.NewMode)
  46.         ElseIf e.NewMode = FormViewMode.ReadOnly Then
  47.             FormView1.ChangeMode(e.NewMode)
  48.         End If
  49.         Call displayform()
  50.     End Sub
  51. Sub edit()
  52.         Dim dt As Date
  53.                Dim email, fname, lname, city, state, country, profession, birth_place, birth_time, zipcode As TextBox
  54.         Dim v As String
  55.  
  56.         email = CType(FormView1.FindControl("email_id_txtBox"), TextBox)
  57.         fname = CType(FormView1.FindControl("fname_txtBox"), TextBox)
  58.         lname = CType(FormView1.FindControl("lname_txtBox"), TextBox)
  59.         city = CType(FormView1.FindControl("city_txtBox"), TextBox)
  60.         state = CType(FormView1.FindControl("state_txtBox"), TextBox)
  61.         country = CType(FormView1.FindControl("country_txtBox"), TextBox)
  62.         profession = CType(FormView1.FindControl("profession_txtBox"), TextBox)
  63.         birth_place = CType(FormView1.FindControl("birth_place_txtBox"), TextBox)
  64.         birth_time = CType(FormView1.FindControl("birth_time_txtBox"), TextBox)
  65.         zipcode = CType(FormView1.FindControl("zipcode_txtBox"), TextBox)
  66.         Dim Year, month, day, gender, relation, languages, belongs_to As DropDownList
  67.         Year = CType(FormView1.FindControl("dob_year"), DropDownList)
  68.         month = CType(FormView1.FindControl("dob_month"), DropDownList)
  69.         day = CType(FormView1.FindControl("dob_day"), DropDownList)
  70.         gender = CType(FormView1.FindControl("gender_DDList"), DropDownList)
  71.         relation = CType(FormView1.FindControl("relation_DDList"), DropDownList)
  72.         languages = CType(FormView1.FindControl("language_DDList"), DropDownList)
  73.         belongs_to = CType(FormView1.FindControl("belongs_to_DDList"), DropDownList)
  74.         dt = New DateTime(Year.SelectedValue, month.SelectedValue, day.SelectedValue)
  75.  
  76.     Dim updatecmd As New OleDbCommand()
  77.  
  78.  
  79.     dim v as String
  80.     v=fname.Text
  81.         MsgBox(v)
  82.     'shows the original value not the one user has entered
  83.         Dim str As String
  84.  
  85.         'was testing the 3 versions and my main problem is that it retains its old values and 2nd one works
  86.         'str = "update general set first_name='" & fname.Text.Replace("'", "''") & "', last_name ='" & lname.Text.Replace("'", "''") & "', gender ='" & gender.SelectedValue & "', date_of_birth ='" & dt & "' , birth_place ='" & birth_place.Text.Replace("'", "''") & "', birth_time ='" & birth_time.Text.Replace("'", "''") & "', city ='" & city.Text.Replace("'", "''") & "', state ='" & state.Text.Replace("'", "''") & "', zipcode =" & CInt(zipcode.Text) & ", country ='" & country.Text.Replace("'", "''") & "', profession ='" & profession.Text.Replace("'", "''") & "', relation_status ='" & relation.SelectedValue & "', languages ='" & languages.SelectedValue & "', belongs_to ='" & belongs_to.SelectedValue & "', interested_in ='hello' where user_id='pankajsingh5k'"
  87.       working  'str = "update general set first_name='pankajfgfgfbbbb' where user_id='pankajsingh5k'"
  88.         'str = "update [general] set [first_name]='" & fname.Text.Replace("'", "''") & "' where [user_id]='pankajsingh5k'"
  89.  
  90.         MsgBox(str)
  91.  
  92.         updatecmd = New OleDbCommand(str, con)
  93.     con.open()
  94.         updatecmd.ExecuteNonQuery()
  95.         con.Close()
  96.     End Sub
  97.  
  98.     Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  99.         Call edit() calls the edit function
  100.     End Sub
  101. End Class
please help me i have tried everything that comes to my mind....if this works my work will be finished...why this is happening.....if u guys ask i can post the whole source code
Feb 11 '09 #1
Share this Question
Share on Google+
4 Replies


Frinavale
Expert Mod 5K+
P: 9,731
I see your problem, it's on line 37 of the above posted code.
You're calling DataBind in your Page Load event (well in the method that you're calling in your Page Load).

What's happening is that you are binding the old data to your FormView before you can access what the user entered.

Move the DataBind to your PreRender event.
You can set the DataSource for the control, just don't bind the data to it until the very end of your page's life cycle.
Feb 11 '09 #2

P: 8
Thanks a lot....

i did as you asked and it works


Thanx for ur time
Feb 13 '09 #3

P: 1
I'm sort of a noob and had the same problem. Finding this info took me hours of reading msdn and a bunch of other sites.
T.Saxeggen
Dec 8 '09 #4

Frinavale
Expert Mod 5K+
P: 9,731
I hope it helped you!
It's a common mistake that many people make and it's not well documented because the problem has to do with binding and the asp.net page life cycle.

-Frinny
Dec 8 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.