Kazu
Kazu

Reputation: 137

How to update Value from the textbox user puts in

I have a code like this

<form id="form1" runat="server">

        <asp:Label ID="Label1" runat="server" Visible="False" Text="Please update the price"></asp:Label>
        <br />
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </form>

and C# is

namespace WebApplication6
{
    public partial class WebForm20 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["onealbumid"] != null)
            {
                Label1.Visible = true;

                int onealbumid = Convert.ToInt32(Session["onealbumid"]);

                String Artists = System.Configuration.ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString;
                System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(Artists);

                SqlCommand cmd = new SqlCommand("Select Price from OneAlbum where OneALbumID='" + onealbumid + "'", con);


                con.Open();
                TextBox1.Text = cmd.ExecuteScalar().ToString();
                con.Close();




            }
            else {

                Response.Redirect("~/BENEinsertOneAlbum3.aspx");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            String Artists = System.Configuration.ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString;
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(Artists);

            int onealbumid = Convert.ToInt32(Session["onealbumid"]);

            SqlCommand command = new SqlCommand(@"UPDATE [dbo].[OneAlbum] 
                SET Price=@Price where OneAlbumID =" + onealbumid + ";", con);

            command.Parameters.AddWithValue("@Price", TextBox1.Text);
            con.Open();
            command.ExecuteNonQuery();
            con.Close();
            Response.Redirect("~/BENEinsertOneAlbum3.aspx");
        }
    }
}

There is a page/form, before this page, users select the price which users want to change from the gridview

I want users to edit new "Price" in the textbox1 on this page.

So this is happening right now.

For example, I choose "Price" 22 before this page and when this page opens/loads textbox1 showing 22.

so I change/type to 40 in Texbox1, and click button.

The price is still 22. not 40 and I checked the database, which still did not change, still 22

why is this happening?

Upvotes: 0

Views: 69

Answers (1)

JB King
JB King

Reputation: 11910

The Page_Load will overwrite the data you are entering. Put a !IsPostBack check around that so that when you do click the button the data isn't being put back to what it was when the form loaded.

Also, where is a gridview in this code? There is such a class and thus beware of what terms you use given the code you show.

Upvotes: 1

Related Questions