Hackviking He killed Chuck Norris, he ruled dancing so he took up a new hobby…

12Sep/090

Must declare the scalar variable @ID

Was just burning the midnight oil on a project for a client. Working with asp.Net for the first time in ages. Making a quick fix, adding a new form for editing customer details, I used theSqlDataSource control. Usually I work with the SqlCommand in SqlDataClient directly but now I needed paging and sorting fixed quick and easy! I had a really big problem with the select command parameters. When you use them with the SqlCommand you declare them with an @ prefix in the select command like this:

Select * FROM Customers WHERE CustomersID = @ID 

(SELECT * is a sin, I know!)

Then declare the variable as: SqlCommand.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int))
Received errors like "Must declare the scalar variable @ID" and so on. Checked, dubble checked and triple checked... The variable was defined!
But no! Some idiot at MS decided to think outside of the box and the declaration for the SqlDataSource control should be without the @!

Not:

<asp:SqlDataSource ID="sdsContacts" runat="server" ConnectionString="<%$ ConnectionStrings:DB_CONN %>"
SelectCommand="Select * FROM Customers WHERE CustomersID = @ID" ProviderName="System.Data.SqlClient">
<SelectParameters>
<asp:Parameter Name="@ID" Direction="Input" />
</SelectParameters>
</asp:SqlDataSource>

But instead this:

<asp:SqlDataSource ID="sdsContacts" runat="server" ConnectionString="<%$ ConnectionStrings:DB_CONN %>"
SelectCommand="Select * FROM Customers WHERE CustomersID = @ID" ProviderName="System.Data.SqlClient">
<SelectParameters>
<asp:Parameter Name="ID" Direction="Input" />
</SelectParameters>
</asp:SqlDataSource>

Posted by Kristofer Källsbo

Tagged as: , , Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a Reply

No trackbacks yet.