Priyanka Singh

Priyanka Singh

  • 655
  • 1.4k
  • 624.4k

How to stop submitting blank value in the db from textbox???

Sep 26 2019 12:57 AM
 
 
I have a textbox, I have to enter a company name and save to the database. Problem is If there is blank value in the textbox,on click of submit button blank value is stored into the database.
 
Here is my code
 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="GRIDOPERATION.GRIDVIEW_PROJECT.index" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <title>Do or Die</title>  
  6.   <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>  
  7.   <script language="javascript" type="text/javascript">  
  8.          $(function () {  
  9.              $('#Submit').click(function () {  
  10.                  var txt = $('#txtCompany');  
  11.                  if (txt.val() != null && txt.val() != '') {  
  12.                      alert('You Entered Company Name ' + txt.val())  
  13.                  } else {  
  14.                      alert('Please Enter Company Name')  
  15.                  }  
  16.              })  
  17.          });    
  18.          </script>   
  19. </head>  
  20. <body>  
  21.     <form id="form1" runat="server">  
  22.     <div>  
  23.        <label for="Company/ NSE Symbol"> Company Name</label>  
  24.        <asp:TextBox ID="txtCompany" runat="server" Text=""></asp:TextBox>  
  25.        <asp:Button  ID="Submit" runat="server" Text="Submit" onclick="Submit_Click"/>  
  26.        <asp:Label ID="lblmsg" runat="server" Text="lblmsg"></asp:Label>  
  27.     </div>  
  28.     </form>  
  29. </body>  
  30. </html> 
 Here is index.cs code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Web.Configuration;  
  9. using System.Data.SqlClient;  
  10. using System.Web.Script.Services;  
  11. using System.Web.Services;  
  12. using System.Configuration;  
  13. namespace GRIDOPERATION.GRIDVIEW_PROJECT  
  14. {  
  15.     public partial class index : System.Web.UI.Page  
  16.     {     
  17.         string strConnString = WebConfigurationManager.ConnectionStrings["ConnDB"].ConnectionString;  
  18.         protected void Page_Load(object sender, EventArgs e)  
  19.         {  
  20.               
  21.         }  
  22.         protected void Submit_Click(object sender, EventArgs e)  
  23.         {  
  24.             SqlConnection con = new SqlConnection(strConnString);  
  25.             SqlCommand cmd = new SqlCommand("sp_insert", con);  
  26.             cmd.CommandType = CommandType.StoredProcedure;  
  27.             cmd.Parameters.AddWithValue("company_name", txtCompany.Text);  
  28.             con.Open();  
  29.             int k = cmd.ExecuteNonQuery();  
  30.             if (k != -1)  
  31.             {  
  32.                 lblmsg.Text = "Record Inserted Succesfully into the Database";  
  33.                 lblmsg.ForeColor = System.Drawing.Color.CornflowerBlue;  
  34.             }  
  35.             con.Close();  
  36.         }  
  37.        
  38.     }  


 Here I have applied jquery to stop submitting the blank value. Please help me.
 
Thank you so much in advance.
 

Answers (2)