2016-05-27 2 views
-1
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 [email protected]"; 
     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 [email protected]"; 
      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>

При нажатии на кнопку добавления, данные сноска пункт вставит в свою базу данных, но это вставить в два раза. Он находится в режиме отладки и прыгает повсюду. это шоу «Процесс или нить изменились с последнего шага».

+0

код выглядит нормально ..Не одну вещь, когда вы нажимаете на кнопку, установить флаг и проверить, когда он приходит снова для вставки и если флаг является истинным, тогда перенаправляйте туда, где вы хотите – stylishCoder

+0

, когда im debug показывает, что процесс или поток изменились с последних шагов и снова вскакивают вверх по строке и вниз –

ответ

0

Вы снова отправляете команду вставки, вызывая gvEmpList.DataSource = GetData(cmd); внутри btnAdd_Click. Пожалуйста, проверь это. Объект cmd внутри btnAdd_Click является командой вставки.

+0

i также удаляет gvEmpList> DataSource = Getdata (cmd); и используйте cmd.ExecuteNonQuery(); оба имеют одинаковую проблему в режиме отладки и вскакивают линию линии и downl. он показывает «Процесс или поток изменился с последнего шага». –

0

Удалить следующий код из btnAdd_Click

gvEmpList.DataSource = GetData(cmd); 
gvEmpList.DataBind(); 

И следующий вызов метода

BindData(); 
+0

, когда я использовал данные метода BindData(), не был вставлен –

+0

Вы удалили только две строки, которые я упомянул? – Mairaj

+0

есть. две строки удаляют и отлаживают, но данные не хранятся в базе данных. –

Смежные вопросы