Search code examples
c#asp.netsql-servervisual-studio-2015sql-server-2014

How to update GridView in asp.net C#?


aspx design is:

       <asp:GridView ID="GridView5" class="tablePrice" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" AutoGenerateEditButton="True" DataKeyNames="RoomPrice_ID" OnRowCancelingEdit="GridView5_RowCancelingEdit" OnRowEditing="GridView5_RowEditing" OnRowUpdating="GridView5_RowUpdating">
                <Columns>
                    <asp:BoundField DataField="RoomPrice_ID" HeaderText="RoomPrice_ID" SortExpression="RoomPrice_ID" HeaderStyle-HorizontalAlign="Left" Visible="False" />
                    <asp:BoundField DataField="Season_ID" HeaderText="Season_ID" SortExpression="Season_ID" HeaderStyle-HorizontalAlign="Left" Visible="False" />
                    <asp:BoundField DataField="RoomPrice" HeaderText="RoomPrice" SortExpression="RoomPrice" />
                    <asp:BoundField DataField="RoomType" HeaderText="RoomType" SortExpression="RoomType" />
                </Columns>
         </asp:GridView>
         <br />
         <asp:SqlDataSource ID="SqlDataSource2"  runat="server" ConnectionString="<%$ ConnectionStrings:Hotel_TeyaConnectionString %>" SelectCommand="SELECT [RoomPrice_ID], [Season_ID], [RoomPrice], [RoomType] FROM [ReportRoomPrice]" UpdateCommand="UPDATE RoomPrice SET RoomPrice = @RoomPrice WHERE (RoomPrice_ID = @RoomPrice_ID)">
             <UpdateParameters>
                <asp:Parameter Name="RoomPrice_ID" Type="Int32" />
                <asp:Parameter Name="RoomPrice" Type="String" />
            </UpdateParameters>
         </asp:SqlDataSource>   

code behind

    protected void GridView5_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView5.EditIndex = e.NewEditIndex;
        GridView5.DataBind();
    }

    protected void GridView5_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView5.EditIndex = -1;
        GridView5.DataBind();
    }

    protected void GridView5_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlDataSource2.Update();
    }

I want to update a column RoomPrice(type money in DB) only in GridView from table RoomPrice.I have got another columns for information like RoomType and have got a button Edit.When I click on Edit I have Update and Cancel and then I write on the row to update the Price but when I click Update all is the same and error missing. I don't know why GridView doesn`t update maybe is something in RowUpdating...Thank you


Solution

  • Something like that

        protected void GridView5_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=TEYA-PC;Initial Catalog=Hotel_Teya;Integrated Security=True");
            GridViewRow row = GridView5.Rows[e.RowIndex];
            string RoomPrice_ID = GridView5.DataKeys[e.RowIndex].Values["RoomPrice_ID"].ToString();
            string txt2 = ((TextBox)row.Cells[3].Controls[0]).Text;
            con.Open();
            SqlCommand cmd = new SqlCommand("UPDATE RoomPrice set RoomPrice='" + txt2 + "' where RoomPrice_ID=" + RoomPrice_ID, con);
            cmd.ExecuteNonQuery();
            con.Close();
            GridView5.DataBind();
        }