Click here to Skip to main content
15,742,581 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
This is the output from SQL Stored Procedure.
employeeID ,Sum(Leave)
EID00 , 13
EID01 , 14
EID02 , 9

Below is the form that I want to create. So I want to put the value in the second column from SQL procedure into the textbox below in windows form. For example, let say an integer value of 13 from Sum(LEAVE) column and row of EID00 will be put in the the corresponding text box.
EmployeeID, SumOfLEave
E00 ,TextBOx
E01 ,TextBox
E02 ,TextBox

What I have tried:

I have got the output from SQL procedure. However, I don't know how to proceed to the next step which is put the value in text box of win form. Pls help me suggest on that. Thank you.
Posted
Updated 7-Sep-22 12:41pm
v4

Quote:
How to write C# code to retrieve value from SQL procedure and put it in text box in win form?

Advice : Stop combining little problems into super big problems.
Variables are the glue between parts of a program, so you have :
- Retrieve data from SQL Server to variables.
- Store contain of variable into textbox.
2 simpler separate problems, by checking variables, it is easy to know which part fails.
 
Share this answer
 
Use: SqlCommand.ExecuteScalar Method (System.Data.SqlClient) | Microsoft Docs[^]

Then:
C#
cmd.CommandText = "SELECT ... FROM ... WHERE EmployeeId = @EmpId;";  
int count = (int)cmd.ExecuteScalar();
TextBoxEmp01.Text = count.ToString();
 
Share this answer
 
Have you tried:
C#
MyTextBox.Text = resultFromSQL.ToString();
 
Share this answer
 
Comments
MK_VEEE 6-Sep-22 4:16am    
I haven't tried yet. I will try to find something similar. Could you help me guide with some sample code or similar program if possible? Thank you.
MK_VEEE 6-Sep-22 21:59pm    
@OriginalGriff Could you pls explain in depth more?
OriginalGriff 7-Sep-22 1:27am    
Explain what? Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!
MK_VEEE 8-Sep-22 1:38am    
Yes, sorry that I was not able to make the explanation very well. However, based on your suggestion, I have fulfil the purpose by using MyTextBox.Text = resultFromSQL.ToString(); Thank you so much.
OriginalGriff 8-Sep-22 1:48am    
You're welcome!

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