Webgrid editing with SQLDataSource

7 replies. Last post: June 3, 2010 3:32 AM by Handy Surya
Tags :
  • New Discussion
  • New Question
  • New Product Feedback
Jason WrightMember

I'm using Webgrid with an SQLdatasource that allows editing.  For datasources that have a single column as the primary key this appears to be working fine.  However if the Sqldatasource has multiple columns in the primary key, I get the message "The datarow you are trying to update no longer exists in the datasource....".

 

I've attached a file showing the table layout in SQL Server Management Studio, and the code showing the webgrid and sqldatasource.  Using the old gridview tag in Visual Studio I used to be able to specify more then one field in the data key (and the editing would work fine), however I'm not sure how to do this in Webgrid.

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DropdownItems.aspx.vb"
    Inherits="WebApplication1.DropdownItems" %>

<%@ Register Assembly="ISNet.WebUI.ISDataSource, Version=1.0.1500.1, Culture=neutral, PublicKeyToken=c4184ef0d326354b"
    Namespace="ISNet.WebUI.DataSource" TagPrefix="ISDataSource" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%@ Register Assembly="ISNet.WebUI.WebGrid" Namespace="ISNet.WebUI.WebGrid" TagPrefix="ISWebGrid" %>
<%@ Register Assembly="ISNet.WebUI.WebDesktop" Namespace="ISNet.WebUI.WebDesktop"
    TagPrefix="ISWebDesktop" %>
<html>
<head id="Head1" runat="server">
    <title>MVMS Administration - Drop Down Items</title>
    <link rel="stylesheet" type="text/css" href="../GlobalMVMS.css" />
</head>
<body id="bdyDropDownItem" runat="server">
    <form id="frmDropDownItem" runat="server">
    <div class="" style="margin: 10px;">
        <asp:Label ID="Label1" runat="server" Text="Select Drop Down Item"></asp:Label>
        <asp:SqlDataSource ID="sqlDropDown" runat="server" ConnectionString="<%$ ConnectionStrings:MVMS %>"
            SelectCommand="SELECT * FROM [ValidValues] WHERE ([TableType] = @TableType)">
            <SelectParameters>
                <asp:Parameter DefaultValue="MASTER" Name="TableType" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:ToolkitScriptManager ID="ScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
        <asp:DropDownList ID="lstValidValue" CssClass="DropDownList" AppendDataBoundItems="true"
            runat="server" AutoPostBack="True" DataSourceID="sqlDropDown" DataTextField="Description"
            DataValueField="TableValue">
        </asp:DropDownList>
        <br />
        <br />
        <asp:CheckBox ID="chFilter" AutoPostBack="true" CssClass="CheckBox" Text="Filter List"
            runat="server" />
        <ISWebGrid:WebGrid ID="grdValidValue" runat="server" Height="245px" UseDefaultStyle="True"
            Width="500px" DataSourceID="sqlGridView" DefaultStyleMode="Win7">
            <LayoutSettings AllowAddNew="No" AllowEdit="Yes" AllowExport="Yes" AllowFilter="Yes"
                AllowSelectColumns="Yes" AllowSorting="Yes" CellClickAction="CellSelect" EditOnClick="True"
                RowChangedAction="FullPagePostback">
            </LayoutSettings>
            <RootTable DataKeyField="TableType">
                <Columns>
                    <ISWebGrid:WebGridColumn Visible="true" AllowMultiLine="True" Caption="TableType"
                        DataMember="TableType" Name="TableType" Width="100px">
                        <ValueList DataSourceID="sqlGridView" DataTextField="TableType" DataValueField="TableType">
                        </ValueList>
                    </ISWebGrid:WebGridColumn>
                    <ISWebGrid:WebGridColumn AllowMultiLine="True" Caption="TableValue" DataMember="TableValue"
                        Name="TableValue" Width="100px">
                        <ValueList DataSourceID="sqlGridView" DataTextField="TableValue" DataValueField="TableValue">
                        </ValueList>
                    </ISWebGrid:WebGridColumn>
                    <ISWebGrid:WebGridColumn Caption="Description" DataMember="Description" Name="Description"
                        Width="100px">
                        <ValueList DataSourceID="sqlGridView" DataTextField="Description" DataValueField="Description">
                        </ValueList>
                    </ISWebGrid:WebGridColumn>
                    <ISWebGrid:WebGridColumn Caption="ExpiryDate" DataMember="ExpiryDate" DataType="System.Date"
                        EditType="CalendarCombo" DataFormatString="dd/MM/yyyy" Name="ExpiryDate" Width="100px">
                    </ISWebGrid:WebGridColumn>
                </Columns>
            </RootTable>
        </ISWebGrid:WebGrid>
        <asp:SqlDataSource ID="sqlGridView" runat="server" ConnectionString="<%$ ConnectionStrings:MVMS %>"
            UpdateCommand="UPDATE [ValidValues] set [Description] = @Description, [ExpiryDate] = convert(datetime,@ExpiryDate,103) where [TableType] = @TableType and [TableValue] = @TableValue"
            SelectCommand="SELECT * FROM [ValidValues] WHERE ([TableType] = @TableType)">
            <SelectParameters>
                <asp:ControlParameter ControlID="lstValidValue" Name="TableType" PropertyName="SelectedValue" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="TableValue" />
                <asp:Parameter Name="Description" />
                <asp:Parameter Name="ExpiryDate" />
                <asp:Parameter Name="TableType" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <br />
        <br />
        <asp:Table ID="Table1" CellPadding="5" runat="server">
            <asp:TableRow>
                <asp:TableCell>
                    <asp:Label ID="lblTableValue" CssClass="label" runat="server" Text="Value"></asp:Label>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:TextBox ID="txtTableValue" CssClass="textbox" runat="server">
                    </asp:TextBox>
                </asp:TableCell>
                <asp:TableCell></asp:TableCell>
                <asp:TableCell>
                    <asp:Label ID="lblDescription" CssClass="label" runat="server" Text="Description">
                    </asp:Label>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:TextBox ID="txtDescription" CssClass="textbox" runat="server">
                    </asp:TextBox>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:Label ID="lblExpiryDate" CssClass="label" runat="server" Text="Expiry Date">
                    </asp:Label>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:TextBox ID="txtExpiryDate" CssClass="textbox" runat="server">
                    </asp:TextBox>
                    <asp:CalendarExtender ID="CalendarExtender1" TargetControlID="txtExpiryDate" Format="dd/MM/yyyy"
                        PopupButtonID="imgDatePicker" runat="server" />
                    <asp:ImageButton ID="imgDatePicker" runat="server" ImageUrl="~/Images/date_picker.gif" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>
                </asp:TableCell><asp:TableCell></asp:TableCell>
                <asp:TableCell>
                    <asp:Button ID="btnAddEntry" runat="server" Text="Add Entry" />
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </div>
    </form>
</body>
</html>

Thanks,

 

Jason.

1 attachment
All times are GMT -5. The time now is 3:36 AM.
Previous Next