locked
Button doesn't work RRS feed

  • Question

  • User-2094959909 posted

    hello everyone,

    i did a test in a test project about adding a user ... i coded the button and it worked it added the user in the database so i took the code and put it inside the button 

    in my main project but it doesn't work when i click on the button it does nothing no error no exception nothing ... here's my code ==>

    <div> <!--ADD USER-->
    <div class="form-group row">
    <label class="col-sm-2 col-form-label">Name</label>
    <div class="col-sm-10">
    <asp:TextBox ID="TextBox1" class="form-control" placeholder="Name" runat="server"></asp:TextBox>
    </div>
    </div>
    <div class="form-group row">
    <label class="col-sm-2 col-form-label">Surname</label>
    <div class="col-sm-10">
    <asp:TextBox ID="TextBox2" class="form-control" placeholder="Surname" runat="server"></asp:TextBox>
    </div>
    </div>
    <div class="form-group row">
    <label class="col-sm-2 col-form-label">Username</label>
    <div class="col-sm-10">
    <asp:TextBox ID="TextBox3" class="form-control" placeholder="Username" runat="server"></asp:TextBox>
    </div>
    </div>
    <div class="form-group row">
    <label class="col-sm-2 col-form-label">Password</label>
    <div class="col-sm-10">
    <asp:TextBox ID="TextBox4" class="form-control" placeholder="Password" runat="server"></asp:TextBox>
    </div>
    </div>
    <div class="form-group row">
    <label class="col-sm-2 col-form-label">Role</label>
    <div class="col-sm-10">
    <asp:TextBox ID="TextBox5" class="form-control" placeholder="Admin / User" runat="server"></asp:TextBox>
    </div>
    </div>
    <div class="form-group row">

    <div class="col-sm-10">
    <asp:Button ID="Button2" runat="server" Text="Ajouter" OnClick="Button2_Click"/>
    <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
    </div>
    </div>
    </div>
    <div><!--SHOW ALL USERS-->

    Button2 does not want to work and add the user i don't know why please help

    thanks in advance

    Sunday, July 22, 2018 10:01 AM

Answers

  • User-1171043462 posted

    Then definitely something wrong on Page. Create a new page and just put Items needed and then check whether Button click is hit.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 22, 2018 3:59 PM

