Click here to Skip to main content
15,908,172 members
Home / Discussions / Database
   

Database

 
Questionrestor backup in sql Pin
8213001231-Oct-07 9:40
8213001231-Oct-07 9:40 
AnswerRe: restor backup in sql Pin
Hesham Amin31-Oct-07 11:23
Hesham Amin31-Oct-07 11:23 
AnswerRe: restor backup in sql Pin
Paul Conrad31-Oct-07 14:08
professionalPaul Conrad31-Oct-07 14:08 
QuestionSQL Express server con error Pin
solarthur0131-Oct-07 7:36
solarthur0131-Oct-07 7:36 
AnswerRe: SQL Express server con error Pin
pmarfleet31-Oct-07 8:13
pmarfleet31-Oct-07 8:13 
GeneralRe: SQL Express server con error Pin
pmarfleet31-Oct-07 13:15
pmarfleet31-Oct-07 13:15 
AnswerRe: SQL Express server con error Pin
Paul Conrad31-Oct-07 14:09
professionalPaul Conrad31-Oct-07 14:09 
QuestionWHERE Clause's Column from a Parameter [modified] Pin
Brian C Hart31-Oct-07 7:13
professionalBrian C Hart31-Oct-07 7:13 
Hello folks,

I am a Newbie. I know I could just do this dynamically, however I am looking for any possible way to keep as much stuff declared at design-time as possible (just a personal preference).

I want to do as is illustrated below -- using the 'Employees' table from the 'Northwind' database. My environment is VS.NET 2005 with SQL server 2005, language is C# and page is ASP.NET.

Basically, I have a DropDown with "City" and "LastName" as the choices, next to a TextBox. The user picks either of the field names in the DropDownList and then types a search filter in the TextBox. I want to use a Parameter to specify the column name and a Parameter to specify the value to search with a WHERE clause. The value is replaced with its appropriate SelectParameter but not the field to search, i.e. the code below is not working:

Default.aspx:

<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>City</asp:ListItem>
<asp:ListItem>LastName</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server" Width="220px"></asp:TextBox><br />
<br />
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False"
ReadOnly="True" SortExpression="EmployeeID" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [City] FROM [Employees] WHERE ([@Field] LIKE @Value)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Field" PropertyName="SelectedValue" Type="String"/>
<asp:ControlParameter ControlID="TextBox1" Name="Value" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

Is there something I am doing wrong? Am I not allowed to say, i.e. SelectCommand="SELECT [field1] [field2] FROM [mytable] WHERE [@param] = @value" where @param upon replacement, specifies the field's name? It is not getting replaced, so naturally SQL Server complains with the error:

Invalid column name '@Field.'

What's up? Why cannot I use a parameter when specifying a field in a WHERE clause?


-- modified at 13:21 Wednesday 31st October, 2007
AnswerRe: WHERE Clause's Column from a Parameter Pin
pmarfleet31-Oct-07 7:32
pmarfleet31-Oct-07 7:32 
QuestionCount errors and not display any less than a value Pin
solutionsville31-Oct-07 7:00
solutionsville31-Oct-07 7:00 
AnswerRe: Count errors and not display any less than a value Pin
pmarfleet31-Oct-07 7:09
pmarfleet31-Oct-07 7:09 
GeneralRe: Count errors and not display any less than a value Pin
solutionsville31-Oct-07 7:17
solutionsville31-Oct-07 7:17 
GeneralRe: Count errors and not display any less than a value Pin
pmarfleet31-Oct-07 7:28
pmarfleet31-Oct-07 7:28 
AnswerRe: Count errors and not display any less than a value Pin
solutionsville31-Oct-07 7:29
solutionsville31-Oct-07 7:29 
QuestionNested Stored Procedure Pin
lossy31-Oct-07 5:53
lossy31-Oct-07 5:53 
AnswerRe: Nested Stored Procedure Pin
pmarfleet31-Oct-07 7:05
pmarfleet31-Oct-07 7:05 
GeneralRe: Nested Stored Procedure Pin
lossy31-Oct-07 8:26
lossy31-Oct-07 8:26 
GeneralRe: Nested Stored Procedure Pin
pmarfleet31-Oct-07 8:44
pmarfleet31-Oct-07 8:44 
Questionautomaticaly refresh Pin
muharrem31-Oct-07 5:37
muharrem31-Oct-07 5:37 
AnswerRe: automaticaly refresh Pin
Paul Conrad31-Oct-07 14:10
professionalPaul Conrad31-Oct-07 14:10 
GeneralRe: automaticaly refresh Pin
muharrem1-Nov-07 1:07
muharrem1-Nov-07 1:07 
AnswerRe: automaticaly refresh Pin
ALAQUNAIBI2-Jan-09 21:23
ALAQUNAIBI2-Jan-09 21:23 
GeneralRe: automaticaly refresh Pin
muharrem5-Jan-09 10:12
muharrem5-Jan-09 10:12 
QuestionSQL 2005 connectivity Pin
nicetohaveyou31-Oct-07 2:37
nicetohaveyou31-Oct-07 2:37 
AnswerRe: SQL 2005 connectivity Pin
pmarfleet31-Oct-07 2:46
pmarfleet31-Oct-07 2:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.