Solved

ASP Button click event firing twice

Posted on 2013-06-24
3
561 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Word Directory is not in the drop down list 4 26
Runtime Error 2 29
In WPF / C# binding a local database in code behind 1 24
Server Error 11 48
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

26 Experts available now in Live!

Get 1:1 Help Now