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" %>
- >
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Do or Dietitle>
- <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 Namelabel>
- <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>
- 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();
- }
- }
- }
Thank you so much in advance.

Jaimin ShethiyaPosted Sep 26, 2019, 1:34 AM
can you please changes the your javascript side
here the code for javascript check the validation.
here the asp:button code for validate the first validation and after the calling server side method.
let me know if you have needed any other help form muside.
Thanks
Sheetal GargPosted Sep 26, 2019, 3:19 AM