Visual Basic .NET » Database Programming
Text box show null on return -- bubberz --


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 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. I'm trying to do a count on the text entered, then notifiy the user if that number exists or not. Whatever I type in, the second box says '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
'Try
' sCon1.Open()
' Dim strRQ As String = txtStateInitial.Text.ToString()
' Dim strSQL As String = "Select count(*) from WBTbl where
= '" & strRQ & "'"
' Dim cmd As New SqlCommand(strSQL, sCon1)
' 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)
{
CSS.WebForm1.GetStateName(obj.value,CallbackFunc);
}
function CallbackFunc(res)
{
document.getElementById('txtStateName').value = 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>State Name:</div>
<input id="txtStateName" type="text">
</form>
</body>
</HTML>

-- bubberz --


Joseph,

Thanks for the help!

I just added this javascript, and get an alert message of
:
function CallbackFunc(res)
{
if (res.error)
{
alert(res.error);
}
else
{
alert(res.value);
}
document.getElementById('txtStateName').value = res.value;
}

-- JosephGuadagno --


There could be an erro being returned from the ASP.NET. You should check for res.error first to see if it is not null, then work on the value. Did you set a breakpoint in your ASP.NET method GetStateName to see if it was fired and the value of stateInitial?

Joseph Guadagno
http://josephguadagno.net 


-- bubberz --


With a breakpoint at the Ajax Method/GetStateName, I hover over GetStateName and it "= Nothing", but if I hover over stateInitial, then it's got the value of what I typed in the box..

-- JosephGuadagno --


Use the following code for the callback

function CallbackFunc(res)
{
if (res.error != null)
alert(res.error.Message);
else
alert(res.value);
document.getElementById('txtStateName').value = res.value;
// this assumes that txtStateName is an INPUT box of type Text
}
}

Joseph Guadagno
http://josephguadagno.net 


-- bubberz --


Joseph,

My text box for the user to enter in data is an ASP:Textbox

-- bubberz --


I used the above javascript, and got "Object Reference not set to an instance of an object"

-- bubberz --


I can write the variable sent to the method out to a div tag, and see it's value, and even write out the SQL string to the div tag and it works.

It's just when I get to the SQLCommand it says null in debug.

-- JosephGuadagno --

I see what you are talking about. The problem lies within the following code

*

' sCon1.Open()
' Dim strRQ As String = txtStateInitial.Text.ToString()
' Dim strSQL As String = "Select count(*) from WBTbl where
*You can not access the txtStateInitial text box becuase ASP.NET runtime does not about it since you are not officially posting back. Change the line strRQ to 


Dim strRQ As String = stateInitial

Joseph Guadagno
http://josephguadagno.net 

On 7/11/06, bubberz <chip> wrote:


I can write the variable sent to the method out to a div tag, and see
it's value, and even write out the SQL string to the div tag and it
works.

It's just when I get to the SQLCommand it says null in debug.
>

--  

Joseph Guadagno
http://josephguadagno.net 


-- bubberz --


Hi Joseph!

Thanks for the help...yeah, I was thinking about what you say above last night @ home, but still couldn't get it to work. I get the 'null'
value just like before, along with the same alert message.

Try
sCon1.Open()
Dim strSQL As String = "Select count(*) from WBTbl where
= '" & stateInitial & "'"
Dim cmd As New SqlCommand(strSQL, sCon1)
Dim intCnt As Integer = cmd.ExecuteScalar()
sCon1.Close()
If intCnt = 0 Then
Return "This is unique"
Else
Return "Please select another!"
End If

My breakpoint does show stateInitial with the value I put in the box though.

-- MichaelSchwarz --


Hi,

can you send me the full AjaxMethod source code?

Regards,
Michael
On 7/12/06, bubberz <chip> wrote:

Hi Joseph!

Thanks for the help...yeah, I was thinking about what you say above
last night @ home, but still couldn't get it to work. I get the 'null'
value just like before, along with the same alert message.

Try
sCon1.Open()
Dim strSQL As String = "Select count(*) from WBTbl where
= '" & stateInitial & "'"
Dim cmd As New SqlCommand(strSQL, sCon1)
Dim intCnt As Integer = cmd.ExecuteScalar()
sCon1.Close()
If intCnt = 0 Then
Return "This is unique"
Else
Return "Please select another!"
End If

