1

I am using C#(asp.net). I have two tables(data and details) in a same database.

Table "data"

id | chap | unit |
  ----------------
  1| chap1|unit1 |
  2| chap2|unit2 |
  3| chap3|unit3 |

Table "details"

id| code| num |
----------------
 1|abc  |2    |
 2|efg  |3    |
 3|hij  |1    |

Now I want to fetch a value from "num" where code="efg" (in table "details"). And use the same value (3) to fetch data from table "data" by id. I am using this code.

OleDbConnection conn = new OleDbConnection(*** ...... *****); 
       OleDbCommand cmd;
       OleDbDataReader reader;
        String query = String.Format("select num from details where code="efg");
        cmd = new OleDbCommand(query, conn);
        reader = cmd.ExecuteReader();
        int num = int.Parse(reader.GetValue(0).ToString());

        query = String.Format("select chap from data where id={0}",num);
         cmd = new OleDbCommand("select lesson from data where id=3", conn);
        reader = cmd.ExecuteReader();


        Label1.Text = reader.GetValue(0).ToString();

But it shows error. It shows "No data exists for the row/column."

0

2 Answers 2

2

You can use

SELECT d.chap, d.unit
FROM data d INNER JOIN details de
    ON d.id = de.num
WHERE de.code = 'efg'

or

SELECT d.chap, d.unit
FROM data d INNER JOIN details de
    ON d.id = de.num
   AND de.code = 'efg'

More: if you're using SQL-Server, use SqlConnection in place of OleDbConnection.
More: don't format your query joining strings, numbers, dates and so on; use SqlParameter so you don't have to worry about types and formatting!!

Sign up to request clarification or add additional context in comments.

2 Comments

Actually i m using Microsoft access for database, that's why I am using 'OleDbConnection'. Sorry I mistakely tag sql server.
@Viktor: did my answer solved your problem? If so you should accept it if you think it's fine.
-1

Or you can use a subquery

SELECT * FROM data INNER JOIN 
    (SELECT num FROM details WHERE code='efg') det
ON data.id = det.num

2 Comments

This query is exactly the same as both of the two you provided in your answer, when it comes to execution, due to the Query Optimizer. Non-correlated is the keyword here. In fact, we have provided three different ways to make SQL perform the exact same task (execution plan can confirm this). Downvoter probably doesn't have a clue about how the optimizer works.
alex, what I wanted to show you is not that query itself is wrong, nor that query optmizer does not translate it in my same query; I'm sure you understand that a newbie like OP must be shown simpler and default way to write queries, because he is not able to understand what an execution plan is or how query optiomizer works :) Do you agree with me?

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.