Jump to content

AJAX in C# for Update a record !


haibec

Recommended Posts

Hi Guys!I have a pages make by C# (update.aspx and Update.aspx.cs) for update a record . My tabel have 4 filed : ID,Username, Password, Email. I want only update user at field Email and all email of users not duplicate. ( When user type in to textbox email if email exist my pages will apper a alert : This Email exist!) please help me!

Link to comment
Share on other sites

Hi Haibec,If you're using SQL, this might help:You can do this using AutoPostBack - when the user types something in the Email textbox and moves out of the textbox the page will automatically post back and if email exists it will show using the 'errorLabel',or,You can call checkEmail() as part of a function called by the final 'Submit' button.Here's the postback-method example:for<asp:TextBox ID="EmailTextBox" runat="server" OnTextChanged="checkEmail" AutoPostBack="true">code behind:public void checkEmail(Object sender, EventArgs e){ string eMail = EmailTextBox.Text; SqlConnection myConnection = new SqlConnection(Connstring); string strSQL = "SELECT * FROM Users WHERE Email = '" + eMail + "'"; SqlCommand mycommand = new SqlCommand(strSQL, myConnection); SqlDataReader myReader = myCommand.ExecuteReader(); if(myReader.Read()) { // Records were found where Email address was the same as type errorLabel.Text = "Sorry, the E-mail your typed already exists in the system!"; } else { // Email typed was not found in the database // Maybe add his entry to the Database here? } myReader.Close(); myConnection.Close();} }

Link to comment
Share on other sites

you can use this Ajax library I wrote and do something like...

function Ajax(sender,parameters){    this.send = function()    {        var http = null;        var method = (parameters.method == null) ? "get" : parameters.method;        var async = (parameters.async == null) ? true : parameters.async;        //Firefox,Opera 8.0+,Safari        try{http = new XMLHttpRequest();}        catch(e)        {            //Internet Explorer            try{http = new ActiveXObject("Msxml2.XMLHTTP");}            catch(e)            {                try{http = new ActiveXObject("Microsoft.XMLHTTP");}                catch(e)                {                    alert("Your browser does not support Ajax!");                    return false;                }            }        }        http.onreadystatechange = function(){_OnComplete(parameters.onComplete,parameters.onFailure,http)};        if(typeof sender == "string")        {            http.open(method,sender,async);            http.send(null);        }        else        {            var action = (sender.action == null) ? parameters.action : sender.action;            var args = this.toQueryString(sender);            http.open(method,action,async);            http.setRequestHeader("Content-type","application/x-www-form-urlencoded");            http.setRequestHeader("Content-length",parameters.length);            if(!document.all){http.setRequestHeader("Connection","close");}            http.send(parameters);        }    }        this.toQueryString = function(object)    {        var str = "";        var count = 0;        if(object.length)        {            for(var i=0;i<object.length;i++)            {                //filter out ASP.Net system elements and Telerik RadControl elements                var sysElem = (    object[i].name == null ||                                object[i].name == "__EVENTTARGET" ||                                object[i].name == "__EVENTARGUMENT" ||                                object[i].name == "__VIEWSTATE" ||                                object[i].name.indexOf("rad") != -1);                if(!sysElem)                {                    if(count == 0)                    {                        str += object[i].name + "=" + object[i].value;                    }                    else                    {                        str += "&" + object[i].name + "=" + object[i].value;                    }                    count++;                }            }        }        else        {            for(key in object)            {                if(count == 0)                {                    str += key + "=" + object[key];                }                else                {                    str += "&" + key + "=" + object[key];                }                count++;            }        }        return str;    }}function _OnComplete(fnComplete,fnFailure,http){    if(http.readyState == 4)    {        if(http.status == 200)        {            if(fnComplete){fnComplete({text:http.responseText});}        }        else        {            if(fnFailure){fnFailure({text:http.responseText,status:http.status});}        }    }}

JavaScript

function checkAvailability(textbox){	new Ajax(	"yourAjaxPage.aspx?email=" + textbox.value,				{	method:"get",					onComplete:function(response)					{						if(response.text == "available")							textbox.style.border = "1px solid green";						else							textbox.style.border = "1px solid red";					}				}			).send();}

ASPX

<input id="txtEmailAddess" runat="server" onchange="checkAvailability(this)"/>

yourAjaxPage.aspx

if(Request.QueryString["email"] != null && Request.QueryString["email"].Length > 0){	//...	//setup database objects	//...		command = new SqlCommand(String.Format("select * from table where emailAddress='{0}'",Request.QueryString["email"]),connection);	connection.Open();	datareader = command.ExecuteReader();	if(datareader.HasRows())		Response.Write("taken");	else		Response.Write("available");	datareader.Close();	connection.Close();}

Link to comment
Share on other sites

Hi Haibec,If you're using SQL, this might help:You can do this using AutoPostBack - when the user types something in the Email textbox and moves out of the textbox the page will automatically post back and if email exists it will show using the 'errorLabel',or,You can call checkEmail() as part of a function called by the final 'Submit' button.Here's the postback-method example:for<asp:TextBox ID="EmailTextBox" runat="server" OnTextChanged="checkEmail" AutoPostBack="true">code behind:public void checkEmail(Object sender, EventArgs e){ string eMail = EmailTextBox.Text; SqlConnection myConnection = new SqlConnection(Connstring); string strSQL = "SELECT * FROM Users WHERE Email = '" + eMail + "'"; SqlCommand mycommand = new SqlCommand(strSQL, myConnection); SqlDataReader myReader = myCommand.ExecuteReader(); if(myReader.Read()) { // Records were found where Email address was the same as type errorLabel.Text = "Sorry, the E-mail your typed already exists in the system!"; } else { // Email typed was not found in the database // Maybe add his entry to the Database here? } myReader.Close(); myConnection.Close();} }
Hi !thank you very much!Now ,I want when duplicate Email("Sorry, the E-mail your typed already exists in the system") . then button SUBMIT not allow user Update. Please help me
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...