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?

3
  • 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 Commented Dec 24, 2012 at 14:47
  • I want to do your way too. but I don't know how can I do this? Commented Dec 24, 2012 at 14:55
  • 2
    possible duplicate of How to pass variable to SelectCommand of SqlDataSource?. The first answer of that question will show you how to do this properly with parameters. Commented Dec 24, 2012 at 14:57

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Please state that clearly in your question. That will help you get the answers you are looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.