-
Null value getting passed back
) 11 Jul 06 23:14
I'm using this page as another step towards my goal of seeing if a
number already exists in a database, so I do a SQL count. The tutorial,
http://www.kynou.com/GetTutorial.aspx?TutorialID=51 , where you type in
the state inital and onblur(), then second box fills with the full
name. This tutorial works, but I'm trying to do a SQL count on the text entered, then notifiy the
user if that number exists or not in a db table. Whatever I type in, the div tag says 'null'.
I can get to where I successfully show the parameter passed to the Ajax Method, stateInitial, and the SQL string via the Return value for the div tag, but that's it. All else gives null.
I have the following code behind for my WebForm1.aspx v1.1 page:
Start code behind:
*****
*****
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
AjaxPro.Utility.RegisterTypeForAjax(GetType(WebForm1))
End Sub
<AjaxPro.AjaxMethod()> Public Function GetStateName(ByVal stateInitial As String) As String
'Return stateInitial
Dim strSQL As String = "Select count(*) from WBS_Table where [WBS Number] = '" & stateInitial & "'"
Dim cmd As New OleDbCommand(strSQL, sCon1)
Try
sCon1.Open()
Dim intCnt As Integer = cmd.ExecuteScalar()
sCon1.Close()
If intCnt = 0 Then
Return "This is unique"
Else
Return "Please enter another Number!"
End If
Catch ex As Exception
Return ex.ToString()
Finally
sCon1.Close()
End Try
'Select Case stateInitial.ToUpper()
' Case "CA"
' Return "California"
' Case "NY"
' Return "New York"
' Case "IL"
' Return "Illinois"
' Case Else
' Return "What tha'!"
'End Select
End Function
Here's the html:
*****
*****
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script language="javascript">
function getStateName(obj)
{
Ajax.WebForm1.GetStateName(obj.value,CallbackFunc);
}
//function CallbackFunc(res)
//{
//document.getElementById('txtStateName').value = res.value;
//}
function CallbackFunc(res){
if (res.error != null)
{
alert(res.error.Message);
}
// else
//{
// alert(res.value);
//}
//document.getElementById('txtStateName').value = res
txtStateName.innerHTML = res.value;
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<div>State Initial:</div>
<asp:textbox id="txtStateInitial" onblur="getStateName(this);" runat="server"></asp:textbox>
<div id="txtStateName"></div>
</form>
</body>
</HTML>
-
All,
I figured out the code (right after if(xmlHttp.status == 200)). My earlier problem was a null reference to the div tage 'show' only in IE, but it worked in FireFox.
The app now works like:
If the number isn't unique via a database SQL count(*), it'll tell you so, but then if you enter another number (which is in fact distinct), then the user gets the message right underneath the previous {div id=show} message...it's not replaced....just appended! I'd like to have it replace the value, rather than stick the new message underneath.
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="AjaxTesting.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<!-- Start script -->
<script language="javascript">
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
var xmlHttp;
if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
xmlHttp = false;
}
}
else
{
try
{
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
xmlHttp = false;
}
}
if (!xmlHttp)
{
alert("Error creating the XMLHttpRequest object.");
}
else
{
return xmlHttp;
}
}
function process()
{
varWBS = encodeURIComponent(document.getElementById("TextBox1").value);
xmlHttp.open("GET", "test.aspx?WBS=" + varWBS);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
}
function handleServerResponse()
{
//if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
//{
// document.getElementById("show").innerHTML = xmlHttp.responseText;
//}
//}
//New code
if (xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var parentElement = document.getElementById('show');
var wrappingDiv = document.createElement('div');
wrappingDiv.innerHTML = xmlHttp.responseText;
parentElement.appendChild(wrappingDiv);
//document.getElementById("show").innerHTML = xmlHttp.responseText;
//document.all[show].innerHTML = xmlHttp.responseText;
}
else
{
alert("hmmmm");
}
}
}
//End code
</script>
<!-- End script -->
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table id="maintbl" border="1">
<tr>
<td><asp:textbox id="TextBox1" onblur="process()" style="Z-INDEX: 101" runat="server"></asp:textbox></td>
<td><asp:textbox id="TextBox2" style="Z-INDEX: 101" runat="server"></asp:textbox></td>
</tr>
<tr>
<td>
<div id="show"></div>
</td>
</tr>
</table>
</form>
</body>
</HTML>
-
All
The repeating div tags are fine.
I can type in the value in one text box, then it'll go to the code behind of another page and do the correct functionality....which will tell the user if the number is distinct or not.
The only problem is when I post back the page, I get an IE error, ieexplore.exe error, The instruction at "0x4a594476" referenced memory at "0x00000004". The memory could not be "read".
HTML code is:
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<!-- Start script -->
<script language="javascript">
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
var xmlHttp;
if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
xmlHttp = false;
}
}
else
{
try
{
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
xmlHttp = false;
}
}
if (!xmlHttp)
{
alert("Error creating the XMLHttpRequest object.");
}
else
{
return xmlHttp;
}
}
function process()
{
varWBS = encodeURIComponent(document.getElementById("TextBox1").value);
xmlHttp.open("GET", "test.aspx?WBS=" + varWBS);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
}
function handleServerResponse()
{
//if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
//{
// document.getElementById("show").innerHTML = xmlHttp.responseText;
//}
//}
//New code
if (xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var parentElement = document.getElementById('show');
var wrappingDiv = document.createElement('div');
wrappingDiv.innerHTML = document.getElementById("TextBox1").value+' '+xmlHttp.responseText;
parentElement.appendChild(wrappingDiv);
//document.getElementById("show").innerHTML = xmlHttp.responseText;
//document.all[show].innerHTML = xmlHttp.responseText;
}
else
{
alert("hmmmm");
}
}
}
//End code
</script>
<!-- End script -->
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table id="maintbl" border="1" style="WIDTH: 952px; HEIGHT: 59px">
<tr>
<td style="WIDTH: 327px"><asp:textbox id="TextBox1" onblur="process()" style="Z-INDEX: 101" runat="server" Width="100%"></asp:textbox></td>
<td><asp:textbox id="TextBox2" style="Z-INDEX: 101" runat="server"></asp:textbox></td>
</tr>
<tr>
<td colspan="2">
<div id="show" style="FONT-SIZE: smaller; COLOR: red" runat="server"></div>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<asp:Label id="Label1" runat="server">Label</asp:Label>
</td>
</tr>
</table>
</form>
</body>
</HTML>
The code behind for the test.aspx page is:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
sCon1.Open()
Dim strSQL As String = "Select count(*) from Table where [Number] = '" & Server.HtmlEncode(Request.QueryString("WBS")).ToString() & "'"
Dim cmd As New SqlCommand(strSQL, sCon1)
Dim intCnt As Integer = cmd.ExecuteScalar()
sCon1.Close()
If intCnt = 0 Then
Response.Write("is unique, and able to be added!")
Else
Response.Write("is already used. Please select another Number!")
End If
Catch ex As Exception
Response.Write(ex.ToString())
Finally
sCon1.Close()
End Try
Similar Threads
-
Replies: 4
Last Post: 04-14-2006, 09:09 AM
-
By Billkamm in forum Database
Replies: 0
Last Post: 01-30-2006, 02:54 PM
-
By Colin McGuigan in forum Database
Replies: 12
Last Post: 04-15-2002, 07:43 AM
-
Replies: 1
Last Post: 11-27-2001, 06:53 AM
-
By Bob Hines in forum Database
Replies: 7
Last Post: 04-27-2000, 11:14 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks