Friday, March 30, 2012

Problem with SqlParameterCollection - Looking for advice

I have a site which works fine with an Access DB, I now want to upsize it to use a SQL DB... I have changed my OleDB Commands etc... And Used SqlCommands and everything seems fine..

Only one problem I have and its really stumped me... I always used SQLDataSource for the app even with the AccessDB, as I knew I would be upsizing at some point. We I have the below SQLDataSource

1 <asp:SqlDataSource ID="RandomBusinessDataSource" runat="server"
2 ConnectionString="<%$ ConnectionStrings:SQLDataBaseConnectionString%>"
3 SelectCommand="SELECT TOP (1) intBusinessID, txtBusinessName, intSubCatID, intCatID, txtTelNo, txtPostCode, txtWebAdd, txtReferred, bitBusinessShow, txtLong, txtLat, memBusinessDesc, intIPAddress, bitBusinessPaid FROM tblBusiness WHERE (intBusinessID = @.Param1)" OnSelecting="RandomBusinessDataSource_Selecting">
4 <SelectParameters>
5 <asp:Parameter Name="Param1" Type="Int32" />
6 </SelectParameters>
7 </asp:SqlDataSource>

Very Simple... I have my function that is creating my random number and returning a VALID intbusinessID ... And now I have this OnSelecting event

1 Protected Sub RandomBusinessDataSource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)
2 e.Command.Parameters("Param1").Value = MyNewRandomNum
3 End Sub

But when I try and run the page I get the following error (Don't forget this page IS working using the SQLDataSource and an Access DB??)

An SqlParameter with ParameterName 'Param1' is not contained by this SqlParameterCollection.

I am a bit confused?? why would it say this now... Yet it works fine if I just change the connectionString to the access DB?? Any helps very much appreciated... Thanks

Try changing "Param1" to "@.Param1" in the parameter declaration and in the code where you set the value.|||

Hi TGnat ... thanks for the help...

Unfortunately that gave the same error - Although I did manage get sort it, a chap I know at Cre8asites gave me the answer

( thread herehttp://www.cre8asiteforums.com/forums/index.php?showtopic=44722&hl= )

I had to use the following and it worked fine...

e.Command.Parameters(0).Value = MyNewRandomNum

|||Hey TGnat ... Actually you were pretty much right in the end - I printed out the parameter name and it was @.Param1 ... Although I just had the leave the selectparameter name as Param1 ... Weird hey!!... Thanks again

No comments:

Post a Comment