Click here to Skip to main content
15,850,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
there are 4 columns(A,B,C,D) in SQL Table and 3 string in 3 different variable @abc, @bcd and @cda, stored from 3 textboxes.
and the sql statement is
C#
SqlDataAdapter da = new SqlDataAdapter("select * from Table where (A=@abc OR A=@bcd OR A=@cda) AND (B=@abc OR B=@bcd OR B=@cda) AND (C=@abc OR A=@bcd OR A=@cda) AND (C=@abc OR C=@bcd OR C=@cda) AND (D=@abc OR D=@bcd OR D=@cda)", cn);
DataSet ds = new DataSet();
da.Fill(ds, "stk");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "stk";

The problem is that if there No related string(@abc,@bcd,@cda) data is present in Column B(for Ex.), its not show the data in gridview.
i want the SQL statement which show data in gridview, if there No data present in any one column.
Posted
Updated 14-Dec-14 21:44pm
v2
Comments
Agent__007 15-Dec-14 3:13am    
I am not sure I get your issue, but I think you need to add SqlParameters and their values to your SqlDataAdapter instance. Refer: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter%28v=vs.110%29.aspx URL for details.
Praveen Kumar Upadhyay 15-Dec-14 3:17am    
I think your query is wrong. As you have mentioned that you have 4 columns and comparing with 3 string variable. As per the query you have mentioned, you condition is never gonna true, because 4 columns will never match with 3 Textbox variables. These variable will either match with A,B,C or B,C,D or any other 3 combination but will not match with all 4 columns.
Please correct me if I am wrong.
Member 10861672 15-Dec-14 6:31am    
Is there are no any query in SQL for that condition.
Praveen Kumar Upadhyay 15-Dec-14 6:39am    
There are condition, but before that look at your query. What you are specifying the condition is totally wrong.
You are checking that if column A is matching with any textbox, column B is matching with any Textbox, similarly for C and D. Now how come 3 Textbox can be compared for 4 column value and that too also with OR condition.


You please tell your scenario what you want to do
Shweta N Mishra 15-Dec-14 3:40am    
you need to elaborate your question details a little more, Put your table sample record and expected result in you question too.

Try this

SQL
SELECT D,W,X,Z,AA,AB,AC,AD
FROM ABC WHERE (D LIKE 'S.B.I.%' OR
D LIKE 'ATM%' OR
D LIKE 'BHILAI%' OR
D LIKE 'G.E.%') or(W LIKE 'S.B.I.%' OR
W LIKE 'ATM%' OR
W LIKE 'BHILAI%' OR
W LIKE 'G.E.%') or(X LIKE 'S.B.I.%' OR
X LIKE 'ATM%' OR
X LIKE 'BHILAI%' OR
X LIKE 'G.E.%') or(Z LIKE 'S.B.I.%' OR
Z LIKE 'ATM%' OR
Z LIKE 'BHILAI%' OR
Z LIKE 'G.E.%') or (AC LIKE 'S.B.I.%' OR
AC LIKE 'ATM%' OR
AC LIKE 'BHILAI%' OR
AC LIKE 'G.E.%')


Or looking at your column values if you know what those textboxes are going to contain then accordingly put them in condition, e.g. 1st textbox will have values to be searched in column D , 2nd text box value will have values to be searched for column W and so on

SQL
SELECT D,W,X,Z,AA,AB,AC,AD
FROM ABC WHERE (D LIKE 'S.B.I.%') AND (
W LIKE 'BHILAI%') 
and so on
 
Share this answer
 
SqlDataAdapter da = new SqlDataAdapter(@"select * from Table where (A=@abc OR A=@bcd OR A=@cda) AND (B=@abc OR B=@bcd OR B=@cda) AND (C=@abc OR A=@bcd OR A=@cda) AND (C=@abc OR C=@bcd OR C=@cda) AND (D=@abc OR D=@bcd OR D=@cda)", cn);
DataSet ds = new DataSet();
da.Fill(ds, "stk");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "stk";
 
Share this answer
 
use left outer join and fK for your tables.
 
Share this answer
 
Comments
Praveen Kumar Upadhyay 15-Dec-14 3:06am    
There is no join operation in the query if you see.
Member 10861672 15-Dec-14 3:20am    
sir! can u plz tell me the modified query of above...cz i don't know about left outer join and fK.
mr47_p 24-Dec-14 5:49am    
without join its not a proffesional work
Tomas Takac 15-Dec-14 3:24am    
How does it solve the problem?

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