ExecuteNonQuery: Connection property has not been initialized

  • Thread starter Thread starter Jason Williard
  • Start date Start date
J

Jason Williard

I am trying to create a web form that will be used to create new users. The
first step that I am taking is creating a web form that can check the
username against a database to see if it already exists. I would it to do
this on the fly, if possible. When I execute my current code, I get the
following error:

ExecuteNonQuery: Connection property has not been initialized

Below is the code from the page itself:
-----
<!-- #INCLUDE FILE="../include/context.inc" -->
<!-- #INCLUDE FILE="../include/db_access.inc" -->

<script language="VB" runat="server">

Sub CheckButton_Click(Sender as Object, e as EventArgs)

Dim result As Int32
Dim cmd As OdbcCommand

cmd = new OdbcCommand( "(? = CALL CheckUserExists(?))", db_conn )
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add( "result", OdbcType.Int ).Direction =
ParameterDirection.ReturnValue

cmd.Parameters.Add( "@userName", OdbcType.VarChar, 100 ).Value =
Request.Form("userName")

cmd.ExecuteNonQuery()
result = cmd.Parameters("result").Value

If result <> 1 Then
CheckResults.Text="<font color=""#ff0000"">Username already
exists!</font>"
Else
CheckResults.Text="<font color=""#009900"">Username is
available.</font>"
End If

end Sub

</script>

<html><body>
<form runat="server">
<asp:TextBox id=userName runat="server" />
<asp:Button id=CheckButton runat="server" Text="Check Username"
onClick="CheckButton_Click" />

<p>
<asp:Label id=CheckResults runat=server />
</form>
</body></html>
-----

Can anyone see why I might get this error? Here are some more details of
the error:

Line 15: cmd.Parameters.Add( "@userName", OdbcType.VarChar, 100 ).Value =
Request.Form("userName")
Line 16:
*Line 17: cmd.ExecuteNonQuery()
Line 18: result = cmd.Parameters("result").Value

Thank You,
Jason Williard
 
Cor,

My answer is quite simple. I am new to anything and everything .NET. In
the past, I have developed with PHP. So, the code that I use is partially
mine and partially pre-existing code. In this case, I copied the format to
connect and used it over again in this page.

While this method has worked for me many times before, it failed this time.
I wonder if it is, perhaps, because I am using web forms or enclosing the
connection with in a Sub. What are your suggestions in the way of a fix?
How difficult is it to switch to OleDB?

Thank You,
Jason Williard
 
Jason,

The standard first question for ODBC users, why are you using ODBC because
OleDb gives so much less trouble.

There is in my opinion not so much ODBC knowledge (Except for MySQL) in
these newsgroups while that is for OleDB, SQLClient and OracleClient a lot.

Cor
 
Jason,

When you are not using MySQL as I wrote (you said PHP, so maybe you are
using it) than very easy I did not see any limitations with Microsoft
databases. The problem can be that there is not yet a good OleDb
connectionstring/provider for your database.

I found this interesting messagethread by the way. To got a good opinion you
should read it untill the end.
http://tinyurl.com/5xljx

In these pages you see the connectionstrings
http://www.connectionstrings.com/

http://www.able-consulting.com/ADO_Conn.htm

When it is possible than you can start changing everywhere that you have
typed ODBC in OleDB
Dont forget to set this somewhere in top of your page.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

I saw you are using the parameters what is well, however it goes to far now
checking if it is well done. I would try those changes first.

I hope this helps something?

Cor










Cor
 
Back
Top