954,566 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problems with parameters and selectcommand

Hello Everybody!

I have the following problem: I want to make an application, which need tables to show, edit, etc. You would be able to select the table, which you want to work on, by a dropdownlist (the whole project is in ASP.NET). I want to load the selected table's data to a gridview, and I desperately need a selectcommand for it.
I have succesfully retreived the name of the table, which is selected in the dropdownlist, with a sqlparameter, but can't use it in my selectcommand. The code is the following:

select * from (Select table_name 
from information_schema.tables 
where table_name= @table_name)


I know it probably looks lame, but the simple

select * from @table_name

refused to work too.

To the first one, I get the error message "incorrect syntax near ')'
For the second, it tells me that I must declare the table variable, but I have no idea, how to link it with the dropdownlist.

I thank you forwardly to your help!

konczuras
Junior Poster in Training
59 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

is the first one in a stored procedure? where you pass @table_name in?

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

change your command into:

select * 
  from (Select table_name 
             from information_schema.tables 
             where table_name= @table_name
          ) AliasName


change your second command into:

declare @table_name varchar(255)
select @table_name = 'sysdatabases'
exec ('select * from ' + @table_name)
huangzhi
Light Poster
48 posts since Feb 2008
Reputation Points: 10
Solved Threads: 13
 

Thank you for your help, but it still don't works. The first code ignores the 'select * from' from the beginning, and just displays the table name.
The second tells me that the table_name variable is duplicate, so it can't perform it's task.

Any idea, how could I solve this?

Thanks!

konczuras
Junior Poster in Training
59 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

I've figured it out, I used some coding in C#, and it works perfectly, thank you very much.

konczuras
Junior Poster in Training
59 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

Solved!
I wanted TableName to come from the web.config:

<asp:SqlDataSource ID="dsMySource" runat="server" 
  SelectCommand="
    DECLARE @LocalTable as nvarchar(255)
    SET @LocalTable = @MyTable
    DECLARE @mySQL varchar(max)
    SET @mySQL = 'SELECT * from ' + @LocalTable
    EXEC (@mySQL) "
  ...........
    <SelectParameters>
      <asp:QueryStringParameter DefaultValue="<%$ appSettings:TableName%>" Name="MyTable" QueryStringField="HOrder" />
    </SelectParameters>
</asp:SqlDataSource>


!!BOOM!!

vectorsoftware
Newbie Poster
1 post since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You