I am having an app in visual c# ,, i need to retrieve the table values of coloumn name title corresponding to a certain user id in combobox on the same form . can anyone help me doing this ?

Hi,
If I understand your question the combo box will have the id in it (or maybe the user's name only) so you will get the selected item of the combo box and then use that value in the sql query. You can't use parameters for column names (unless I'm out of date, hope i'm not) so you need to build a query string that included the column as text.

Say for example your combo box has the selected value '001' and that is the name of the column in your table you would then do something like this:
string sql = "SELECT " + id + " FROM table WHERE... etc";

If you have something else as the combo values that relates to a user ID in a table then you'll have to do a another query before this one to extract the correct user id.
Note that as you aren't using parameters you will need to be careful about SQL injection. In other words, your column name variable that you add to the query must never be something enterd by the user as text.

Is that what you're after?

hey hericles .. that was so far helpfull ,, and i met ma result using a method fill combo and intializing it ,,,, may i know for can i use more than one sql queries in a single form or not ? i mean one for passing id from ma form 1 to form 2 and other for getting the value for combobox from table on same form 2 ?

but the connections r creating problm can u give me an example plz of using 2, 3 queries simultaneously

luk sir m quiet new to programming in visualc# .. i tel yu my problm ... its
that m having two tables named views and video ,, coulomn video_id is same in both ,, i tuk a combobox and filled it with the titles colmn corresponding to video table ,, now i need to retrieve in richtext box the u_id colmn entries in views table corresponding to that video_id i have olready retrieved from the video table ..... i'll be realy grateful if anyone can help me .... thankyu

To what i understand from your requirements following code may be helpful

private void richtxtbox(object sender, EventArgs e)
{
        //declare connection string 
        string stcon = "your connection string";

        //Storing the selected Combobox entry
        string combosl = comboBox1.SelectedItem.ToString();

        //Your sql query
        string sql="SELECT id FROM Video WHERE video_id="+combosl;

        //openning connection
        SqlConnection conn = new SqlConnection(connsql);
        conn.Open();


        SqlCommand comd = new SqlCommand(sql, stcon);

        richTextBox.Text = comd.ExecuteScalar().ToString();

        conn.Close();

 }

Edited 2 Years Ago by johnrosswrock

sir the code u gave me wil gv me video_id corresponding to the title that comes in combobox .. but i dont need that ,, i need user_id wich is common in both tables .... // actualy i elaborate ::::::

its that i want to find a user_id list in rich text box for a corresponging video_id ,, bt the problm is that i have a table video that is having video_id and title that is required for combobox and other table views that wil tel me u_id'z that viewed the title i selected from combobox ... please help me out .. i suppose it needs a property like inner join or so .... tel me hw to use that in visual c#

string sql="SELECT u_id FROM views WHERE video_id="+combosl;

This is all you have to change.

P.S:Please google your problems first and if you don't find any ans then ask

sir i olrdy googled it bt cant find an appropriate sol. to my problm ... chck i sent a snapshot of ma app

Edited 2 Years Ago by Amanpreet_1

Attachments Untitled.png 294.23 KB

I think it will be better to test your query in SQL first. You can do the JOIN logic in SQL and merely pass the string to SQL via a SQLCommand. Reading your previous comments led me to the conclusion that johnrosswrock's previous comment is what you need. You want to retrieve all the users that viewed the selected video? Is that correct?

Edited 2 Years Ago by Fenrir()

in the combobox m getting the title ,, with the secnd query on table video i cn get the videoid for corresponding selected itrem in combobox bt the u_id i need is in other table views ..... please try'a resolve ma conflict the video _id i retrieve in ma query is nt getting passed to another query for views table

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string myconnection = "connection string";
        MySqlConnection myConn = new MySqlConnection(myconnection);

        MySqlCommand SelectCommand = new MySqlCommand("select video_id from livestream.video where title ='" + channeltitle.Text + "' and active=1  ", myConn);

        MySqlDataReader myReader;


        try
        {
            myConn.Open();
            myReader = SelectCommand.ExecuteReader();

            while (myReader.Read())
            {


                string videoID = myReader.GetString("video_id");
                MessageBox.Show(videoID);

            }


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        myConn.Close();

    }

Edited 2 Years Ago by Amanpreet_1

Am i getting this right : you want the "video_id" returned by your reader so you can pass it into another query to retrieve the values? If that is the case you need to declare the variable outside your method so you can use it in another. So string videoID; needs to be moved outside the method and only assigned in the comboBox1_SelectedIndexChanged as follows videoID = myReader.GetString("video_id"); and then you can use it in your other query.

Edited 2 Years Ago by Fenrir()

thankyuu ,, that to very much extent resolved my problm ,,, but as i gave abv. the tabular data ,, on video_id has been viwed by many u_id'z so i need ol of them in ma richtext box bt only the id thts logged in is been shown ... array declaration is required or wat ???? guide please

use List<string> u_id = new List<string>() to create list of u_id and store the result of the query in it and then insert it into richtext box

Edited 2 Years Ago by johnrosswrock

sir the string i declared globally video_id to have it passed in next query when passed is tking a blank value vn passed .. and m nt being able to identify how to use this declaration of dynamic arrays in ma code ,, do help ...

You have the video ID so i assume your second query is just a standard select aswell. So when you're selecting you can just initialize another reader and loop through the User Id's you return. So as John suggested you can then create a list<string> outside your loop and add the items to it. Once your DataReader (While) has finished and the list is populated you can then assing the list to the RichTextbox as follows RichTextBox.Lines = myList.ToArray();

So it should look something like this

//Declare your list

List<string> list = new List<string>

while (myReader.Read()) 
{
string userID = myReader.GetString("user_id");
list.add(UserID);
}

RichTextBox.Lines = list.ToArray();

I hope this helps you

Edited 2 Years Ago by Fenrir()

by this m getting the entire list of u_id'z corresponding to any of my video_id'z .. my purpose is to find out only those id'z that correspond to a single combobox selected entry ...

PLEASE GUIDE A BIT MORE ,,

m getting ol the entries of that colmn name u_id in richtext box instead i jux want the u_id'z who viewd the video_id only they appear .. wat shud i write in the WHERE clause of the query .. do suggest please

error is comming , unknown dgdf in where clause where dgdf is 1st entry in my combobox ... m not gettn one point clr dat hw wil the video_id take the value of combobox selected item as combobox contains titles nt video_id'z .... // just suggest me wat to write for selecting the entries corresponding to a single video_id from u_id ... m hygh near to ma solution jux a bit of it is gettn hampered

string videoID = myReader.GetString("video_id"); this sould be in your combobox you select from by my understanding. Is that the physical ID? If you select from views based on the video ID then you should get the people who viewed that video. Otherwise when you populate the Video ID combobox and you're displaying the video name you will need to Set the value member and display member

can u just send me a syntax for inner join of three tables views , video and users taking inner join on video.video_id and views.video_id where video_id - videoID i olready retrieved ... please i guess m gettn mistake in ma syntax near where clause of query

Inline Code Example Here MySqlCommand SelectCommand = new MySqlCommand("select u_id " + "FROM livestream.views " + " INNER JOIN livestream.video " + " ON video.video_id = views.video_id" +"WHERE video_id ="+ videoID, myConn); lukk whr m i makin mistake ,, compiler is showin me error in " of while clause in query

This question has already been answered. Start a new discussion instead.