ExecuteReader(CommandBehavior.CloseConnection)

Joined
Jan 22, 2003
Messages
22
Location
Indiana, USA
Im trying to use CommandBehavior.CloseConnection to close the data conection. When I try this method I get the following error:
No value given for one or more required parameters.

Exception Details: System.Data.OleDb.OleDbException: No value given for one or more required parameters.

Source Error:

Line 21: lstTest.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection)

What am I missing with this? Ive tried a few things and nothing seems to work. I can go back to the old db.close method, but this seems cleaner and more efficient. Any thoughts?

Cheers!
 
This should work

Dim SqlString As String = "Your string"
Dim myCommandE As New SqlCommand(SqlString, myConnection)
Dim objDR As SqlDataReader
MyConnection.Open()
objDR=myCommandE.ExecuteReader(system.data.CommandBehavior.CloseConnection)
While objDR.Read()
 
I still cant anything to work. Heck I cant even get a connection after fiddling with it all. This is my code:


<%@ Page Language="vb" Debug="true" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>
<script runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
Create a connection
Const sConnStr as String="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Inetpub\wwwroot\testing net\Citadel.mdb"
Dim objConn as New OleDbConnection(sConnStr)

You must open the connection before populating the DataReader
objConn.Open()

Create a command object for the query
Const sSQL as String = "SELECT [last name] & , & [First name] FROM Employee WHERE ActiveEmp=Yes ORDER BY [last name] & , & [First name];"
Const sSQL as String = "SELECT * FROM Employee"
Dim objCmd as New OleDbCommand(sSQL, objConn)

Create/Populate the DataReader
Dim objDataRead as OleDbDataReader
objDataRead = objCmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)

lstTest.DataSource = objDataRead
lstTest.DataBind()

objDR = Nothing
objConn.Close()
objConn = Nothing
End Sub
</script>
<html>
<body>
<form runat="server" ID="Form1">
<asp:listbox id="lstTest" runat="server" Rows="1" DataTextField="Task" />
</form>
</body>
</html>


I mean thats it and i looks simple enough. But i know Im not doing something right when I cant connect to a database 100% of the time.

Thanks.
 
This code works like a champ

<%@Import Namespace="System.Data.OleDb" %>
<%@Import Namespace="System.Data" %>
<%@ Page Language="vb" Debug="true" %>

<HTML>
<script runat="server">



Sub Page_Load(sender as Object, e as EventArgs)
Create a connection
Const sConnStr as String="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\testing net\Citadel.mdb"
Dim objConn as New OleDbConnection(sConnStr)

You must open the connection before populating the DataReader


Create a command object for the query
Const sSQL as String = "SELECT * FROM Employee WHERE ActiveEmp=Yes ORDER BY [last name] & , & [First name];"
Dim objCmd as New OleDbCommand(sSQL, objConn)



Create/Populate the DataReader
Dim objDataRead as OleDbDataReader
objDataRead = objCmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)

objConn.Open()
ListBoxA.DataTextField = "Last Name"
ListBoxA.DataSource = objCmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
ListBoxA.DataBind()


objConn.Close()
objConn = Nothing
End Sub
</script>
<body>
<form runat="server" ID="Form1">
<asp:ListBox id="ListBoxA" runat="server">
<asp:ListItem></asp:ListItem>
</asp:ListBox>

</form>
</body>
</HTML>
 
Thanks for your help on this, I just started looking at .NET yesterday.

I actually got the same error as before on this line:
ListBoxA.DataSource = objCmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)

"No value given for one or more required parameters. "

I dont get it. I have another little test page that is quite simple and does not get this error. Why is that? Do I need to reference something? I think Im losing this war with .NET.

Thanks again.
 
I replied to your last post 2 times. I must have been posting to quickly because I dont see them up here.

-----------------

Try copying my code verbatim and re-create, move and re-reference the Access.mdb file.

Also try a simple script just to make sure that IIS is processing the ASP.NET

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

Public Sub Page_Load(ByVal sender As Object, e As EventArgs)
Response.Write("asp.net is parsing correctly.")
End Sub


</script>


If the above DOES work and you have re-created, moved, re-referenced the .mdb file, copied my code verbatim and that still WONT work, then you have to learn Java and be a bean coder!

Just kidding. There is a .NET fix, ummm - I dont remember the exact line ---
Or re-install the .NET framework and verify it parses .aspx files.
 
Ok, thanks for you help on this. Its weird it works right now, but like a light swtich it turns off. Ill play around with your suggestions if it stops working again (as I assume it will here in a bit).

Thanks!!
 
Back
Top