Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem updating DetailsView when added DropDownList

Problem updating DetailsView when added DropDownList

Scheduled Pinned Locked Moved ASP.NET
databasehtmlsql-serversysadminhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kbalias
    wrote on last edited by
    #1

    Hi I am struggling with updating data from a web page. On the page I have a DetailsView control which is bound to a table in the SQL Server database. I have enabled Editing. One of the fields in the table is a foreign key to a lookup table (for the Referring Hospital) in the database. I would like to have a DropdownList in the DetailsView so that the user can choose the item. I have changed the field to a TemplateField and while the field (“HospID”) is still a TextBox the update to database works. For example when I change the LastName and click ‘Update’, the update reflects in the database. Here is the markup:

    <asp:TemplateField HeaderText="HospID" SortExpression="HospID">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HospID") %>'></asp:TextBox>
    </EditItemTemplate>
    <InsertItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HospID") %>'></asp:TextBox>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("HospID") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="LastName" HeaderText="LastName"
    SortExpression="LastName" />

    However when I replace the TextBox in the EditTemplate with a DropdownList and try to update, the changes do not reflect in the database. Even if I have just changed the LastName. The markup for the DropdownList is as follows:

    <asp:TemplateField HeaderText="HospID" SortExpression="HospID">
    <EditItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server"
    DataSourceID="SqlDataSource_RefHosp" DataTextField="HospShort"
    DataValueField="HospID">
    </asp:DropDownList>
    </EditItemTemplate>
    <InsertItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HospID") %>'></asp:TextBox>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("HospID") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    Even if I just use a basic DropdownList with no databinding the update does not work.

    <asp:DropDownList ID="DropDownList_RefHosp" runat="server"
    DataTextField="HospShort" DataValueField=

    C 1 Reply Last reply
    0
    • K kbalias

      Hi I am struggling with updating data from a web page. On the page I have a DetailsView control which is bound to a table in the SQL Server database. I have enabled Editing. One of the fields in the table is a foreign key to a lookup table (for the Referring Hospital) in the database. I would like to have a DropdownList in the DetailsView so that the user can choose the item. I have changed the field to a TemplateField and while the field (“HospID”) is still a TextBox the update to database works. For example when I change the LastName and click ‘Update’, the update reflects in the database. Here is the markup:

      <asp:TemplateField HeaderText="HospID" SortExpression="HospID">
      <EditItemTemplate>
      <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HospID") %>'></asp:TextBox>
      </EditItemTemplate>
      <InsertItemTemplate>
      <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HospID") %>'></asp:TextBox>
      </InsertItemTemplate>
      <ItemTemplate>
      <asp:Label ID="Label1" runat="server" Text='<%# Bind("HospID") %>'></asp:Label>
      </ItemTemplate>
      </asp:TemplateField>
      <asp:BoundField DataField="LastName" HeaderText="LastName"
      SortExpression="LastName" />

      However when I replace the TextBox in the EditTemplate with a DropdownList and try to update, the changes do not reflect in the database. Even if I have just changed the LastName. The markup for the DropdownList is as follows:

      <asp:TemplateField HeaderText="HospID" SortExpression="HospID">
      <EditItemTemplate>
      <asp:DropDownList ID="DropDownList1" runat="server"
      DataSourceID="SqlDataSource_RefHosp" DataTextField="HospShort"
      DataValueField="HospID">
      </asp:DropDownList>
      </EditItemTemplate>
      <InsertItemTemplate>
      <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HospID") %>'></asp:TextBox>
      </InsertItemTemplate>
      <ItemTemplate>
      <asp:Label ID="Label1" runat="server" Text='<%# Bind("HospID") %>'></asp:Label>
      </ItemTemplate>
      </asp:TemplateField>

      Even if I just use a basic DropdownList with no databinding the update does not work.

      <asp:DropDownList ID="DropDownList_RefHosp" runat="server"
      DataTextField="HospShort" DataValueField=

      C Offline
      C Offline
      Cnu55555
      wrote on last edited by
      #2

      You are connected the Dropdownlist by automatically by Database connection wizard. When program starts running the values in Database which you specified is loaded Dropdownlist. Again it will not goto database to retive the values for dropdownlist. So you can't get updated values. Instaed of that you will write the code in separate method RetriveData() to retive the data from database and add the SqlDataReader values to Dropdownlist. Call thet method when page load and update button click methods . Take another connection object to for RetriveData() method Open the connection when RetriveData() is called and Close at the method end. U will get updated list evrytime. Hope you can do the coding simple try it..

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups