Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to use a variable in he querry.
can i use it like...

select * from students where name='textbox.text' ......????

if not then please help how to use a variable that may contain a value at run time and can we use that variable in the querry??



for instance. when i type the word "rahul" in the textbox i want to run a
query like " Select * from customers WHERE name = textbox.Text "

I do not know how to use the textbox name in the query, I think this should
lead me to the solution on finding names.
Posted
Updated 12-Feb-15 3:31am
v2
Comments
Thanks7872 12-Feb-15 9:29am    
Sql server doesn't know what textbox.text means. So you have to make use of ADO.NET to talk to sql server from .NET.

Tons of Articles available out there on Google regarding the same. Try to search.

You can pass command parameters - SqlCommand.Parameters Property [^]. Command parameters are used to pass data from input fields into a query that executes on the back end.
 
Share this answer
 
Comments
Member 11075676 12-Feb-15 9:35am    
please can u elaborate?
Abhinav S 12-Feb-15 9:37am    
Try http://www.csharp-station.com/Tutorial/AdoDotNet/lesson06
Member 11075676 13-Feb-15 11:49am    
Thanks :)
try,

SqlCommand.CommandText = "SELECT * FROM students WHERE name= @Name";
SqlCommand.Parameters.AddWithValue("@Name", textbox.text);
 
Share this answer
 
Comments
Member 11075676 13-Feb-15 11:44am    
thanks a tonn!!! :) happy coding
deepankarbhatnagar 16-Feb-15 6:09am    
your welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900