Solved

ASP Button click event firing twice

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 35
looking for an ASP.net report designer 4 41
Amazon S3 Images with .Net 3 30
PowerShell: ForEach-Object Export to CSV 4 32
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
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 …

740 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