asp.netsqldatasource

SqlDataSource UpdateCommand is not working


I have a GridView and its DataSource is a SqlDataSource.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
    UpdateCommand="UPDATE chemlab_Registerkarte SET [Name] =@Name WHERE [regID] =@regID;" OnUpdating="Update"
    SelectCommandType="Text" SelectCommand='select [regID], [Name] from chemlab_Registerkarte;'>
</asp:SqlDataSource>

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
    DataSourceID="SqlDataSource1" AllowPaging="True" DataKeyNames="regID, Name">
    <Columns>
        <asp:CommandField ShowEditButton="True" CancelText="<%$ Resources:LocalizedText,TextCancel %>"
            EditText="<%$ Resources:LocalizedText,TextEdit %>" UpdateText="<%$ Resources:LocalizedText,TextSave %>"/>
        <asp:BoundField DataField="regID" HeaderText="regID" ReadOnly="True" SortExpression="regID"
            Visible="true" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"/>
    </Columns>
</asp:GridView>

My problem is, that it's not updating, although the select command is working fine.

I've looked at this question: Why isn't my SqlDataSource's UpdateCommand working? but I have DataKeyNames included (as you can see), so this isn't my mistake.

What did I do wrong?

Edit

I tried to get the Data out of my GridView using this code:

String[] values = new String[2];    
values[0] = this.GridView1.Rows[0].Cells[0].Text;
values[1] = this.GridView1.Rows[0].Cells[1].Text;

I get the value when I am not editing it, if I do so I get an empty String. Therefore I cannot even update my database manually.

Edit 2

I have another site where it's working:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
        UpdateCommand="UPDATE chemlab_Anlagen SET Kuerzel =@Kuerzel, Fuellvolumen =@Fuellvolumen, aktiv =@aktiv where aID=@aID;"
        SelectCommandType="Text" SelectCommand='select a.aID, a.InventarNum, m.MO_name, a.Kuerzel, a.Fuellvolumen, a.aktiv from chemlab_Anlagen as a join vw_chemlab_MaintenanceObject as m on a.InventarNum = m.MO_key;'>
    </asp:SqlDataSource>

 <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1" AllowPaging="True" DataKeyNames="InventarNum,aID,MO_name">
            <Columns>
                <asp:CommandField ShowEditButton="True" CancelText="<%$ Resources:LocalizedText,TextCancel %>"
                    EditText="<%$ Resources:LocalizedText,TextEdit %>" UpdateText="<%$ Resources:LocalizedText,TextSave %>" />
                <asp:BoundField DataField="aID" HeaderText="aID" ReadOnly="True" SortExpression="aID"
                    Visible="false" />
                <asp:BoundField DataField="InventarNum" HeaderText="InventarNum" ReadOnly="True"
                    SortExpression="InventarNum" />
                <asp:BoundField DataField="MO_name" HeaderText="MO_name" SortExpression="MO_name"
                    ReadOnly="true" />
                <asp:BoundField DataField="Kuerzel" HeaderText="Kuerzel" SortExpression="Kuerzel" />
                <asp:BoundField DataField="Fuellvolumen" HeaderText="Fuellvolumen" SortExpression="Fuellvolumen" />
                <asp:CheckBoxField DataField="aktiv" HeaderText="aktiv" SortExpression="aktiv" />
            </Columns>
        </asp:GridView>

Solution

  • I've found the solution myself, it was a very stupid mistake.

    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
    DataSourceID="SqlDataSource1" AllowPaging="True" DataKeyNames="regID, Name">
    

    The field DataKeyNames is ONLY for the Primarykey(s). I just removed 'Name' from this attribute (because it isn't a Primarykey) and it worked.

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames(v=vs.110).aspx