ASP - check if a record exists from aspx form text box RRS feed

  • Question

  • User389883181 posted


    Let say I have a database table (myTable) like this:

    and I have a form like this

    <form id="form1" runat="server">
    	Box 1
    	<asp:TextBox id="txt1" runat="server"></asp:TextBox>
    	<br />
    	Box 2
    	<asp:TextBox id="txt2" runat="server"></asp:TextBox>
    	<span id="span1"></span>

    How can I check if the inputted value into txt1 exists in MyTable - field1
    and if it does to add the value of field2 (of the same record in my table) into txt2
    if it doesn't exist span1 should display and error message (e.g. value not found)

    This is very simple with a dropdown and some javascript but I don't want to display the records in a dropdown.
    I need to check if the user kows the value of one of the records (field1) and has input it into the form.

    Thank you

    This would be much simpler to explain with pictures but I can't work out how to add pictures to this post

    Sunday, October 21, 2018 11:10 AM

All replies

  • User1120430333 posted

    OnBlur() on the textbox that calls a JavaScript function that calls a function on the codebehind C#. The onblur() function would show an alert message based on a return value from the C# function.



    Sunday, October 21, 2018 6:33 PM
  • User839733648 posted

    Hi reflo2,

    According to your description, I suggest that the key point of your requirement is to create a arraylist to return the datas from database and use ajax to check the datas.

    I've made a sample on my side, and for more details, you could refer to the code below.


    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#Button1").click(function () {
                    var txtValue1 = $("#TextBox1").val();
                        url: "CheckTextBox.aspx/GetInfo",
                        type: "POST",
                        data: "{}",
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            var jsonObj = JSON.parse(data.d);
                            for (var i = 0; i < jsonObj.length; i++) {
                                if (jsonObj[i].Field1 == txtValue1) {
                                    return false;
                                else {
                                    $("#infoname").text("Field1 not exist");
        <form id="form1" runat="server">
                Field1:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
                Field2:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /><br />
                <span id="infoname"></span><br /><br />
                <input type="button" id="Button1" value="Test"/>


    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web.Services;
    using System.Web.Script.Serialization;
            public static string GetInfo()
                List<FieldInfo> fieldlist = new List<FieldInfo>();
                string constr = ConfigurationManager.ConnectionStrings["EmployeeManagementConnectionString"].ConnectionString;
                using(SqlConnection con=new SqlConnection(constr))
                    string query = "SELECT * FROM tb_txtField";
                    SqlCommand cmd = new SqlCommand(query, con);
                    SqlDataReader sdr = cmd.ExecuteReader();
                    while (sdr.Read())
                        FieldInfo fieldInfo = new FieldInfo();
                        fieldInfo.ID = Convert.ToInt32(sdr["ID"]);
                        fieldInfo.Field1 = sdr["Field1"].ToString();
                        fieldInfo.Field2 = sdr["Field2"].ToString();
                JavaScriptSerializer js = new JavaScriptSerializer();
                return js.Serialize(fieldlist);
            public class FieldInfo
                public int ID { get; set; }
                public string Field1 { get; set; }
                public string Field2 { get; set; }


    Best Regards,


    Tuesday, October 23, 2018 2:31 AM