1

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>
6
  • Any errors you are getting? Commented Jul 27, 2015 at 6:57
  • No I am not getting any error, that's why I am confused. Commented Jul 27, 2015 at 7:00
  • try update static text (to check if update is working like name='test') if its not then its not sql problem only element configuration problem Commented Jul 27, 2015 at 7:18
  • @Buzka91 I tried it with a static text and it's updating, so it's not a sql problem. Commented Jul 27, 2015 at 7:23
  • @killexe according to this site, there are some elemenets called "UpdateParameters" maybe try with it? asp.net/web-forms/overview/data-access/… + you dont need semicolons at end of commands ;) Commented Jul 27, 2015 at 7:25

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

I too had this issue and it worked out that I had the following set to ="False"

EnableSortingAndPagingCallbacks="True"

in

<asp:GridView ID="My_GridView" runat="server EnableSortingAndPagingCallbacks="True">....</asp:GridView>

when I changed it to true (or deleted it), My Update button worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.