I am having following textbox in my aasp.net page. And user enters any username in it and I want ajax to check the availability and show the success or failure message in the label as the user leaves the text box.
UserName<asp:Label ID="usernamelbl" runat="server"></asp:Label>
<asp:TextBox ID ="usernametxt" runat="server" CssClass="twitterStyleTextbox"></asp:TextBox><br />
thats how I am using the ajax
function result() {
var username = "<%=usernametxt%>";
var result = "<%usernamelbl%>";// here i am getting an error on usernamelbl
var jsonText = JSON.stringify({ list: username });
//array = +jsonText;
$.ajax({
url: "staffregistration.aspx/Test", type: "POST", dataType: "json",
contentType: "application/json; charset=utf-8",
data: jsonText,
success: function (data) {
if (data == username) {
result = "username available"
}
else {
result = "username not avilable"
}},
error: function () { alert("its not working"); }
});
return false;
}
and thats how i am interacting with the aspx
public static string Test(string username)
{
string conString = @"user id=ejaz;password=ejaz;persistsecurityinfo=True;server=localhost;database=geospatialdb";
MySqlConnection conn = new MySqlConnection(conString);
MySqlDataReader reader = null;
conn.Open();
MySqlCommand command = new MySqlCommand("select owner_id from owner where owner_username = '" + usernametxt.Text + "';", conn);
reader = command.ExecuteReader();
username = reader[0].ToString();
return username;
}