Click here to Skip to main content
15,881,381 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one form in which i have one label and one button and one datagridview control.
I want to retrive the records of "EmpName" coloumn of my emp table on that label.I also want that when my form is loaded the 1st record of my EMpName column is displayed on the Label and when i click on button that name is stored in datagrid view in name column and at the same time 2nd record EMpName from my database is displayed on that label and 1st record is invisible from label control.. how i do this please help me give me some code for this... as soon as possible..plss..!
Posted
Comments
Richard MacCutchan 13-Mar-15 4:38am    
What have you tried? Where are you stuck? Please do not expect other people to do your work for you.
Member 11057488 13-Mar-15 5:21am    
I use this code and it works properly but its just for single record bt i have multiple records in my column so i dont know how to do it for multiple recordds
SqlConnection con = new SqlConnection("Data Source=USER;Initial Catalog=emp;Integrated Security=True");
SqlCommand cmd;
con.Open();

string str = "select emp_name from Emp_table";
cmd = new SqlCommand(str, con);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
label1.Text = reader["emp_name"].ToString();
radioButton1.Text = reader["emp_name"].ToString();
radioButton1.Visible = true;
con.Close();
Richard MacCutchan 13-Mar-15 5:38am    
You must read all the records returned by the SqlDataReader.
Member 11057488 13-Mar-15 6:23am    
But I want to display all records on single label one by one after clicking the button means when the form is loaded the first record is shown on label bt when i click on button i want that another record display on the label the records displayed randomly one after another
Richard MacCutchan 13-Mar-15 6:36am    
Then, as I said, you must read the records one by one from the datareader. Go to the documentation to see how to do it.

1 solution

I would suggest you to run a count, if your emp_id is integer. So, you will fetch the record only for one employee.

Your query would look something like...
C#
string str = "select emp_name from Emp_table WHERE emp_id = @emp_id";
cmd = new SqlCommand(str, con);
cmd.Parameters.Add("@emp_id", empIdCount);

Define empIdCount = 1 initially and then inside button click increament it to 1. So, you will get the next Id. Thus, when you run the query, you will retrieve only one record and then just take the emp_name and assign it to the Label. So, Label will show new emp_name every time when you click the Button.
 
Share this answer
 

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