Using SelectParameters in ASP.NET gives 'Must declare variable' errors - asp.net

I have an ASP SqlDataSource connected to Sybase DB query, using Select Parameters that are populated by a dropdown:
Dropdown (works OK):
<asp:SqlDataSource ID="dsBondIDList" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [name], [bondID] FROM [bonds]">
</asp:SqlDataSource>
<asp:DropDownList ID="lstBondID" runat="server" DataSourceID="dsBondIDList" DataTextField="name" DataValueField="bondID">
SqlDataSource with parameter:
<asp:SqlDataSource ID="dsBonds" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [ticker], [name], [isin], [currency],
[stock], [maturity], [bid], [ask]
FROM [bonds] where [bondID] = #bondID">
<SelectParameters>
<asp:ControlParameter Name="bondID" ControlID="lstBondID" PropertyName="SelectedValue" DefaultValue="-1" />
</SelectParameters>
</asp:SqlDataSource>
But I get an error when I try to run it:
ERROR [HY000] [DataDirect][ODBC Sybase Wire Protocol driver][SQL Server]Must declare variable '#bondID'.
Which of course I would get it I ran the sql literally as displayed. I expect the ASP engine to do the substitution before sending the query (and to use the default value of lstBondID if necessary)
Does anybody know why the #bondID is not being substituted with the lstBondID.SelectedValue?
Thanks in advance
Ryan

I just went through this problem 10 minutes ago and here's the fix:
If you need to keep using the ODBC driver you have, change your query to use ? instead of Named Parameters.
SELECT [ticker], [name], [isin], [currency],
[stock], [maturity], [bid], [ask]
FROM [bonds] where [bondID] = ?
The parameters are used in the order they are added to the parameters collection (This gets hairy when you need to use the parameter more than once in your query etc).
Otherwise you can change drivers. Look for sybdrvodb.dll and regsvr32 it. Then setup your DSN and use that.

Related

GridView and Stored Procedure?

I want to use a GridView in Visual Studio. I went through the tutorial they have, and I have it set. However I don't see how I can use my Stored Procedure.
It sets me up with this:
<asp:SqlDataSource ID="SqlDataSourceViewRegistrants" runat="server"
ConnectionString="<%$ ConnectionStrings:Events2 %>"
SelectCommand="SELECT * FROM [Registrant]"></asp:SqlDataSource>
And I somehow want to replace that SelectCommand with a Stored Procedure, as such:
sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
sqlCmd.CommandText = "spGetAllRegistrants";
I'm not sure where to put this info of spGetAllRegistrants.
You need to change the SelectCommandType to StoredProcedure:
<asp:SqlDataSource ID="SqlDataSourceViewRegistrants" runat="server"
ConnectionString="<%$ ConnectionStrings:Events2 %>"
SelectCommand="spGetAllRegistrants"
SelectCommandType="StoredProcedure"></asp:SqlDataSource>

asp:sqldatasource SelectCommandType="Text" Dropdown not populating ONLY when parameters are passed to SQL

I'm trying to use a asp:SqlDataSource driven by a SQL Select with Arguments. It works as long as I don't have any arguments. I can run the parameterized query via studio and it works and returns rows. If I any arguments, then the asp:View doesn't render.
First, the code returns 2 rows when I execute the SQL via Studio.
Second, I don't want it as a stored proc. Deployment issue. Smile, let it go. :-)
<asp:DropDownList ID="lstUsers" runat="server" DataSourceID="sqlGetCSGUsersOnClaim" DataTextField="username" DataValueField="userID" />
<asp:SqlDataSource ID="sqlGetCSGUsersOnClaim" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer%>" SelectCommandType="Text" SelectCommand="
SELECT aspnet_Users.UserId as userID, aspnet_users.username as username
FROM claims, aspnet_Users
WHERE claims.claimid = #ClaimID and
(Claims.AdjusterID = aspnet_Users.UserId or Claims.SupervisorID = aspnet_Users.UserId )">
<SelectParameters>
<asp:SessionParameter Name="ClaimID" SessionField="ClaimID" Type="Int32" DbType="Int32" />
</SelectParameters>
</asp:SqlDataSource>
If I remove the #ClaimID and SelectParameters arguments, the dropdown populates. It only fails once I add the parameter.
What am I doing wrong?
Thanks in advance,
Jason
Well it turned that if I removed the Type and DBType attributes of the SelectParameter, the code worked. sigh....
I have no idea, why though.

change text on a hyperlink in a datalist

