Solved

ASP Button click event firing twice

Posted on 2013-06-24
3
557 Views
Last Modified: 2013-06-25
I have a form  and when the ADD JSN button is pressed, the click event is firint twice.  Any help would be greatly appreciated.  Here's the code including the HTML. and the subs that are called:

<tr>
   <td></td>
   <td  align="left">
   <asp:Button ID="btnAdd" runat="server"  
       Text="ADD JSN" style="font-weight: 700" Width="104px" 
       onclick="btnAdd_Click" />
   &nbsp;<asp:Button ID="btnResetJSN" runat="server" CausesValidation="false" 
       style="font-weight: 700" Text="Reset JSN Form" /> 
   &nbsp;<asp:Button ID="btnBack" runat="server" CausesValidation="false" 
       style="font-weight: 700" Text="Back" />
   </td>
</tr>


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            
            bool boolAddJsn = true;

            DataSet dsJSN = JCN.GetLatestJSN(ConfigurationManager.AppSettings["PCSConnString"] + Session["database"].ToString());
            if (dsJSN != null && dsJSN.Tables.Count > 0)
            {
                DataRow row = dsJSN.Tables[0].Rows[0];
                foreach (Object item in row.ItemArray)
                {
                    if (String.Equals(Convert.ToString(item), txtJSN.Text, StringComparison.OrdinalIgnoreCase))
                        boolAddJsn = false;
                }
            }

            if (boolAddJsn)
            {
                JCN.AddJcn(txtJSN.Text, ddEquipment.SelectedItem.Value.ToString(), ddSystem.SelectedItem.Value.ToString(), ddEquipment.SelectedItem.Value.ToString(), Convert.ToInt32(txtEngHrsPort.Text), Convert.ToInt32(txtEngHrsStbd.Text),
                            txtDate.Text, txtCompDate.Text, txtManHrs.Text, txtRemarks.Text, Session["database"].ToString());
                ResetData(false);
                lblResult.Text = "JSN Added successfully.";
            }
            else
            {
                lblResult.Text = "JSN already exists in database.";
            }
        }

        catch (Exception ex)
        {
            throw ex;
        }
    }//end btnAdd_Click



        public static DataSet GetLatestJSN(string ConnectionString)
        {
            SqlConnection Conn = new SqlConnection(ConnectionString);
            string SQL = " select JCN, ENGHRSPORT, ENGHRSSTBD from tblJCN order by JCN DESC";
            SqlDataAdapter Cmd = new SqlDataAdapter(SQL, Conn);
            DataSet DS = new DataSet();
            Cmd.Fill(DS, "tblJCN");
            return DS;
        }
        public static void AddJcn(string JSN, string Equip, string APL, string EQUIPNAME, Int32 EngHrsPort, Int32 EngHrsStbd, string Date, string CompDate, string ManHrs, string Remarks, string strDATABASE)
        {
            string ConnectionString = ConfigurationManager.AppSettings["PCSConnString"] + strDATABASE;
            SqlConnection Conn = new SqlConnection(ConnectionString);
            
            try
            {
                //do trans update
                String SQL = "INSERT INTO [tblJCN] (JCN, SHIP_NAME, APL, EQUIP_NAME, ENGHRSPORT, ENGHRSSTBD, DATE_OF_EST, COMPLETION_DATE, MAN_HR_EXP, REMARKS)" 
                + "VALUES( @JSN, @Equip, @APL, @EQUIPNAME, @EngHrsPort, @EngHrsStbd, @Date, @CompDate, @ManHrs, @Remarks)";

                SqlConnection Conn1 = new SqlConnection(ConnectionString);
                SqlCommand Cmd = new SqlCommand(SQL, Conn1);

                SqlParameter ParamJSN = new SqlParameter("@JSN", SqlDbType.VarChar ,20);
                ParamJSN.Value = JSN;
                SqlParameter ParamEquip = new SqlParameter("@Equip", SqlDbType.VarChar, 20);
                ParamEquip.Value = Equip;
                SqlParameter ParamAPL = new SqlParameter("@APL", SqlDbType.VarChar, 15);
                ParamAPL.Value = APL;
                SqlParameter ParamEQUIPNAME = new SqlParameter("@EQUIPNAME", SqlDbType.VarChar, 20);
                ParamEQUIPNAME.Value = EQUIPNAME;
                SqlParameter ParamEngHrsPort = new SqlParameter("@EngHrsPort", SqlDbType.Int);
                ParamEngHrsPort.Value = EngHrsPort;
                SqlParameter ParamEngHrsStbd = new SqlParameter("@EngHrsStbd", SqlDbType.Int);
                ParamEngHrsStbd.Value = EngHrsStbd;
                SqlParameter ParamDate = new SqlParameter("@Date", SqlDbType.Date);
                ParamDate.Value = Convert.ToDateTime(Date);
                SqlParameter ParamCompDate = new SqlParameter("@CompDate", SqlDbType.Date);
                ParamCompDate.Value = Convert.ToDateTime(CompDate);
                SqlParameter ParamManHrs = new SqlParameter("@ManHrs", SqlDbType.VarChar, 4);
                ParamManHrs.Value = ManHrs;
                SqlParameter ParamRemarks = new SqlParameter("@Remarks", SqlDbType.VarChar, 300);
                ParamRemarks.Value = Remarks;

                //for this method, params must be added in the order that they are in the sql statment!
                Cmd.Parameters.Add(ParamJSN);
                Cmd.Parameters.Add(ParamEquip);
                Cmd.Parameters.Add(ParamAPL);
                Cmd.Parameters.Add(ParamEQUIPNAME);
                Cmd.Parameters.Add(ParamEngHrsPort);
                Cmd.Parameters.Add(ParamEngHrsStbd);
                Cmd.Parameters.Add(ParamDate);
                Cmd.Parameters.Add(ParamCompDate);
                Cmd.Parameters.Add(ParamManHrs);
                Cmd.Parameters.Add(ParamRemarks);


                Conn1.Open();
                Cmd.ExecuteNonQuery();
                Conn1.Close();
            }//end try
            catch (Exception ex)
            {
                throw new ApplicationException("Error Inserting into TblJCN", ex);
            }//end catch
            finally
            {
                Conn.Close();
            }//end finally
        }

Open in new window

0
Comment
Question by:PSCTECH
3 Comments
 
LVL 10

Accepted Solution

by:
Monica P earned 500 total points
Comment Utility
Remove the eventhandler attached to the "btnadd" -->  onclick="btnAdd_Click"

Simply double click the button in design mode., it will take you to the server side code..In that skeleton you make button event code

Change this

<asp:Button ID="btnAdd" runat="server"  
       Text="ADD JSN" style="font-weight: 700" Width="104px" 
       onclick="btnAdd_Click" />

Open in new window


to

<asp:Button ID="btnAdd" runat="server"  
       Text="ADD JSN" style="font-weight: 700" Width="104px" />

Open in new window

0
 
LVL 11

Expert Comment

by:LordWabbit
Comment Utility
By double clicking from the designer you will just put the OnClick back into the markup.  I have tried to recreate your error using your code fragments but have had no luck.  When I click it executes the onclick method once.  Could you post some more of the code.  Also make sure you don't manually add another click event in code somewhere.
0
 

Author Closing Comment

by:PSCTECH
Comment Utility
Great answer.  1. IT WORKED, 2. it was very clear and 3 it was a very fast response
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now