$("#colnm").change(function () {
$.ajax({
url: '@Url.Action("INDEX", "HOME")',
type: "POST",
context: document.body,
data: { colnm: $(this).val() },
success: function (data) {
if (data) {
}
else {
alert("this colnm already exist in system");
$("#colnm").val("");
}
},
error: function (err) {
console.log(err);
}
});
});
public JsonResult CheckValidColName(string colnm)
{
bool status = false;
try
{
string SQL = "Usp_CheckValidColName '" + colnm + "'";
DataTable DT = GetDataQuery(SQL);
status = Convert.ToBoolean(DT.Rows[0]["Status"].ToString());
}
catch (Exception)
{
throw;
}
return Json(status, JsonRequestBehavior.AllowGet);
}
string ConString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
public DataTable GetDataQuery(string SQL)
{
DataTable DT = new DataTable();
var conn = new SqlConnection(ConString);
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = SQL;
cmd.CommandTimeout = 6000;
SqlDataAdapter da = new SqlDataAdapter(cmd);
conn.Close();
da.Fill(DT);
da.Dispose();
conn.Close();
}
return DT;
}
CREATE PROC USP_CHECKVALIDCOLNAME
@COLNM VARCHAR(50)
AS
BEGIN
DECLARE @CNT INT
SELECT @CNT= COUNT(*) FROM TBL_NM WHERE COLNM=@COLNM
SELECT CAST(CASE WHEN @CNT >0 THEN 0 ELSE 1 END AS BIT) AS STATUS
END
OR:
CREATE PROC USP_CHECKVALIDCOLNAME
@COLNM VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
IF NOT EXISTS(SELECT COLNM FROM TBL_NM
WHERE COLNM = @COLNM)
BEGIN
SELECT 1 AS STATUS
END
ELSE
BEGIN
SELECT 0 AS STATUS
END
END
0 Comments
if you have any doubts , please let me know