Click here to Skip to main content
15,884,083 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi all,

How do i retrieve the ID value of a specific column? Lets say i have a database called user.db with two tables, one called check_details and check_pins.

lets say check details has the following columns: x_id, username, email, fullname, department

and the second table has the following columns: id, primarypin, secondarypin

Now i run an application that pulls the users 'username' from his PC and queries sql for all the columns data.

The problem i have is that i don't know the 'username's' id so i cant query both tables for the data.

How would i obtain both tables's id and x_id?

C#
        public EQData()
        {
            SQLConnect sqlcon = new SQLConnect();
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("select x_id, fullname, email, name, primarypin, secondarypin FROM cas_user_ext, cat_validation WHERE x_id = @key AND id = @key",
                                                                        sqlcon.Connection);
            myCommand.Parameters.AddWithValue("@key", "501"); 
            myReader = myCommand.ExecuteReader();
                        
            while (myReader.Read())
            {
                x_id = myReader["x_id"].ToString();
                fullname = myReader["fullname"].ToString();
                email = myReader["email"].ToString();
                name = myReader["name"].ToString();
                primarypin = myReader["primarypin"].ToString();
                secondarypin = myReader["secondarypin"].ToString();
            }
            sqlcon.Close();
        }
    }
}


This is the class i have created that's working but only if i manually add the @key value. Any assistance would be appreciated. I have read up on the identity commands but i don't know how to implement it to my code.
Posted
Comments
Pheonyx 15-Sep-14 8:52am    
This is a repost of your earlier question, which you should not do. It can be considered abusive/spamming you should instead update your existing question with any new information/further explanations.

That aside, what relationship do you have defined between your two tables? because from what you have written in both your questions it is unclear. I am guessing that the x_id from the user table is the same as the id value in the second table?
Ivan Lubbe 15-Sep-14 10:25am    
I apologize, i was not getting any responses so i assumed the structuring of my question was bad and tried to improve on it. I'm not familiar with all the terms and hence cant really structure my questions well. Well the value of x_id and id are exactly the same i assume they are linked somehow? I'm not too familiar with SQL in general been doing some c# for round about a month now too.
Sinisa Hajnal 15-Sep-14 9:22am    
Assuming x_id and id are foreign key columns (or at least contain same data, which it looks like by your WHERE clause), you could change your query to inner join and get everything neatly bound together.

1 solution

Change your query to
SQL
select x_id, fullname, email, name, primarypin, secondarypin 
FROM cas_user_ext cue 
    INNER JOIN cat_validation cv ON cue.x_id = cv.id
WHERE
    cue.username = @key 


Change inner to left join if there are users without pins :)


If this helps, please take time to accept the solution. Thank you.
 
Share this answer
 
v3
Comments
Ivan Lubbe 15-Sep-14 10:30am    
I will try this :) the cue and cv are a sort of alias for the tables? I have actually seen a SQL view but at that time it wasn't very clear.
Sinisa Hajnal 16-Sep-14 2:01am    
Yes, they are aliases. In this case (since the fields are named differently) it doesn't matter, but for complex queries with multiple tables and linking fields it makes it easier to qualify fields then retyping table name.
Ivan Lubbe 16-Sep-14 3:02am    
Thank you for the assistance Sinisa! is it possible to put the code into multiple lines? I have my query in 2 lines but the lines are a bit long and it looks messy.
Sinisa Hajnal 16-Sep-14 3:13am    
c#? Yes, it is possible. Start the string with "@" sign. String multiline = @" type between quotes however much text here in multiple lines"

It would be better to put your SQL code in stored procedure and call that with parameters rather then concatenating SQL in code.
Ivan Lubbe 16-Sep-14 3:27am    
Well i am already using it within class. Would that be the same as a stored procedure? Sorry since i am self taught and only been working with c# and SQL for a month. Hence i'm not too familiar with all the terms.

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