I'm trying to bind a column from a SQL code that i have written to all hyperlinks in a datalist. This should be really simple but I'm getting the error
DataBinding: 'System.Data.Common.DataRecordInternal' does not contain
a property with the name 'NumberOfComments'.
Well I'm pretty sure the column exist but in this case it's created by a function maybe that has something to do with it. When I run the SQL code i get the values I should.
The hyperlink
<asp:HyperLink ID="lnkComment" runat="server"
NavigateUrl='<%# Eval("ID", "~/Default.aspx?ID={0}") %>'
Text='<%# Eval("NumberOfComments") %>'></asp:HyperLink>
The SQLDataSource
<asp:SqlDataSource ID="sdsNews" runat="server"
ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString %>" SelectCommand="SELECT News.ID, News.Topic, News.Text, News.PostTime, aspnet_Users.UserName, "NumberOfComments" = dbo.fnNumberOfCommentOnNews(News.ID)
FROM News INNER JOIN
aspnet_Users ON News.UserId = aspnet_Users.UserId
WHERE (News.ID = ISNULL(#ID, News.ID))
ORDER BY News.PostTime DESC ">
<SelectParameters>
<asp:QueryStringParameter DbType="Guid" Name="ID" QueryStringField="ID" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>
Why do you have """ surrounding NumberOfComments FieldName?
Try pasting the select command into SSMS (SQL Server Mgmt studio) and you should most definitely get an incorrect syntax error. If you drop those quot html code you will still be able to reference and bind to that column.

Accessing query string variable in the sql query in aspx page

I am quite new to asp.net,I am building an application where I need to show the in the grid view,Now the query I am generating for fetching the data from database containing one parameter which comes from the query string.I am using this code
<asp:SqlDataSource runat="server" ID="MySQLData"
ConnectionString="server=localhost;port=3309; User Id=xxxxx;password=xxxxx;database=xxxxx"
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT contenthead.lastmodifieddate,contenthead.heading,lk_technology.technology FROM contenthead JOIN lk_technology WHERE contenthead.techID=lk_technology.techID AND contenthead.authorid='<%=Request.QueryString["uid"]%>'" />
Now when I am using <%..%> tag I am getting parser error that says: Server tags cannot contain <% ... %> constructs.
Now I want to use this variable from query string.Please tell me how I can access this variable in this context.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="..." ProviderName="System.Data.SqlClient" SelectCommand="SELECT [UserId], [LastUpdatedDate] FROM [vw_aspnet_Profiles] WHERE ([UserId] = #UserId)">
<SelectParameters>
<asp:QueryString ParameterDefaultValue="0" Name="UserId" QueryStringField="Id" Type="Object"/>
</SelectParameters>
</asp:SqlDataSource>
You have to add a parameter to your SqlDataSource

asp.net parameters in odbc

I have a page 3 datacontrols (in order, datalist->grid->listview, the selection from one feeds into the next) works perfectly locally, using dot net connector for mysql. My webhost uses ODBC, and I had to remove [ ] from the select statements in my code and put the table names. I removed the [ ] 's and my page runs, and my 1st/datalist control shows but now my "selected value" of my datalist, doesn't populate my grid control.
I imagine theres another peculiarity with the way odbc handles parameters.
Here is my original, for my grid:
<asp:SqlDataSource ID="recipegrid" runat="server"
ConnectionString="<%$ ConnectionStrings:exoticingConnectionString %>"
ProviderName="<%$ ConnectionStrings:exoticingConnectionString.ProviderName %>"
SelectCommand="SELECT [Id], [Name], [Cal], [Pro], [Fat], [Carb], [Fiber], [Chol], [Sod] FROM [tblrecipes] WHERE ([filenameid] = #filenameid) ORDER BY [name]">
<SelectParameters>
<asp:ControlParameter ControlID="DataList1" Name="filenameid"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
example of changes to select that allowed my page to run again:
SelectCommand="SELECT tblrecipes.Id, tblrecipes.Name, tblrecipes.Cal, tblrecipes.Pro, tblrecipes.Fat, tblrecipes.Carb, tblrecipes.Fiber, tblrecipes.Chol, tblrecipes.Sod FROM tblrecipes WHERE tblrecipes.filenameid = #filenameid ORDER BY tblrecipes.name">
BTW,
I also tried removing my scriptmanager & updatepanel,, and using autopostbacks instead in my controls, jik, and nothing changed so I put it back.
changed :
WHERE ([filenameid] = #filenameid)
to:
WHERE ([filenameid] = ?)
and it worked

Resources