skip to Main Content

A couple of hours ago I posted a question regarding my code doesn’t work, after seeing answers and solutions and try them, my code still not working!

So I tried debugging tricks and tips to see what’s going on and I noticed that the problem isn’t in my code! It’s because the button is not firing an event.

I’ve tried those solutions and none of them worked:

Also tried to add CausesValidation on the button to false and true,
recoding the button event and delete the old one from the source code and html markup,
tried to add EnableEventValidation to true and false in top of the web form..
Fixed the js linking to be <script></script>
nothing is working </3
Please help me

Here is the HTML markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="sevenCODE.index" %>

<form id="signup_form" runat="server">
        <asp:TextBox ID="email" TextMode="Email" required="true" runat="server"></asp:TextBox>
        <asp:Button ID="btnSubmit" runat="server" Text="Notify me" OnClick="btnSubmit_Click" />
</form>

and here is the code behind:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    //This event is for submitting an email address to receive future e-mails.
    string cs = ConfigurationManager.ConnectionStrings["notifyCS"].ConnectionString;

    //also I tried to not use using()
    using (SqlConnection con = new SqlConnection(cs))
    {

        try
        {
            con.Open();

            //SQL statements
            //check if there is an email registered before
            string checkEmail = "SELECT User_Email FROM tbl_users WHERE User_Email = @User_Email";

            //check subscription if the above email is registered.
            string checkSubscription = "SELECT User_Status FROM tbl_users WHERE User_Email = @User_Email";

            //Insert email as a new one in the db
            string submitEmail = "INSERT INTO tbl_users (User_UID, User_Email, User_Status) VALUES (@User_UID, @User_Email, @User_Status)";

            //Update email info if the email is already registered.
            string updateEmail = "UPDATE tbl_users SET User_UID = @User_UID, User_Status = @User_Status WHERE User_Email = @User_Email";


            //check if the submitted email is in the db
            using (SqlCommand cEmailCMD = new SqlCommand(checkEmail, con))
            {
                cEmailCMD.Parameters.AddWithValue("@User_Email", HttpUtility.HtmlEncode(email.Text));
                SqlDataAdapter cEmailSDA = new SqlDataAdapter
                {
                    SelectCommand = cEmailCMD
                };
                DataSet cEmailDS = new DataSet();
                cEmailSDA.Fill(cEmailDS);

                //If there is no email registered
                if (cEmailDS.Tables[0].Rows.Count == 0)
                {
                    using (SqlCommand sEmailCMD = new SqlCommand(submitEmail, con))
                    {
                        //Generate UID to add so later we can play with it
                        string User_UID = System.Guid.NewGuid().ToString().Replace("-", "").ToUpper();

                        //Insert data as new email
                        sEmailCMD.Parameters.AddWithValue("@User_UID", HttpUtility.HtmlEncode(User_UID));
                        sEmailCMD.Parameters.AddWithValue("@User_Email", HttpUtility.HtmlEncode(email.Text));
                        sEmailCMD.Parameters.AddWithValue("@User_Status", HttpUtility.HtmlEncode("subscribed"));
                        sEmailCMD.ExecuteNonQuery();
                        sEmailCMD.Dispose();
                        con.Close();
                        con.Dispose();
                        email.Text = null;
                        Response.Redirect("index.aspx");
                    }
                }
                //If there is an email registered
                else
                {
                    //first check subscription of the email
                    //if sub != true then we can resubscribe email again with different UID and change status
                    using (SqlCommand cSubCMD = new SqlCommand(checkSubscription, con))
                    {
                        cSubCMD.Parameters.AddWithValue("@User_Email", HttpUtility.HtmlEncode(email.Text));
                        SqlDataAdapter cSubSDA = new SqlDataAdapter
                        {
                            SelectCommand = cSubCMD
                        };
                        DataSet cSubDS = new DataSet();
                        cSubSDA.Fill(cSubDS);
                        string result = cSubDS.Tables[0].Rows[0]["User_Status"].ToString();
                        if (result != "subscribed")
                        {
                            using (SqlCommand uEmailCMD = new SqlCommand(updateEmail, con))
                            {
                                //Generate UID to update the old one
                                string User_UID = System.Guid.NewGuid().ToString().Replace("-", "").ToUpper();

                                //update data
                                uEmailCMD.Parameters.AddWithValue("@User_UID", HttpUtility.HtmlEncode(User_UID));
                                uEmailCMD.Parameters.AddWithValue("@User_Email", HttpUtility.HtmlEncode(email.Text));
                                uEmailCMD.Parameters.AddWithValue("@User_Status", HttpUtility.HtmlEncode("subscribed"));
                                uEmailCMD.ExecuteNonQuery();
                                uEmailCMD.Dispose();
                                con.Close();
                                con.Dispose();
                                email.Text = null;
                                Response.Redirect("index.aspx");
                            }
                        }
                    }
                }
            }
        }
        //if there is any errors please show me (debug)
        //show the user an error message (release)
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        //check if the connection is still open, and if is, please close and dispose it.
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
                con.Dispose();
            }
        }
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    Thank you everyone for trying to answering the question.. I spent around 9 hours trying to see what is the problem, when I was checking all the files I saw a js lib code which was preventing the page from postbacking and sending requests, etc.. when I removed it everything worked prefectly.


  2. You now have to post your full markup for the page.

    We assume that you add a new web page to the project.

    so in project explorer, on the next node RIGHT below project soluiton,
    (you right click and choose add->new item)

    Say like this:

    enter image description here

    And then you get this:

    enter image description here

    Ok, so in above, we created a new web page called WebForm4.

    We now have a blank web page, and it will have this markup:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="CSharpWebApp.WebForm4" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
            </div>
        </form>
    </body>
    </html>
    

    Ok, so now we drag + drop in a button from the tool box – say like this:

    enter image description here

    So, we can hit ctrl-s for good measure.

    Now, we have this:

    Lets drag + drop in a text box

    So now we have this:

    enter image description here

    Now double click on button – we are auto jumped to code behind:

    We can now enter code for button click event. Say this:

    public partial class WebForm4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "Hello World";
        }
    }
    

    now, f5 to run the page.

    And if we click on the button, we get this:

    enter image description here

    So we assume you followed the above steps. And if not, then follow the above steps and post back what occurs.

    So, create a WHOLE new web page – do NOT use any existing page.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search