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
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="GRIDOPERATION.GRIDVIEW_PROJECT.index" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Do or Die</title>
- <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
- <script language="javascript" type="text/javascript">
- $(function () {
- $('#Submit').click(function () {
- var txt = $('#txtCompany');
- if (txt.val() != null && txt.val() != '') {
- alert('You Entered Company Name ' + txt.val())
- } else {
- alert('Please Enter Company Name')
- }
- })
- });
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <label for="Company/ NSE Symbol"> Company Name</label>
- <asp:TextBox ID="txtCompany" runat="server" Text=""></asp:TextBox>
- <asp:Button ID="Submit" runat="server" Text="Submit" onclick="Submit_Click"/>
- <asp:Label ID="lblmsg" runat="server" Text="lblmsg"></asp:Label>
- </div>
- </form>
- </body>
- </html>
Here is index.cs code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Web.Configuration;
- using System.Data.SqlClient;
- using System.Web.Script.Services;
- using System.Web.Services;
- using System.Configuration;
- namespace GRIDOPERATION.GRIDVIEW_PROJECT
- {
- public partial class index : System.Web.UI.Page
- {
- string strConnString = WebConfigurationManager.ConnectionStrings["ConnDB"].ConnectionString;
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
- protected void Submit_Click(object sender, EventArgs e)
- {
- SqlConnection con = new SqlConnection(strConnString);
- SqlCommand cmd = new SqlCommand("sp_insert", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("company_name", txtCompany.Text);
- con.Open();
- int k = cmd.ExecuteNonQuery();
- if (k != -1)
- {
- lblmsg.Text = "Record Inserted Succesfully into the Database";
- lblmsg.ForeColor = System.Drawing.Color.CornflowerBlue;
- }
- con.Close();
- }
-
- }
- }
Here I have applied jquery to stop submitting the blank value. Please help me.
Thank you so much in advance.