Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think it's the way you are inserting the items into the database since you said that it disables the button but doesn't insert the database. So to me, that means the event is firing but something is wrong with the way you are inserting. This is how my buttons look, using your code..</p> <pre><code>protected void Button2_Click(object sender, EventArgs e) { string person = droplist_person.SelectedItem.Text; string item = txt_stuff.Text; string expdate = DateTime.Now.ToString(); string price = txt_price.Text; System.Threading.Thread.Sleep(500); SqlConnection con = new SqlConnection("Data Source=DTPXP-77A;Initial Catalog=practice;Integrated Security=true"); con.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO (expense person, item, expdate, price) VALUES (@person, @item, @expdate, @price)", con); cmd.Parameters.AddWithValue("@person", person); cmd.Parameters.AddWithValue("@item", item); cmd.Parameters.AddWithValue("@expdate", expdate); cmd.Parameters.AddWithValue("@price", price); cmd.ExecuteNonQuery(); InsertHistory(); Response.Redirect("Add.aspx"); Button2.Enabled = false; //you can disable the button one the button click as well. Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "mess();", true); } </code></pre> <p>I would also surround that in a try/catch. Also, as far as I know, the Add is the "old" way of inserting items into the database. AddWithValue is going to be the best way since you are inserting items from drop downs and from text boxes. I think this is the where the problem lies. You can also put the connection inside the connection inside the SqlConnection like above to "clean" things up a bit. As to your "2nd" question, I'm not completely sure what you mean, but I hope this solves the problem to your 1st question.</p> <p>EDIT: Added the disabling of the button since it was described in the title. </p> <p>EDIT 2: Also, remove the UseSubmitBehavior="false" and the OnClientClick. Instead, you can call the message in the behind code.. Look above for example. </p> <p>EDIT 3: From what I understand then, you will want to keep what the code the inserts the values. So keep the OnClientClick = "return Validate();" And then in your button click, disable the button. Then you will want to add if(!PostBack) to the page load, like so.. This will enable the button on the page load if the postback has happened. So what will be happening is that you disable the button, then once it posts back, it will be re-enabled. </p> <pre><code>protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { Button2.Enabled = true; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload