Question

Anybody know how to get a Templatefield column of text-boxes to feed data typed into them back into a sql database using the SqlDataSource already being used in the gridview the templatefield is in and a buttonfield using an update function? I was just wondering if anybody has any idea how to get this to work because I haven't been able to find any way of doing this.

Please let me know if any of you need any more information to help you solve this dilemma.

I am able to get the information to feed back into the SQL server if i enable edit and change the information in the textbox as well as another column's field in the same row, but it won't update if I only change the information in the textbox whether in edit mode or just clicking the update button.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<center>
<form id="form1" runat="server">
<div>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="Brdcst_Nbr" DataSourceID="SqlDataSource1" 
        EmptyDataText="There are no data records to display." Width="657px">
        <Columns>
            <asp:BoundField DataField="Brdcst_Nbr" HeaderText="Brdcst_Nbr"     ReadOnly="True" 
                SortExpression="Brdcst_Nbr" />
            <asp:BoundField DataField="Item_Nbr" HeaderText="Item_Nbr" 
                SortExpression="Item_Nbr" ReadOnly="True" />
            <asp:CheckBoxField DataField="IsPicked" HeaderText="IsPicked" 
                SortExpression="IsPicked" />
            <asp:TemplateField SortExpression="Row" HeaderText="Row">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Mode="twoway" AutoPostBack="true" CausesValidation="true" Text='<%# Bind("Row") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Mode="twoway" AutoPostBack="true" CausesValidation="true" Text='<%# Bind("Row") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:ButtonField ButtonType="Button" CommandName="Update" Text="Update" />
            <asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" />
        </Columns>
        <EditRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
        <RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConflictDetection="CompareAllValues" 
        ConnectionString="<%$ ConnectionStrings:ORDCConnectionString1 %>" 
        DeleteCommand="DELETE FROM [Test_New_Transpick] WHERE [Brdcst_Nbr] = @original_Brdcst_Nbr AND (([Item_Nbr] = @original_Item_Nbr) OR ([Item_Nbr] IS NULL AND @original_Item_Nbr IS NULL)) AND [IsPicked] = @original_IsPicked AND (([Row] = @original_Row) OR ([Row] IS NULL AND @original_Row IS NULL))" 
        InsertCommand="INSERT INTO [Test_New_Transpick] ([Brdcst_Nbr], [Item_Nbr], [IsPicked], [Row]) VALUES (@Brdcst_Nbr, @Item_Nbr, @IsPicked, @Row)" 
        UpdateCommand="UPDATE [Test_New_Transpick] SET [IsPicked] = @IsPicked, [Row] = @Row WHERE [Brdcst_Nbr] = @original_Brdcst_Nbr" 
        OldValuesParameterFormatString="original_{0}" 
        ProviderName="<%$ ConnectionStrings:ORDCConnectionString1.ProviderName %>" 
        SelectCommand="SELECT * FROM [Test_New_Transpick]" >
        <DeleteParameters>
            <asp:Parameter Name="original_Brdcst_Nbr" Type="String" />
            <asp:Parameter Name="original_Item_Nbr" Type="String" />
            <asp:Parameter Name="original_IsPicked" Type="Boolean" />
            <asp:Parameter Name="original_Row" Type="String" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Brdcst_Nbr" Type="String" />
            <asp:Parameter Name="Item_Nbr" Type="String" />
            <asp:Parameter Name="IsPicked" Type="Boolean" />
            <asp:Parameter Name="Row" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Item_Nbr" Type="String" />
            <asp:Parameter Name="IsPicked" Type="Boolean" />
            <asp:Parameter Name="Row" Type="String" />
            <asp:Parameter Name="original_Brdcst_Nbr" Type="String" />
            <asp:Parameter Name="original_Item_Nbr" Type="String" />
            <asp:Parameter Name="original_IsPicked" Type="Boolean" />
            <asp:Parameter Name="original_Row" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>

</div>
</form>
</center>
</body>
</html>
Was it helpful?

Solution

So, changing the property of the SQLdatabase from CompareAllValues to OverwriteChanges fixed this issue. It is now allowing the changes to be written back to the sql database by just changing the textbox in the row/column that needed to be updated without having to change anything else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top