Solved

ASP Button click event firing twice

Posted on 2013-06-24
3
578 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Accepted Solution

by:
Monica P earned 500 total points
ID: 39273907
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
ID: 39274502
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
ID: 39274666
Great answer.  1. IT WORKED, 2. it was very clear and 3 it was a very fast response
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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