Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have result

System.Data.SqlClient.SqlDataReader


Some help?

What I have tried:

private void ukupno_bez_pdv_roba()
       {
           SqlConnection con2 = new SqlConnection(cs);

           string sqlquery = ("select * from mp_racun_roba where tip_robe = 'Roba (Generalno)' and id=" + id_fakture);


           SqlCommand command = new SqlCommand(sqlquery, con2);
           con2.Open();
           SqlDataReader sdr = command.ExecuteReader();

           roba_bez_pdvTextBox.Text = sdr.ToString();
           con2.Close();
       }
Posted
Updated 1-Jun-18 7:55am
v2
Comments
F-ES Sitecore 1-Jun-18 4:24am    
That's not how you use SqlDataReader, look at the docs for an example

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader(v=vs.110).aspx

If you know your query can only return one row then select an explicit field ("select fieldname from ..." rather than "select * from ...") and use ExecuteScalar rather than ExecuteReader. Check the docs to see how to use that too.

C#
private void ukupno_bez_pdv_roba()
{
    using (SqlConnection con2 = new SqlConnection(cs))
    using (SqlCommand command = new SqlCommand("select YOUR_FIELD_NAME from mp_racun_roba where tip_robe = 'Roba (Generalno)' and id = @id", con2))
    {
        command.Parameters.AddWithValue("@id", id_fakture);
        
        con2.Open();
        using (SqlDataReader sdr = command.ExecuteReader())
        {
            if (sdr.Read())
            {
                roba_bez_pdvTextBox.Text = sdr.GetString(0);
            }
        }
    }
}

ADO.NET Overview | Microsoft Docs[^]
ADO.NET code examples | Microsoft Docs[^]


And once again, since you seem to keep forgetting this:
Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

Interactive SQL Injection demo[^]

Hacking is child's play - SQL injection with Havij by 3 year old[^]
 
Share this answer
 
Comments
Eric Lynch 31-May-18 16:10pm    
Sigh...forgetting SQL injection does seem to be a theme in these posts. After answering a previous post, I seconded your concerns...sad to see its ongoing :(
F-ES Sitecore 1-Jun-18 4:26am    
How do you know the code is liable to SQL injection? We can't see enough of it to know for sure.
Richard Deeming 1-Jun-18 7:17am    
Have you seen his(?) previous questions?

Even if you haven't, it's a safe bet that someone using string concatenation instead of parameters won't only be doing that with "safe" values.
F-ES Sitecore 1-Jun-18 8:19am    
Probably, but we still can't say for sure.
Richard Deeming 1-Jun-18 8:32am    
As I said, look at the OP's previous questions. There's a definite pattern!
Solved


SqlConnection con2 = new SqlConnection(cs);

           string sqlquery = ("SELECT SUM(isnull(cast(REPLACE(TRY_CONVERT(int,TRY_CONVERT(float,iznos_bpdv),1), '#,0.00','')AS decimal(10,2)),0.00)) as UKUPNObpdv," +
                                     " SUM(isnull(cast(REPLACE(TRY_CONVERT(float, TRY_CONVERT(float, pdv), 1), '#,0.00', '')AS decimal(10, 2)), 0.00)) as UKUPNOpdv," +
                                     " SUM(isnull(cast(REPLACE(TRY_CONVERT(float, TRY_CONVERT(float, iznos_sa_pdv), 1), '#,0.00', '')AS decimal(10, 2)), 0.00)) as UKUPNOsapdv" +
                                     " from mp_racun_roba" +
                                     " where tip_robe = 'Roba (Generalno)' and id_fakture =" + id_fakture
                             );


           SqlCommand command = new SqlCommand(sqlquery, con2);
               con2.Open();
               SqlDataReader sdr = command.ExecuteReader();

               if (sdr.Read())
               {

               roba_bez_pdvTextBox.Text = sdr["UKUPNObpdv"].ToString();
               roba_pdvTextBox.Text = sdr["UKUPNOpdv"].ToString();
               roba_sa_pdvTextBox.Text = sdr["UKUPNOsapdv"].ToString();

           }
               con2.Close();
 
Share this answer
 
Comments

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