All replies

  • User-1171043462 posted
    1. Share your Button clode
    2.  Have you placed breakpoint and checked whether it calls Click event or not
    3. If already done then make sure there is no Validator on Page which is preventing PostBack. You can also try setting CausesValidation = "False"
    Sunday, July 22, 2018 11:03 AM
  • User-2094959909 posted

    1 / this is the code of the button "Add New User"   (*** I WORK WITH TAB CONTROL ***)

    protected void Button2_Click(object sender, EventArgs e)
    {
    MySqlConnection con = new MySqlConnection(strConnString);
    //con.Open();
    com = new MySqlCommand();
    com.Connection = con;
    com.CommandType = CommandType.Text;
    com.Parameters.Clear();
    // com.Parameters.AddWithValue("@userid",TextBox1.Text);
    /*com.Parameters.AddWithValue("@name", TextBox1.Text);
    com.Parameters.AddWithValue("@surname", TextBox2.Text);
    com.Parameters.AddWithValue("@username", TextBox3.Text);
    com.Parameters.AddWithValue("@password", TextBox4.Text);
    com.Parameters.AddWithValue("@role", DropDownList1.SelectedItem.Text);*/

    com.CommandText = "insert into tblusers (name, surname, username, password, role) values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
    //com.CommandText = "insert into tblusers values ('99','99','99','99','99')";

    //com.ExecuteNonQuery();
    if (con.State == ConnectionState.Closed)
    {
    con.Open();
    com.ExecuteNonQuery();
    con.Close();
    Label1.Text = "Successfully registered";
    //TextBox1.Text = "";
    TextBox1.Text = "";
    TextBox2.Text = "";
    TextBox3.Text = "";
    TextBox4.Text = "";

    }
    }

    HTML code

    <form class="form-inline my-2 my-lg-0">
    <div class="col-sm-10">
    <asp:Button ID="Button2" runat="server" Text="Ajouter" OnClick="Button2_Click"/>
    <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
    </div>
    </form>

    I have add this marked in blue because first of all i had a problem in my logout button until i added it 

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AdminPanel.aspx.cs" EnableEventValidation="false" Inherits="GestionOperationMarocModis.AdminPanel" %>

    Sunday, July 22, 2018 11:29 AM
  • User-1171043462 posted

    So is it working, after adding?

    Sunday, July 22, 2018 12:01 PM
  • User-2094959909 posted

    No sir

    i added CausesValidation = "False" like you said but still when i fill fields of user to add and click the button the fields have been empty so when i checked the database i didn't find the user i added 

    Sunday, July 22, 2018 12:20 PM
  • User-1171043462 posted

    Dont check database.

    First do debugging.

    Add a breakpoint in Click event and see whether it is getting called.

    Sunday, July 22, 2018 12:28 PM
  • User-1171043462 posted

    And remove this IF condition.

    Because of this it may not be inserting in DB

    if (con.State == ConnectionState.Closed)
    {

    Sunday, July 22, 2018 12:30 PM
  • User-2094959909 posted

    i removed the IF condition it doesn't work

    Sunday, July 22, 2018 12:37 PM
  • User-1171043462 posted

    1. Add a break point and check and kindly debug line by line and let me know if executeNonQuery is executed

    Sunday, July 22, 2018 12:44 PM
  • User-2094959909 posted

    i did it but still doesn't work when i went to add a user the fields went empty but nothing happened at all

    Sunday, July 22, 2018 2:21 PM
  • User-1171043462 posted

    Did the breakpoints hit?

    Sunday, July 22, 2018 3:28 PM
  • User-2094959909 posted

    no they don't hit ... i put breakpoints in everyline in a row but the problem is like the first time when i click the fields went empty and nothing changed after  

    Sunday, July 22, 2018 3:57 PM
  • User-1171043462 posted

    Then definitely something wrong on Page. Create a new page and just put Items needed and then check whether Button click is hit.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 22, 2018 3:59 PM
  • User-330142929 posted

    Hi Omar27,

    According to your codes, I have a bit clarity about your codes.  I suggest you could write a hard-coded SQL statement and check if it works Just like commented statement.

    //com.CommandText = "insert into tblusers values ('99','99','99','99','99')";

    Or add some breakpoints to the Code behind and check to see if the code works.

    Feel free to let me know if you have any questions.

    Best Regards

    Abraham

    Monday, July 23, 2018 6:10 AM
  • User446085077 posted

    Do you loss anything in the code?

    Tuesday, July 24, 2018 9:22 AM
  • User446085077 posted

    HTML code

    <form class="form-inline my-2 my-lg-0">
        <div class="col-sm-10">
            <asp:Button ID="Button2" runat="server" Text="Ajouter" OnClick="Button2_Click"/>
            <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
        </div>
    </form>

    Do you loss anything in the code?

    If that, It won't be on work.

    <form class="form-inline my-2 my-lg-0" runat="server">
        <div class="col-sm-10"> 
            <asp:Button ID="Button2" runat="server" Text="Ajouter" OnClick="Button2_Click"/> 
            <asp:Label ID="Label2" runat="server" Text=""></asp:Label> 
        </div> 
    </form>

    Tuesday, July 24, 2018 9:29 AM
  • User-2094959909 posted

    Thank you all for your responses ...

    i had a problem that i was working with single page application that means with <div> so  that the buttons some work and some don't 

    i did what mudassakhan said to create a new page so when that worked i did it for all the other divs i have 

    Tuesday, July 24, 2018 10:58 AM