Yobi Woo
Yobi Woo

Reputation: 49

SQL Select statement not working properly

I facing some problem in SQL query.I try to use select result in other select query to get the result but its not working. Can anyone guide me. Below is the code i use in WCF.Thank u very much.

SqlCommand command = new SqlCommand("select tName from dbo.tblBankBranch where nBankID=(select nID from dbo.tblBankBranch where tState='" + tBank + "')", con);
SqlDataReader reader = command.ExecuteReader();

Upvotes: 0

Views: 1126

Answers (1)

RichardTheKiwi
RichardTheKiwi

Reputation: 107826

I'm guessing that you actually get an error that is caught and ignored somewhere. The SQL statement looks fishy.

select tName
from dbo.tblBankBranch
where nBankID=(select nID from dbo.tblBankBranch where tState='{tbank}')

It will only work if there is a single branch in the state, otherwise you will get an error.

Try writing it as a JOIN. Ignore that, why have tblBankBranch in there twice?

select tName
from dbo.tblBankBranch
where tState='{tbank}'

If the fields nBankID and nID are really different in the single table, and you do want to link them in that way, then the JOIN form is

select A.tName
  from dbo.tblBankBranch A
  join dbo.tblBankBranch B
    on A.nBankID=b.nID AND b.tState='{tbank}'

I'll leave it to you to flatten the statements for use in C#.

Upvotes: 2

Related Questions