My breakpoint does show stateInitial with the value I put in the box
though.
>

--  

Best regards | Schöne Grüße Michael

Microsoft MVP - Most Valuable Professional Microsoft MCAD - Certified Application Developer

http://weblogs.asp.net/mschwarz 
/
http://www.schwarz-interactive.de 
/
mailto:info
-- bubberz --


<AjaxPro.AjaxMethod()> Public Function GetStateName(ByVal stateInitial As String) As String
Try
sCon1.Open()
Dim strSQL As String = "Select count(*) from WBS_Table where
= '" & stateInitial & "'"
Dim cmd As New SqlCommand(strSQL, sCon1)
Dim intCnt As Integer = cmd.ExecuteScalar()
sCon1.Close()
If intCnt = 0 Then
Return "This WBS is unique"
Else
Return "Please select 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

-- MichaelSchwarz --


Ok, and the JavaScript code (sorry, forgot this in my last message)...
On 7/12/06, bubberz <chip> wrote:

<AjaxPro.AjaxMethod()> Public Function GetStateName(ByVal stateInitial
As String) As String
Try
sCon1.Open()
Dim strSQL As String = "Select count(*) from WBS_Table
where
= '" & stateInitial & "'"
Dim cmd As New SqlCommand(strSQL, sCon1)
Dim intCnt As Integer = cmd.ExecuteScalar()
sCon1.Close()
If intCnt = 0 Then
Return "This WBS is unique"
Else
Return "Please select 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
>

--  

Best regards | Schöne Grüße Michael

Microsoft MVP - Most Valuable Professional Microsoft MCAD - Certified Application Developer

http://weblogs.asp.net/mschwarz 
/
http://www.schwarz-interactive.de 
/
mailto:info
-- bubberz --


Oh..no worries...thanks again Michael!

<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)
{
CSS.WebForm1.GetStateName(obj.value,CallbackFunc);
}
function CallbackFunc(res)
{
if (res.error)
{
alert(res.error);
}
//else
//{
// alert(res.value);
//}
document.getElementById('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>

-- MichaelSchwarz --


Change the local JavaScript function:

function CallbackFunc(res)
{
alert(AjaxPro.toJSON(res));
}

What do you get?

On 7/12/06, bubberz <chip> wrote:

Oh..no worries...thanks again Michael!

<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)
{
CSS.WebForm1.GetStateName(obj.value,CallbackFunc);
}
function CallbackFunc(res)
{
if (res.error)
{
alert(res.error);
}
//else
//{
// alert(res.value);
//}
document.getElementById('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>
>

--  

Best regards | Schöne Grüße Michael

Microsoft MVP - Most Valuable Professional Microsoft MCAD - Certified Application Developer

http://weblogs.asp.net/mschwarz 
/
http://www.schwarz-interactive.de 
/
mailto:info
-- bubberz --


With the following HTML javascript, I get a rather large "Object reference not set to an instance of an object" error:

<script language="javascript">
function getStateName(obj)
{
CSS.WebForm1.GetStateName(obj.value,CallbackFunc);
}
//function CallbackFunc(res)
//{
// if (res.error)
// {
// alert(res.error);
// }
//else
//{
// alert(res.value);
//}
//document.getElementById('txtStateName').innerHTML = res.value;
//}
function CallbackFunc(res)
{
alert(AjaxPro.toJSON(res));
}

</script>

-- bubberz --


Michael,

Sorry...should have written out the errorr message:

{"error":{"Message":"Object reference not set to an instance of an object.", "Type":"System.NullReferenceException"},"value":null,
"request":{"method":"GetStateName","args":{"stateInitial":"34344"}},"context":null,"duration":15,"json":"null;r.error
= {\"Message\":\"Ojbect not set to an instance of an object.\",\"Type\":\"System.NullReferenceException\"};/*"}

I did try the SQL code with the button click event, and it works correctly with a post back. I got that suggestion from another JavaScript forum as a safety check.

Thanks for all the help!

-- bubberz --


Michael,

I thought I'd run this by you too for some ideas. The following code doesn't use the AjaxPro.dll at all.
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
.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
= '" &
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

[Submit Comment]Home