I am trying to stop the JavaScript code to run when the data is found in the table (dt.Rows.Count > 0
). At the moment the code is not inserting data in the database (that what I want) but the JavaScript continues running because I am still getting the successful insert message. Thanks!
HTML
<input type="button" id="btnAddConsent" value="Add Consent" onclick="insertData();" />
JavaScript
function insertData() {
var MBID = document.getElementById("txtConsentMBID").value;
var ConsentID = document.getElementById("DropDownListConsent").value;
var ConsentDate = document.getElementById("txtPatientConsentDate").value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "insertConsent.aspx?mb=" + MBID + " &ci= " + ConsentID + "&cd=" + ConsentDate, false);
xmlhttp.send(null);
ConsentID = document.getElementById("DropDownListConsent").value = "";
ConsentDate = document.getElementById("txtPatientConsentDate").value = "";
alert("Consent Added Successfully");
}
C#
using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["Molecular"].ConnectionString))
{
MBID = Request.QueryString["mb"].ToString();
ConsentID = Request.QueryString["ci"].ToString();
ConsentDate = Request.QueryString["cd"].ToString();
con.Open();
using (SqlCommand sc = new SqlCommand(@" select * from ConsentGroup where ConsentID = @ConsentID and MBID=@MBID ", con))
{
sc.Parameters.AddWithValue("@MBID", MBID);
sc.Parameters.AddWithValue("@ConsentID", ConsentID);
//sc.Parameters.AddWithValue("@ConsentDate", ConsentDate);
//sc.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(sc);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
// this message should displayed when count is more that 1
Response.Write("alert('This Patient already has this Concent saved in the Database');");
}
else
{
using (SqlCommand sc1 = new SqlCommand(@"insert into ConsentGroup (MBID, ConsentID, ConsentDate, ConsentWithdraw, ConsentConfirm)
values('" + MBID + "','" + ConsentID + "','" + ConsentDate + "','NO','YES')", con))
{
sc1.ExecuteNonQuery();
}
}
}
con.Close();
}