SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to create query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

lazarjojic
Starting Member

Yugoslavia
7 Posts

Posted - 11/11/2012 :  09:01:21  Show Profile  Reply with Quote
I have form and few textboxes(id,name,last name..) on it.That form is for searching some accounts in my database.If i enter some text in taextbox name, sql query needs to search accounts with that specific name, but if i dont enter text at all query needs to search all names in database.It also aplies to all other textfields?


lazar jojic

lazarjojic
Starting Member

Yugoslavia
7 Posts

Posted - 11/12/2012 :  03:32:35  Show Profile  Reply with Quote
It's like this in C#.


SqlCommand cmd = new SqlCommand("SELECT * FROM accounts WHERE (id=@id2 OR @id2 is null) AND (name=@name2 or @name2 is null) AND (last_name=@last_name2 or @last_name2 is null)",con);



if (id.Text.ToString() == "") cmd.Parameters.AddWithValue("id2", DBNull.Value);
else cmd.Parameters.AddWithValue("id2", id.Text);
if (name.Text.ToString() == "") cmd.Parameters.AddWithValue("name2", DBNull.Value);
else cmd.Parameters.AddWithValue("name2", ime.Text);
if (last_name.Text.ToString() == "") cmd.Parameters.AddWithValue("last_name2", DBNull.Value);
else cmd.Parameters.AddWithValue("last_name2", last_name.Text);

lazar jojic

Edited by - lazarjojic on 11/12/2012 03:33:31
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/12/2012 :  06:55:33  Show Profile  Reply with Quote
The query and the code you have posted, at least conceptually, seems fine. What is the issue you are facing? Is it not working as expected?

There is one problem you may run into - and that relates to poor performance with this type of "Catch-all" queries. See Gail Shaw's blog here for a discussion and possible solutions: http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/
Go to Top of Page

lazarjojic
Starting Member

Yugoslavia
7 Posts

Posted - 11/12/2012 :  10:07:02  Show Profile  Reply with Quote
It is ok.i answered myself on my question

lazar jojic

Edited by - lazarjojic on 11/12/2012 10:07:56
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000