user3599431
user3599431

Reputation: 43

updating column rows in GridView C#

I have a problem that I couldn't find a solution for. As seen in the picture below, i have a gridView to display list of events. what i want is to compare "eventEndDate" column values with the current day, in order to update the last column rows, "eventStatus". enter image description here

So, I added a while loop to read through the database and get the values of "EventEndDate" as the following:

while (reader.Read())
        {

            result.Add(reader.GetValue(4).ToString());
        }
        reader.Close();

Then, within the load method also I added these codes:

string currentDate = DateTime.Now.ToString("MMMM/d/yyyy");
            //endDate = DateTime.Now.ToString("MMMM/d/yyyy");
            //TextBox1.Text = Session["CountryName"].ToString();
            country = Session["CountryName"].ToString();
            SqlDataSource1.SelectCommand = "SELECT * FROM EventsEnglish WHERE CountryName ='" + country + "'";
            SqlDataSource1.DataBind();

            foreach (var lastDay in result)
            {

                string lastDayDate = Convert.ToDateTime(lastDay).ToString("MMMM/d/yyyy");

                if (DateTime.ParseExact(lastDayDate, "MMMM/d/yyyy", CultureInfo.InvariantCulture) > DateTime.ParseExact(currentDate, "MMMM/d/yyyy", CultureInfo.InvariantCulture))
                {

                    SqlDataSource1.UpdateCommand = "UPDATE EventsEnglish SET EventStatus = Open";
                    SqlDataSource1.DataBind();

                }
                else
                {
                    SqlDataSource1.UpdateCommand = "UPDATE EventsEnglish SET EventStatus = Close";
                    SqlDataSource1.DataBind();
                }

the problem is, I don't get any error messages but the event status doesn't update ... So, what is i'm doing wrong???

Upvotes: 1

Views: 330

Answers (2)

farrukh aziz
farrukh aziz

Reputation: 172

please use ExecuteNonQuery() to update your table.

Upvotes: 2

Alex Castillo
Alex Castillo

Reputation: 37

I am doing the same thing for a project. I use this type of c# grid connecting to sql server and the updates works fine, (you only have to change the boundfield values, the sql command for the update and the parameters for the update.

<body>
    <form id="form1" runat="server">
    <div style="height: 300px">
        <asp:Button CssClass="btn btn-primary" ID="btn_back_to_admin" runat="server" Text="Back to Admin" OnClick="btn_back_to_admin_Click" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" Height="260px" style="margin-bottom: 116px">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:CommandField ShowEditButton="True" />
               <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
               <asp:BoundField DataField="Nombre" HeaderText="Nombre" SortExpression="Nombre" />
               <asp:BoundField DataField="Precio" HeaderText="Precio" SortExpression="Precio" />
               <asp:BoundField DataField="Codigo" HeaderText="Codigo" SortExpression="Codigo" />
               <asp:BoundField DataField="Garantia" HeaderText="Garantia" SortExpression="Garantia" />
               <asp:BoundField DataField="Marca" HeaderText="Marca" SortExpression="Marca" />
               <asp:BoundField DataField="AspectosTecnicos" HeaderText="AspectosTecnicos" SortExpression="AspectosTecnicos" />
               <asp:BoundField DataField="Fotografia1" HeaderText="Fotografia" SortExpression="Fotografia1" />
               <asp:BoundField DataField="Descripcion" HeaderText="Descripcion" SortExpression="Descripcion" />
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ShoppingConnectionString %>" DeleteCommand="DELETE FROM [main_product] WHERE [P_ID] = @P_ID" InsertCommand="INSERT INTO [main_product] ([Product_name], [MRP], [Our_Prize], [Discount], [Brand], [Brand_image], [Type_of_product], [Imagepath1], [Imagepath2], [Imagepath3], [Imagepath4], [Detail], [stock], [new_arrival], [best_seller], [best_offer]) VALUES (@Product_name, @MRP, @Our_Prize, @Discount, @Brand, @Brand_image, @Type_of_product, @Imagepath1, @Imagepath2, @Imagepath3, @Imagepath4, @Detail, @stock, @new_arrival, @best_seller, @best_offer)" SelectCommand="SELECT * FROM [FO_Productos]" UpdateCommand=" Exec FOSP_ActualizarProducto @ID , @Nombre, @Descripcion , @Codigo , @Marca , @Precio , @Garantia , @AspectosTecnicos , @Fotografia1 , @Fotografia1 ">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Product_name" Type="String" />
                <asp:Parameter Name="MRP" Type="Int32" />
                <asp:Parameter Name="Our_Prize" Type="Int32" />
                <asp:Parameter Name="Discount" Type="Int32" />
                <asp:Parameter Name="Brand" Type="String" />
                <asp:Parameter Name="Brand_image" Type="String" />
                <asp:Parameter Name="Type_of_product" Type="String" />
                <asp:Parameter Name="Imagepath1" Type="String" />
                <asp:Parameter Name="Imagepath2" Type="String" />
                <asp:Parameter Name="Imagepath3" Type="String" />
                <asp:Parameter Name="Imagepath4" Type="String" />
                <asp:Parameter Name="Detail" Type="String" />
                <asp:Parameter Name="stock" Type="Int32" />
                <asp:Parameter Name="new_arrival" Type="Int32" />
                <asp:Parameter Name="best_seller" Type="Int32" />
                <asp:Parameter Name="best_offer" Type="Int32" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Nombre" Type="String" />
                <asp:Parameter Name="Precio" Type="Int32" />
                <asp:Parameter Name="Codigo" Type="Int32" />
                <asp:Parameter Name="Garantia" Type="Int32" />
                <asp:Parameter Name="Marca" Type="String" />
                <asp:Parameter Name="AspectosTecnicos" Type="String" />
                <asp:Parameter Name="Fotografia1" Type="String" />
                <asp:Parameter Name="Descripcion" Type="String" />
                <asp:Parameter Name="Imagepath2" Type="String" />
                <asp:Parameter Name="Imagepath3" Type="String" />
                <asp:Parameter Name="Imagepath4" Type="String" />
                <asp:Parameter Name="Detail" Type="String" />
                <asp:Parameter Name="stock" Type="Int32" />
                <asp:Parameter Name="new_arrival" Type="Int32" />
                <asp:Parameter Name="best_seller" Type="Int32" />
                <asp:Parameter Name="best_offer" Type="Int32" />
                <asp:Parameter Name="P_ID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:Button ID="Button1"  CssClass="btn btn-primary" runat="server" Text="Back To admin Console" OnClick="Button1_Click" />

    </div>
    </form>
</body>

Upvotes: 1

Related Questions