简体   繁体   中英

grid view update

I had grid view which bind sql data source and added field to update data in grid view but it did not do any update and no error appeared and I did not know where the error

SQL Stored Procedures:

ALTER proc [dbo].[GetNewswithType]
As
Begin
Select News.Id,News.Type_Id,
News.Header,News.HText,News.DText,News.Active,News.Add_Date,
NewsType.Type_AR,NewsType.Type_EN
From News
Inner Join NewsType On 
NewsType.Id=News.Type_Id
End  

ALTER Proc [dbo].[UpdateNews]
(
@Id Int
,@Header Nvarchar(50)
,@HText Nvarchar(Max)
,@DText  Nvarchar(Max)
,@Type_Id Int
,@Active Bit

)
AS
BEGIN 
Update News Set 

@Header =Header 
,@HText =HText 
,@DText  =DText 
,@Type_Id=Type_Id
,@Active =Active 

WHERE @Id=Id
END

ASPX Page:

<div class="m10">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
        CellPadding="4" DataKeyNames="Id" DataSourceID="SDSNews" ForeColor="Black" 
        GridLines="Vertical" 
        onselectedindexchanged="GridView1_SelectedIndexChanged" 
        onselectedindexchanging="GridView1_SelectedIndexChanging" 
            onpageindexchanging="GridView1_PageIndexChanging" AllowPaging="True" 
            onrowupdated="GridView1_RowUpdated" 
            onrowdatabound="GridView1_RowDataBound">
        <FooterStyle BackColor="#CCCC99" />
        <RowStyle BackColor="#F7F7DE" />
        <Columns>
            <asp:CommandField HeaderText="Function" ShowEditButton="True" />
            <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" 
                Visible="False" />
            <asp:BoundField DataField="Header" HeaderText="Header" 
                SortExpression="Header" />
            <asp:BoundField DataField="HText" HeaderText="HomeText" 
                SortExpression="HText" />
            <asp:BoundField DataField="DText" HeaderText="DetailsText" 
                SortExpression="DText" />
            <asp:BoundField DataField="Type_Id" HeaderText="TypeNumber" 
                SortExpression="Type_Id" />
            <asp:BoundField DataField="Type_AR" HeaderText="Type_AR" 
                SortExpression="Type_AR" InsertVisible="False" ReadOnly="True" />
            <asp:BoundField DataField="Type_EN" HeaderText="Type_EN" 
                SortExpression="Type_EN" InsertVisible="False" ReadOnly="True" />
            <asp:CheckBoxField DataField="Active" HeaderText="Active" 
                SortExpression="Active" />
            <asp:BoundField DataField="Add_Date" HeaderText="Add_Date" 
                SortExpression="Add_Date" InsertVisible="False" ReadOnly="True" />
        </Columns>
        <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
        <asp:SqlDataSource ID="SDSNews" runat="server" 
            ConnectionString="Data Source=ELARABY-1EACFA3\SQLEXPRESS;Initial Catalog=ElarabyGroup;Integrated Security=True" 
            ProviderName="System.Data.SqlClient" SelectCommand="GetNewswithType" 
            SelectCommandType="StoredProcedure" UpdateCommand="UpdateNews" 
            UpdateCommandType="StoredProcedure">
            <UpdateParameters>
                <asp:ControlParameter ControlID="GridView1" Name="Id" 
                    PropertyName="SelectedValue" Type="Int32" />
                <asp:ControlParameter ControlID="GridView1" Name="Header" 
                    PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="GridView1" Name="HText" 
                    PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="GridView1" Name="DText" 
                    PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="GridView1" Name="Type_Id" 
                    PropertyName="SelectedValue" Type="Int32" />
                <asp:ControlParameter ControlID="GridView1" Name="Active" 
                    PropertyName="SelectedValue" Type="Boolean" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </div>

Add some code to your update procedure to determine if it's being called:

  1. Create a table called Logging (or any name of your choice) that has a column called Message (again, or any name of your choice)
  2. Add a line to your UpdateNews procedure to INSERT INTO [dbo].[Logging] (Message) VALUES ('Procedure Called') . You could log the values of the parameters passed in, if you so wished.

By doing this you can determine if the stored procedure is actually being called or not. The other option is to use Sql Server Profiler to determine this. If you can determine that it is calling the stored procedure, then something is going wrong in there and by logging the parameters you can "hand call" it to find out why.

More likely is that the stored procedure is never being called, how are you telling the grid to persist the changes back out to the database? Looking at this tutorial , I would suggest that your updates are not triggering a "save".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM