[Solved] Update record in database using Session not working in ASP.Net

Last Reply 8 months ago By pandeyism

Posted 8 months ago

database is not updating. I didn't get any error too.

name is id of textbox where i try to edit the text of textbox but i get old one value through this name id

so database is not updating

protected void Button2_Click(object sender, EventArgs e)
{
    Session["name"] = name.Text.ToString();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    con.Open();
    cmd.CommandText = "update category set category=@cat where Id=@id";
    cmd.Parameters.AddWithValue("@cat", Session["name"].ToString());
    cmd.Parameters.AddWithValue("@id", Session["cate"].ToString());
    cmd.ExecuteNonQuery();
    con.Close();
    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert(' Updated successfully')</script>");
    Response.Redirect("category.aspx");
    GridView1.DataBind();
}

 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
    DataSourceID="SqlDataSource1" CssClass="table table-striped table-bordered table-hover">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <a href="?id=<%#Eval("Id") %>">
                    <input type="button" name="button" value="Update" style="color: black; font: bolder" />
                </a>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <a href="?did=<%#Eval("Id") %>">
                    <input type="button" name="button" value="Delete" style="color:black; font:bolder"  class"btn" />
                </a>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Id" Visible="false" HeaderText="Id" InsertVisible="False"
            ReadOnly="True" SortExpression="Id" HeaderStyle-CssClass="visible-lg" ItemStyle-CssClass="visible-lg"
            ItemStyle-Width="100px"></asp:BoundField>
        <asp:BoundField DataField="category" HeaderText="Category Name" SortExpression="category"
            HeaderStyle-CssClass="visible-lg" ItemStyle-CssClass="visible-lg"></asp:BoundField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT * FROM [category]"></asp:SqlDataSource>

 

Posted 8 months ago

Hi JanviDarji,

Refer below code.

CSS

Make visible false boundfield use css.

<style type="text/css">
    .visible-lg
    {
        display: none;
    }
</style>

HTML

<div class="row">
    <div class="col-sm-7" style="margin-left: 250px">
        <div class="main">
            <div class="col-lg-12">
                <div class="row well wll-sm">
                    <div class="col-sm-4">
                        <asp:Label ID="Label2" runat="server" Text="Enter Category Name" Font-Size="Large"
                            Font-Bold="true"></asp:Label>
                    </div>
                    <div class="col-sm-6">
                        <asp:TextBox ID="name" runat="server" Font-Size="Large" CssClass="form-control" required="true"></asp:TextBox>
                    </div>
                    <div class="row">
                        <div class="text-center">
                            <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" BackColor="#6699FF"
                                BorderStyle="Solid" Width="100px" Height="40px" Font-Bold="true" Font-Size="Large" />
                            <asp:Button ID="Button2" runat="server" Text="Update" OnClick="Button2_Click" BackColor="#6699FF"
                                BorderStyle="Solid" Width="100px" Height="40px" Font-Bold="true" Visible="false"
                                Font-Size="Large" />
                        </div>
                    </div>
                </div>
                <br />
                <div id="mainContainer" class="container">
                    <div class="page-container">
                        <div class="container" style="margin-left: 200px">
                            <div class="row">
                                <div class="col-lg-4 ">
                                    <div class="table-responsive">
                                        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerId">
                                            <Columns>
                                                <asp:TemplateField>
                                                    <ItemTemplate>
                                                        <asp:Button Text="Update" runat="server" OnClick="Edit" />
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:BoundField DataField="Id" HeaderText="Id"></asp:BoundField>
                                                <asp:BoundField DataField="category" HeaderText="category" SortExpression="category">
                                                </asp:BoundField>
                                            </Columns>
                                        </asp:GridView>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Code

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        SqlCommand cmd = new SqlCommand("SELECT * FROM CustomerTest", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

protected void Edit(object sender, EventArgs e)
{
    Button btn = (sender) as Button;
    GridViewRow row = btn.NamingContainer as GridViewRow;
    name.Text = row.Cells[2].Text;
    Session["Id"] = row.Cells[1].Text;
    Button2.Visible = true;
}

protected void Button2_Click(object sender, EventArgs e)
{
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    con.Open();
    cmd.CommandText = "update category set category=@cat where Id=@id";
    cmd.Parameters.AddWithValue("@cat", name.Text);
    cmd.Parameters.AddWithValue("@id", Session["Id"].ToString());
    cmd.ExecuteNonQuery();
    con.Close();
}