I m working with ajax jquery Search Functionality.
when I search the record in textbox then get the record but when I search false record then get the false record? that is the issue
table:
Emploee.aspx
<tr>
<td>
Search Record: <asp:TextBox ID="txtsearch" runat="server"></asp:TextBox>
<input id="btnsearch" type="button" value="Search" onclick="searchrecord()" />
</td>
</tr>
</table>
</div>
<div class="row">
<div>
<table id="tbl" border="1">
<thead>
<tr>
<th>StudentName</th>
<th>StudentAddress</th><br /><br />
<th>Edit</th>
</tr>
</thead>
</table>
</div>
</div>
function searchrecord(studname) {
jQuery.ajax({
url: "WebForm1.aspx/search",
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
data: "{ studname:'" + $("#txtsearch").val() + "' }",
success: function (data) {
debugger
if (data.d == $("#txtsearch").val()) {
debugger
txt = "<tr><td>" + data.d + "</td></tr>";
$("#tbl").append(txt);
alert('you enter right record');
}
else if (data.d != $("#txtsearch").val()) {
alert('you enter wrong record');
}
},
error: function () {
alert('search Error'); //now whatever i search then this run
},
});
}
Emploee.aspx
[WebMethod]
public static string search(string studname,bool flg)
{
string data = "";
SqlConnection con = new SqlConnection(cn);
SqlCommand cmd = new SqlCommand("SELECT studname FROM tblstud WHERE studname LIKE @studname and active=@flagTrueOrFalse", con);
con.Open();
cmd.Parameters.AddWithValue("@studname", "%" + studname + "%");
cmd.Parameters.AddWithValue("@flag", SqlDbType.Bit).Value = flg;
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
studname = reader[0].ToString();
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
data = studname;
return data;
}
how to solve if-else condition ajax logic?
main get data list:
function GetData() {
$.ajax({
url: 'Emploee.aspx/GetData',
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
success: function (data) {
data = JSON.parse(data.d);
$.each(data, function (index, element) {
$("#tbl").append('<td>' + element.studname + '</td>');
$("#tbl").append('<td>' + element.studaddress + '</td>');
$("#tbl").append('<td><input type="button" id="btnupdate" value="Update" onclick="Update(' + element.studid + ', '' + element.studname + '' ,'' + element.studaddress + '')" /></td>');
});
},
error: function (error) {
alert('Not Get Data')
},
});
}
simple, I want the add an if-else condition when the user enters the true record then add the record and the user gets a false record then gets the alert message box?
I want to set below condition in the above ajax:
if(textbox value match then going inside)
{
txt = "<tr><td>" + data.d + "</td></tr>";
$("#tbl").append(txt);
}
else if(textbox value not match then going inside)
{alert("please proper record enter");}
Console log:
preview:
{Message: "Invalid web service call, missing value for parameter: 'flg'.",…}
Message: "Invalid web service call, missing value for parameter: 'flg'."
StackTrace: " at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)
↵ at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)
↵ at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)
↵ at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)"
ExceptionType: "System.InvalidOperationException"
Response
{"Message":"Invalid web service call, missing value for parameter: u0027flgu0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)rn at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)rn at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)rn at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
3
Answers
The condition (data == “#txtsearch”) will most likely never meet.
I am guessing that you wanted to write
To compare the data.
Anyway, is not very efficent, and my want to use the success and error callback properly to achieve wat you want.
rahul I think there is no issue with your ajax request. you can check this by console.log(data) in your success function.
but as your code explains that you are appending the result directly to your
$(“#tbl”) element that means you are appending not deleting previous results. so I suggest you create another element at the end of the table link I am sure this will help.
the first thing that I would like to whenever you try to search the student, please remove Html before appending the response data. so it would avoid confusion if you update content multiple times.
Also, one thing that I notice is that you have taken 2 parameters in the SQL query but you are not passing @flagTrueOrFalse parameter. so make it proper. if still facing the problem then please give us more details so we cal helps you.