Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP Button click event firing twice

Posted on 2013-06-24
3
Medium Priority
?
591 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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

715 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