Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get value from database and display them in label, but the problem is that i am only geting the last inserted value i need to display all the values for every id..

What I have tried:

protected void getdata() {



       SqlConnection con = new SqlConnection(GetConnectionString());
       con.Open();
       SqlCommand cmd = new SqlCommand("SELECT Paid FROM Pay where StdId='" + editstdlbl.Text + "'", con);

       SqlDataReader reader = cmd.ExecuteReader();


       reader.Read();


       payment.Text = reader["Paid"].ToString();

       reader.Close();
       con.Close();


   }
Posted
Updated 7-May-20 12:56pm

1 solution

There are multiple issues with your code.

First and foremost; your code has an SQL Injection Vulnerability.
You should NEVER EVER create a query by concatenating commands and variables together. the proper thing to do is to use the SQL Parameter[^] collection to add variables to a command.
That chunk of code should look something like this
C#
SqlCommand cmd = new SqlCommand("SELECT Paid FROM Pay WHERE StdId=@StdId", con);
cmd.Parameters.AddWithValue("@StdId", editstdlbl.Text);
The second part is what you came here for; you are only getting the last value. Well, the problem is you are only reading one value; and what you need to do is go through all the values.
Typically this would be done with a WHILE block using the value from the Reader.Read() as the condition
C#
while (reader.Read()) {
	// get values and assign to variables
}
But in your case; every time the record is advanced, all you are going to do is to overwrite the value that you set on the prior record.

Unfortunately your question does not have enough detail on what you are trying to actually do with this information, so I cannot tell you what your next steps are going to be to get to where you want to go.
I have ideas on what you may want, some of which have you on the right track and others have you on the wrong track.

References:
MS Docs => SqlParameter Class[^]
MS Docs => SqlDataReader Class[^]
 
Share this answer
 
Comments
Jassom 7-May-20 20:04pm    
Thank you for your clear explanation.
Rajaa Khalifeh 8-May-20 11:15am    
thank you so much for your reply, my idea is that the paid column in database has more that 1 value so i need to get all the values for this id and replace them in label so after while what can i do
MadMyche 8-May-20 11:44am    
Did you want all those values added together (sum)?
Rajaa Khalifeh 9-May-20 22:02pm    
yes i need to get all these values for specific id and add them to gather , and iam very thankful for you help dear..
MadMyche 10-May-20 0:52am    
SELECT Sum(Paid) FROM Pay WHERE StdId = @StdId

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