Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
I have two Tables Named Output and Activities. The Output table contains Output_ID (Int) as a primary key and Output (Varchar(Max)), The Activities table contains Activity_ID (Int) as a primary key, Output_ID (Int) as foreign key and Activity (Varchar) it self. I used one to many relation ship between these tables where an output can have multiple activities but an activity can only have only one output.

The thing i want your help with is How can i retrieve the all activities in different textboxes of an output based on Output_ID? as far i am quite new to SQL Server i will be glad if you help me.

Note : I tried this code, it works with SQL Server Query but not with visual studio c#.

SQL
SELECT * FROM ActivitesTBL


Thanks in advance

Here is what i have done.

C#
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Joining ;Persist Security Info=True;User ID=sa;Password=*********");
            string Query = "SELECT * FROM ActivityTBL WHERE Output_ID = '"+Outputcmbx.Text+"';".ToString();



            SqlCommand cmd = new SqlCommand(Query, conn);
            SqlDataReader dr;
            try
            {
                conn.Open();
                dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    string sActivity = dr.GetString(2);

                    FirstActivitytxt.Text = sActivity;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
Posted
Updated 6-Nov-13 23:04pm
v5
Comments
CodeBlack 7-Nov-13 4:44am    
I think first of all you should learn 'How to retrieve data from SQL'. Below link will be helpful for that :

http://www.codeproject.com/Articles/4416/Beginners-guide-to-accessing-SQL-Server-through-C
[no name] 7-Nov-13 4:51am    
Thank You.
Rak_Avasthy. 7-Nov-13 4:49am    
you need c# code or sql query for that??.... and this is a query.. it does not run in visual studio, you have to meke an sql connection and sql command to execute this command
[no name] 7-Nov-13 4:51am    
I did that i need to load the activities in two text boxes that is it!

Sampath Lokuge 7-Nov-13 4:50am    
Is this asp.net webform,MVC or win App ?

1 solution

Hi Naser,

From your updated question, you retrieved only one string and moved to FirstActivityTxt.text,
you missed to read second string... so try this

C#
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Joining ;Persist Security Info=True;User ID=sa;Password=*********");
            string Query = "SELECT * FROM ActivityTBL WHERE Output_ID = '"+Outputcmbx.Text+"';".ToString();
 

 
            SqlCommand cmd = new SqlCommand(Query, conn);
            SqlDataReader dr;
            try
            {
                conn.Open();
                dr = cmd.ExecuteReader();
 
                while (dr.Read())
                {
                    string sActivity = dr.GetString(2);
		    string poorActivity = dr.GetString(1); //Column ordinal number comes here
                    FirstActivitytxt.Text = sActivity;
	            PoorActivitytxt.Text = poorActivity; //Replace PoorActivitytxt name with your textbox name
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


I think now it would work....

Regards,
RK
 
Share this answer
 
Comments
[no name] 7-Nov-13 5:26am    
Activity is a field the rest Like Poor activity or best activity they are not field of a table but the records entered under the Activity column. It didn't worked.
♥…ЯҠ…♥ 7-Nov-13 5:31am    
Update your question with table schema, output after executing the query and desired output that you want....
[no name] 7-Nov-13 5:48am    
How to post a picture here?
♥…ЯҠ…♥ 7-Nov-13 5:54am    
You can just paste table schema and table output it as text in the question itself
[no name] 7-Nov-13 6:06am    
Thank you! i did solved it my self in a bit difficult way. called manual programming XD :)

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