0

I have a SqlDataSource for a gridview. I want to change SelectCommand of that SqlDataSource.

My select command is:

select *   
from JobLog 
where UserName = @username 
  and PrinterName = @printer 
  and TimeSubmitted between @from and @to

I want to replace @username and ... with textboxes.

How can I do this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Majid Basirati
  • 2,665
  • 3
  • 24
  • 46
  • 1
    **DON'T**! Instead: learn how to **supply proper values** for those parameters - **don't concatenate together your SQL commands!** - this opens all doors to SQL injection attacks - just don't even think about it. Learn the **proper way** - use **parametrized queries** – marc_s Dec 24 '12 at 14:47
  • I want to do your way too. but I don't know how can I do this? – Majid Basirati Dec 24 '12 at 14:55
  • 2
    possible duplicate of [How to pass variable to SelectCommand of SqlDataSource?](http://stackoverflow.com/questions/485821/how-to-pass-variable-to-selectcommand-of-sqldatasource). The first answer of that question will show you how to do this properly with parameters. – Ken White Dec 24 '12 at 14:57

1 Answers1

1

In your SQLDataSource, add SqlParameter to it. Add one for each textbox that you want to use for the query. Example:

<SelectParameters>
    <asp:ControlParameter Name="UserName" ControlID="txtUserName" />
</SelectParameters>
CM Kanode
  • 1,436
  • 1
  • 11
  • 14