locked
Problem with Updating Using Dropdownlist in GridView RRS feed

  • Question

  • User1115497719 posted

    Hi Guys,

    I was used TextBox in the GridView Then I Replace this textBox with Dropdownlist

    and i fetched all the data from the database using DataSource  in Editing Mode

    But The problem is when i choose item from the dropdownlist to update it,it returns to Empty

    (with No values) in the DropDownList??whats the problem?

    Any Help would Be Appreciated....

    Thanks Alot

     Here is the code

    <%

    @ Page Language="C#" MasterPageFile="~/Master.master" AutoEventWireup="true" CodeFile="AddEditCategory.aspx.cs" Inherits="Admin_AddEditCategory" Title="Untitled Page" %>

    <

    asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

     

    &nbsp;<table style="width: 400px">

     

    <tr>

     

    <td colspan="3" rowspan="3" style="text-align: center">

     

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"

     

    DataKeyNames="Category_id" DataSourceID="SqlDataSource1" ForeColor="#333333"

     

    GridLines="None">

     

    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

     

    <Columns>

     

    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />

     

    <asp:BoundField DataField="Category_id" HeaderText="Category_id" InsertVisible="False"

     

    ReadOnly="True" SortExpression="Category_id" />

     

    <asp:TemplateField HeaderText="Name" SortExpression="Name">

     

    <EditItemTemplate>

     

    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"

     

    DataTextField="Name" DataValueField="Category_id" SelectedValue='<%# Bind("Category_id") %>'>

     

    </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:company %>"

     

    SelectCommand="SELECT * FROM [Category]"></asp:SqlDataSource>

     

    </EditItemTemplate>

     

    <ItemTemplate>

     

    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>

     

    </ItemTemplate>

     

    </asp:TemplateField>

     

    </Columns>

     

    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />

     

    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />

     

    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />

     

    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

     

    <AlternatingRowStyle BackColor="White" />

     

    </asp:GridView>

     

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:company %>"

     

    DeleteCommand="DELETE FROM [Category] WHERE [Category_id] = @original_Category_id AND [Name] = @original_Name"

     

    InsertCommand="INSERT INTO [Category] ([Name]) VALUES (@Name)" OldValuesParameterFormatString="original_{0}"

     

    SelectCommand="SELECT * FROM [Category]" UpdateCommand="UPDATE [Category] SET [Name] = @Name WHERE [Category_id] = @original_Category_id ">

     

    <DeleteParameters>

     

    <asp:Parameter Name="original_Category_id" Type="Int32" />

     

    <asp:Parameter Name="original_Name" Type="String" />

     

    </DeleteParameters>

     

    <UpdateParameters>

     

    <asp:Parameter Name="Name" Type="String" />

     

    <asp:Parameter Name="original_Category_id" Type="Int32" />

     

    </UpdateParameters>

     

    <InsertParameters>

     

    <asp:Parameter Name="Name" Type="String" />

     

    </InsertParameters>

     

    </asp:SqlDataSource>

     

    &nbsp;</td>

     

    </tr>

     

    <tr>

     

    </tr>

     

    <tr>

     

    </tr>

     

    </table>

     

     

    <br />

     

     

    <br />

     

    </

    <br />asp:Content>

    Tuesday, February 23, 2010 7:06 PM

Answers

  • User-1635004338 posted

    Hi saedabaid,

    I guess it is caused by "_" in "Category_id" name, if you use OldValuesParameterFormatString as "***_". It seems there is one post like this issue before, I am not quite sure. You can try to use different field name without "_" to see if it works.

    Thanks,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 1, 2010 2:43 AM

All replies

  • User-1355075389 posted

    Use AutoPostBack = "true" for ddl. 

    Tuesday, February 23, 2010 10:05 PM
  • User1115497719 posted

    Unfortunatelly,It didnt Solve the Problem

    I think It is Strange Problem 

    Wednesday, February 24, 2010 4:52 AM
  • User1115497719 posted

    Omg,It is so strange,Because I Built Asimple Example Using TextBox Not DropDownList

    And Still it didnt solve (When I Update It Returns To The Same Previous Item Or Empty TextBox)

    Heres is My Code of My Simple Example

     

    <%

    @ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

    <!

    <

    <

    DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">html xmlns="http://www.w3.org/1999/xhtml" >head runat="server">

     

    </

    <

    <title>Untitled Page</title>head>body>

     

    <form id="form1" runat="server">

     

    <div>

     

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Category_id"

     

    DataSourceID="SqlDataSource1">

     

    <Columns>

     

    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />

     

    <asp:BoundField DataField="Category_id" HeaderText="Category_id" InsertVisible="False"

     

    ReadOnly="True" SortExpression="Category_id" />

     

    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

     

    </Columns>

     

    </asp:GridView>

     

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:company %>"

     

    DeleteCommand="DELETE FROM [Category] WHERE [Category_id] = @original_Category_id AND [Name] = @original_Name"

     

    InsertCommand="INSERT INTO [Category] ([Name]) VALUES (@Name)" OldValuesParameterFormatString="original_{0}"

     

    SelectCommand="SELECT * FROM [Category]" UpdateCommand="UPDATE [Category] SET [Name] = @Name WHERE [Category_id] = @original_Category_id AND [Name] = @original_Name">

     

    <DeleteParameters>

     

    <asp:Parameter Name="original_Category_id" Type="Int32" />

     

    <asp:Parameter Name="original_Name" Type="String" />

     

    </DeleteParameters>

     

    <UpdateParameters>

     

    <asp:Parameter Name="Name" Type="String" />

     

    <asp:Parameter Name="original_Category_id" Type="Int32" />

     

    <asp:Parameter Name="original_Name" Type="String" />

     

    </UpdateParameters>

     

    <InsertParameters>

     

    <asp:Parameter Name="Name" Type="String" />

     

    </InsertParameters>

     

    </asp:SqlDataSource>

     

     

    </div>

     

    </

    </

    </form>body>html>

    Wednesday, February 24, 2010 5:40 AM
  • User-1635004338 posted

    Hi saedabaid,

    I guess it is caused by "_" in "Category_id" name, if you use OldValuesParameterFormatString as "***_". It seems there is one post like this issue before, I am not quite sure. You can try to use different field name without "_" to see if it works.

    Thanks,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 1, 2010 2:43 AM
  • User1115497719 posted

    thank alot for helping me

    Tuesday, March 2, 2010 12:39 PM