Vikas

Vikas

  • NA
  • 831
  • 195.1k

Newsletter Subscribe in Asp.Net

May 2 2018 5:07 AM
ASPX Code
  1. <div class="single">  
  2. <div class="input-group">  
  3. <asp:TextBox ID="txtsubemail" runat="server" class="form-control" placeholder="Enter your email address…" ValidationGroup="email"></asp:TextBox>  
  4. <span class="input-group-btn">  
  5. <!--<button class="btn btn-theme" type="submit">Subscribe</button> -->  
  6. <asp:LinkButton ID="btnsubscribe" runat="server" class="btn btn-theme" OnClick="btnsubscribe_Click">Subscribe</asp:LinkButton>  
  7. </span>  
  8. <asp:RequiredFieldValidator ID="require1" runat="server" ForeColor="red" ControlToValidate="txtsubemail" ErrorMessage="Please Enter Email" ValidationGroup="email" Display="Dynamic"></asp:RequiredFieldValidator>  
  9. <asp:RegularExpressionValidator ID="RegularExpressionValidator2"  
  10. runat="server" ErrorMessage="Enter Valid Email ID"  
  11. ControlToValidate="txtsubemail" Display="Dynamic"  
  12. CssClass="requiredFieldValidateStyle"  
  13. ForeColor="red" EnabledEventValidation="false"  
  14. ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ValidationGroup="email">  
  15. </asp:RegularExpressionValidator>  
  16. </div>  
  17. </div>  
.CS File
  1. protected void btnsubscribe_Click(object sender, EventArgs e)  
  2. {  
  3. DataTable ddtab = con.GetdataTable("select * from tbl_newsletter where emailid='" + txtsubemail.Text.ToString().Trim() + "'");  
  4. if (ddtab.Rows.Count > 0)  
  5. {  
  6. Page.ClientScript.RegisterStartupScript(typeof(Page), "msg""<script>alert('You have already subscribed')</script>");  
  7. txtsubemail.Text = "";  
  8. }  
  9. else  
  10. {  
  11. int res = 0;  
  12. string query = "insert into tbl_newsletter (emailid,subscribe) values('" + txtsubemail.Text + "','yes')";  
  13. res = con.ExecuteQuery(query);  
  14. if (res > 0)  
  15. {  
  16. Page.ClientScript.RegisterStartupScript(typeof(Page), "msg""<script>alert('You have subscribed successfully.')</script>");  
  17. txtsubemail.Text = "";  
  18. }  
  19. }  
  20. }  
When I enter the email and submit the button then the output is not shown in the database so guys help me this problem.

Answers (1)