Databinding & Update in Webform

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • virlinz

    #1

    Databinding & Update in Webform

    Hello
    I'm a newbie who has a problem with updating the dataset into the
    database. Maybe I missed a few lines of codes. Please shed some light
    for me. The following code is working but not like I wanted.


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    Dim dsn As String = ConfigurationSe ttings.AppSetti ngs("dsn")
    oConn = New OleDbConnection (dsn)
    Dim intKey As Integer = CInt(Request.Qu eryString("id") )
    BindData(intKey )
    End Sub

    Private Sub BindData(ByVal int As Integer)
    strQ = "SELECT Name, Company, WorkAddress, DirectLine, ID
    FROM Ad WHERE ID = " & intKey


    oDaAd = New OleDbDataAdapte r(strQ, oConn)
    oCbAd = New OleDbCommandBui lder(oDaAd)

    oDaAd.InsertCom mand = oCbAd.GetInsert Command
    oDaAd.UpdateCom mand = oCbAd.GetUpdate Command

    Try
    oConn.Open()
    oDsAd.Clear()
    oDaAd.MissingSc hemaAction = MissingSchemaAc tion.AddWithKey
    oDaAd.Fill(oDsA d, "Ad")
    txtCoName.DataB ind()
    txtPIC.DataBind ()
    txtDirectLine.D ataBind()

    Catch ex As Exception
    Label1.Text = ex.ToString()

    Finally
    If oConn.State = ConnectionState .Open Then
    oConn.Close()
    End If
    End Try
    End Sub

    Private Sub btnUpdate_Click (ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles btnUpdate.Click

    Dim dr As DataRow = oDsAd.Tables(0) .Rows.Find(intK ey)
    dr("Name") = txtPIC.Text
    dr("Company") = txtCoName.Text
    dr("DirectLine" ) = txtDirectLine.T ext


    Try
    oConn.Open()
    oDaAd.Update(oD sAd, "Ad")

    Catch ex As Exception
    Label1.Text = ex.ToString()

    Finally
    oConn.Close()

    End Try

    With DataGrid1
    .DataSource = oDsAd
    .DataBind()
    End With

    In the aspx page
    <asp:textbox id=txtPIC style="Z-INDEX: 126; LEFT: 200px; POSITION:
    absolute; TOP: 88px" tabIndex=3 runat="server" Text='<%#
    DataBinder.Eval (oDsAd, "Tables[Ad].DefaultView.[0].Name") %>'
    Width="358px"></asp:textbox>
    <asp:textbox id=txtCoName style="Z-INDEX: 124; LEFT: 200px; POSITION:
    absolute; TOP: 56px" tabIndex=1 runat="server" Text='<%#
    DataBinder.Eval (oDsAd, "Tables[Ad].DefaultView.[0].Company") %>'
    Width="358px"></asp:textbox>
    <asp:textbox id=txtDirectLin e style="Z-INDEX: 133; LEFT: 200px;
    POSITION: absolute; TOP: 424px" tabIndex=11 runat="server" Text='<%#
    DataBinder.Eval (oDsAd, "Tables[Ad].DefaultView.[0].DirectLine") %>'

    After executing the codes, the dataset doesnt have the changes that I
    have made in the textboxes i.e they contains the original values. But
    when I made the following changes, the columns of the table is updated
    with the values they are assigned to.
    dr("Name") = "me"
    dr("Company") = "myCo"
    dr("DirectLine" ) = 1234

    Where did I done wrong? Is it because of the binding? I need to bind it
    because I'm populationg the controls with the existing data from the
    database.

    I thank you in advance for your help.

  • stand__sure

    #2
    Re: Databinding &amp; Update in Webform

    the core problem is that bind doesn't quite work the way that one would
    think -- you are doing too much work, however... the framework will do
    most of the heavy lifting for you.

    take a look at this for a working example (it's a datalist binding
    example)


    Comment

    • Bart Mermuys

      #3
      Re: Databinding &amp; Update in Webform

      Hi,

      "virlinz" <virlinz@gmail. com> wrote in message
      news:1126834304 .657672.41650@o 13g2000cwo.goog legroups.com...[color=blue]
      > Hello
      > I'm a newbie who has a problem with updating the dataset into the
      > database. Maybe I missed a few lines of codes. Please shed some light
      > for me. The following code is working but not like I wanted.[/color]

      I'm not very familar with asp.net and databinding, but don't you need to
      check if it is a postback and if it is, then don't reload the data,
      otherwise you're restoring the values:

      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
      System.EventArg s) Handles MyBase.Load
      Dim dsn As String = ConfigurationSe ttings.AppSetti ngs("dsn")
      oConn = New OleDbConnection (dsn)
      If ( Not IsPostBack ) Then
      Dim intKey As Integer = CInt(Request.Qu eryString("id") )
      BindData(intKey )
      End if
      End Sub


      HTH,
      Greetings


      [color=blue]
      >
      > Private Sub BindData(ByVal int As Integer)
      > strQ = "SELECT Name, Company, WorkAddress, DirectLine, ID
      > FROM Ad WHERE ID = " & intKey
      >
      >
      > oDaAd = New OleDbDataAdapte r(strQ, oConn)
      > oCbAd = New OleDbCommandBui lder(oDaAd)
      >
      > oDaAd.InsertCom mand = oCbAd.GetInsert Command
      > oDaAd.UpdateCom mand = oCbAd.GetUpdate Command
      >
      > Try
      > oConn.Open()
      > oDsAd.Clear()
      > oDaAd.MissingSc hemaAction = MissingSchemaAc tion.AddWithKey
      > oDaAd.Fill(oDsA d, "Ad")
      > txtCoName.DataB ind()
      > txtPIC.DataBind ()
      > txtDirectLine.D ataBind()
      >
      > Catch ex As Exception
      > Label1.Text = ex.ToString()
      >
      > Finally
      > If oConn.State = ConnectionState .Open Then
      > oConn.Close()
      > End If
      > End Try
      > End Sub
      >
      > Private Sub btnUpdate_Click (ByVal sender As System.Object, ByVal e As
      > System.EventArg s) Handles btnUpdate.Click
      >
      > Dim dr As DataRow = oDsAd.Tables(0) .Rows.Find(intK ey)
      > dr("Name") = txtPIC.Text
      > dr("Company") = txtCoName.Text
      > dr("DirectLine" ) = txtDirectLine.T ext
      >
      >
      > Try
      > oConn.Open()
      > oDaAd.Update(oD sAd, "Ad")
      >
      > Catch ex As Exception
      > Label1.Text = ex.ToString()
      >
      > Finally
      > oConn.Close()
      >
      > End Try
      >
      > With DataGrid1
      > .DataSource = oDsAd
      > .DataBind()
      > End With
      >
      > In the aspx page
      > <asp:textbox id=txtPIC style="Z-INDEX: 126; LEFT: 200px; POSITION:
      > absolute; TOP: 88px" tabIndex=3 runat="server" Text='<%#
      > DataBinder.Eval (oDsAd, "Tables[Ad].DefaultView.[0].Name") %>'
      > Width="358px"></asp:textbox>
      > <asp:textbox id=txtCoName style="Z-INDEX: 124; LEFT: 200px; POSITION:
      > absolute; TOP: 56px" tabIndex=1 runat="server" Text='<%#
      > DataBinder.Eval (oDsAd, "Tables[Ad].DefaultView.[0].Company") %>'
      > Width="358px"></asp:textbox>
      > <asp:textbox id=txtDirectLin e style="Z-INDEX: 133; LEFT: 200px;
      > POSITION: absolute; TOP: 424px" tabIndex=11 runat="server" Text='<%#
      > DataBinder.Eval (oDsAd, "Tables[Ad].DefaultView.[0].DirectLine") %>'
      >
      > After executing the codes, the dataset doesnt have the changes that I
      > have made in the textboxes i.e they contains the original values. But
      > when I made the following changes, the columns of the table is updated
      > with the values they are assigned to.
      > dr("Name") = "me"
      > dr("Company") = "myCo"
      > dr("DirectLine" ) = 1234
      >
      > Where did I done wrong? Is it because of the binding? I need to bind it
      > because I'm populationg the controls with the existing data from the
      > database.
      >
      > I thank you in advance for your help.
      >[/color]


      Comment

      • virlinz

        #4
        Re: Databinding &amp; Update in Webform

        Hi stand__sure & Bart Mermuys..
        Thanks for the reply. I never use datalist before. I'll try to
        understand the codes. I've added the postback codes. thanks!

        Comment

        Working...