在添加按钮上单击数据插入两次

时间:2016-05-27 08:53:29

标签: c# asp.net

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyManageConnectionString"].ToString());
    string InstituteId = HttpContext.Current.Request.Cookies["InstituteId"].Value;
    protected void Page_Load(object sender, EventArgs e)
    {
        ((Label)Master.FindControl("lblPageName")).Text = "Employee List";
        ((Label)Master.FindControl("lblPageName")).ForeColor = System.Drawing.Color.Black;
        ((Label)Master.FindControl("lblPageName1")).Text = " Employee List";
        if (Request.Cookies["InstituteId"] == null)
        {
            string OriginalUrl = HttpContext.Current.Request.RawUrl;
            string LoginPageUrl = "INlogin.aspx";
            HttpContext.Current.Response.Redirect(String.Format("{0}?R={1}", LoginPageUrl, OriginalUrl));
        }
        if (!IsPostBack)
        {
            BindData();
        }
    }


    private void BindData()
    {
        string strQuery = "select * from tblEmployee where InstituteId=@InstituteId";
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.AddWithValue("@InstituteId", InstituteId);
        gvEmpList.DataSource = GetData(cmd);
        gvEmpList.DataBind();
    }

    private DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        //SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        con.Open();
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        return dt;
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {

        string ActivationCode = Guid.NewGuid().ToString();
        string txtEmployeeName = ((TextBox)gvEmpList.FooterRow.FindControl("txtEmployeeName")).Text;
        string txtEmployeeAddress = ((TextBox)gvEmpList.FooterRow.FindControl("txtEmployeeAddress")).Text;
        string txtEmployeeEmailId = ((TextBox)gvEmpList.FooterRow.FindControl("txtEmployeeEmailId")).Text;
        string txtMobile = ((TextBox)gvEmpList.FooterRow.FindControl("txtMobile")).Text;
        string txtBirthDate = ((TextBox)gvEmpList.FooterRow.FindControl("txtBirthDate")).Text;
        string txtdateOfJoining = ((TextBox)gvEmpList.FooterRow.FindControl("txtdateOfJoining")).Text;
        string txtExperiance = ((TextBox)gvEmpList.FooterRow.FindControl("txtExperiance")).Text;
        string txtLastSchoolDetails = ((TextBox)gvEmpList.FooterRow.FindControl("txtLastSchoolDetails")).Text;
        string txtSalaryDetails = ((TextBox)gvEmpList.FooterRow.FindControl("txtSalaryDetails")).Text;
        bool chkmerriageStatus = ((CheckBox)gvEmpList.FooterRow.FindControl("chkmerriageStatus")).Checked;
        string txtNationality = ((TextBox)gvEmpList.FooterRow.FindControl("txtNationality")).Text;

        try
        {

            //SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            //con.Open();
            //cmd.Connection = con;
            cmd.CommandText = "INSERT INTO tblEmployee (InstituteId, EmployeeName, EmployeeAddress, EmployeeEmailId, Mobile, BirthDate, dateOfJoining, Experiance, LastSchoolDetails, SalaryDetails, merriageStatus, Nationality, ActivationCode, ActivationStatus, Password) VALUES (@InstituteId, @EmployeeName, @EmployeeAddress, @EmployeeEmailId, @Mobile, @BirthDate, @dateOfJoining, @Experiance, @LastSchoolDetails, @SalaryDetails, @merriageStatus, @Nationality, @ActivationCode, @ActivationStatus, @Password);" + "select * from tblEmployee where InstituteId=@InstituteId";
            cmd.Parameters.AddWithValue("@InstituteId", InstituteId);
            cmd.Parameters.AddWithValue("@EmployeeName", txtEmployeeName);
            cmd.Parameters.AddWithValue("@EmployeeAddress", txtEmployeeAddress);
            cmd.Parameters.AddWithValue("@EmployeeEmailId", txtEmployeeEmailId);
            cmd.Parameters.AddWithValue("@Mobile", txtMobile);
            cmd.Parameters.AddWithValue("@BirthDate", txtBirthDate);
            cmd.Parameters.AddWithValue("@dateOfJoining", txtdateOfJoining);
            cmd.Parameters.AddWithValue("@Experiance", txtExperiance);
            cmd.Parameters.AddWithValue("@LastSchoolDetails", txtLastSchoolDetails);
            cmd.Parameters.AddWithValue("@SalaryDetails", txtSalaryDetails);
            cmd.Parameters.AddWithValue("@merriageStatus", chkmerriageStatus);
            cmd.Parameters.AddWithValue("@Nationality", txtNationality);
            cmd.Parameters.AddWithValue("@ActivationCode", ActivationCode);
            cmd.Parameters.AddWithValue("@ActivationStatus", false);
            string GTPassword = System.Web.Security.Membership.GeneratePassword(8, 3);
            cmd.Parameters.AddWithValue("@Password", GTPassword);
            //int result = cmd.ExecuteNonQuery();
            //con.Close();
            //if (result == 1)
            //{
            //    sendActivation(ActivationCode, txtEmployeeName, txtEmployeeEmailId);
            //    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Registration Successfull')", true);

            //}
            //else {
            //    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Registration Fail')", true);
            //}
            gvEmpList.DataSource = GetData(cmd);
            gvEmpList.DataBind();

            //BindData();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message, ex);
        }

    }

单击添加按钮时btnAdd_click插入数据两次。此外,我在btnadd_click代码中没有收到任何异常错误。

<asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div id="dvGrid" style="overflow-x: scroll; Overflow: scroll;">
            
            <asp:UpdatePanel ID="upEmployeeList" runat="server">
                <ContentTemplate>

                    <asp:GridView Width="100%" ID="gvEmpList" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeId" CssClass="gvmydatagrid" PagerStyle-CssClass="gvpager" HeaderStyle-CssClass="gvheader" RowStyle-CssClass="gvrows" ShowFooter="True" OnPageIndexChanging="gvEmpList_PageIndexChanging" OnRowEditing="gvEmpList_RowEditing" OnRowUpdating="gvEmpList_RowUpdating" OnRowCancelingEdit="gvEmpList_RowCancelingEdit" AllowPaging="True" PageSize="25">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:LinkButton ID="lnkRemove" runat="server"
                                        CommandArgument='<%# Eval("EmployeeId")%>'
                                        OnClientClick="return confirm('Do you want to delete?')"
                                        Text="Delete" OnClick="lnkRemove_Click"></asp:LinkButton>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:CommandField ShowEditButton="True" />
                            <asp:TemplateField HeaderText="EmployeeId" InsertVisible="False" SortExpression="EmployeeId">
                                <EditItemTemplate>
                                    <asp:Label ID="lblEmployeeId" runat="server" Text='<%# Eval("EmployeeId") %>'></asp:Label>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblEmployeeId" runat="server" Text='<%# Bind("EmployeeId") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        <%--    <asp:TemplateField HeaderText="InstituteId" SortExpression="InstituteId">
                                <EditItemTemplate>
                                    <asp:Label ID="txtInstituteId" runat="server" Text='<%# Bind("InstituteId") %>'></asp:Label>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblInstituteId" runat="server" Text='<%# Bind("InstituteId") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>--%>
                            <asp:TemplateField HeaderText="EmployeeName" SortExpression="EmployeeName">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtEmployeeName" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtEmployeeName" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblEmployeeName" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="EmployeeAddress" SortExpression="EmployeeAddress">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtEmployeeAddress" runat="server" Text='<%# Bind("EmployeeAddress") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtEmployeeAddress" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblEmployeeAddress" runat="server" Text='<%# Bind("EmployeeAddress") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="EmployeeEmailId" SortExpression="EmployeeEmailId">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtEmployeeEmailId" runat="server" Text='<%# Bind("EmployeeEmailId") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtEmployeeEmailId" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblEmployeeEmailId" runat="server" Text='<%# Bind("EmployeeEmailId") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Mobile" SortExpression="Mobile">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtMobile" runat="server" Text='<%# Bind("Mobile") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtMobile" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblMobile" runat="server" Text='<%# Bind("Mobile") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="BirthDate" SortExpression="BirthDate">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtBirthDate" runat="server" Text='<%# Bind("BirthDate") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtBirthDate" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblBirthDate" runat="server" Text='<%# Bind("BirthDate") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="dateOfJoining" SortExpression="dateOfJoining">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtdateOfJoining" runat="server" Text='<%# Bind("dateOfJoining") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtdateOfJoining" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lbldateOfJoining" runat="server" Text='<%# Bind("dateOfJoining") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Experiance" SortExpression="Experiance">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtExperiance" runat="server" Text='<%# Bind("Experiance") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtExperiance" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblExperiance" runat="server" Text='<%# Bind("Experiance") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="LastSchoolDetails" SortExpression="LastSchoolDetails">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtLastSchoolDetails" runat="server" Text='<%# Bind("LastSchoolDetails") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtLastSchoolDetails" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblLastSchoolDetails" runat="server" Text='<%# Bind("LastSchoolDetails") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="SalaryDetails" SortExpression="SalaryDetails">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtSalaryDetails" runat="server" Text='<%# Bind("SalaryDetails") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtSalaryDetails" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblSalaryDetails" runat="server" Text='<%# Bind("SalaryDetails") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="merriageStatus" SortExpression="merriageStatus">
                                <EditItemTemplate>
                                    <asp:CheckBox ID="chkmerriageStatus" runat="server" Checked='<%# Bind("merriageStatus") %>' />
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:CheckBox ID="chkmerriageStatus" runat="server"></asp:CheckBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkmerriageStatus" runat="server" Checked='<%# Bind("merriageStatus") %>' Enabled="false" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Nationality" SortExpression="Nationality">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtNationality" runat="server" Text='<%# Bind("Nationality") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtNationality" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblNationality" runat="server" Text='<%# Bind("Nationality") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Password" SortExpression="Password">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtPassword" runat="server" Text='<%# Bind("Password") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <%--<FooterTemplate>
                                    <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
                                </FooterTemplate>--%>
                                <ItemTemplate>
                                    <asp:Label ID="lblPassword" runat="server" Text='<%# Bind("Password") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        
                        </Columns>
                        <HeaderStyle CssClass="gvheader" />
                        <PagerStyle CssClass="gvpager" />
                        <RowStyle CssClass="gvrows" />
                    </asp:GridView>

                                        
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="gvEmpList" />
                </Triggers>
            </asp:UpdatePanel>
        </div>

当我点击添加按钮时,页脚项数据将插入到我的数据库中但插入两次。它处于调试模式并随处可见。 它显示“自上一步以来进程或线程已更改。”

2 个答案:

答案 0 :(得分:0)

您正在gvEmpList.DataSource = GetData(cmd);内调用btnAdd_Click再次发送插入命令。请检查一下。 cmd内的btnAdd_Click对象是插入命令。

答案 1 :(得分:0)

btnAdd_Click

中删除以下代码
gvEmpList.DataSource = GetData(cmd);
gvEmpList.DataBind();

并调用以下方法

BindData();