Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Need string if har row to show value, else to be ----

I try this
What I wrong?

What I have tried:

C#
string redni_broj = "";
           using (SqlConnection openCon8 = new SqlConnection(Con))
           {
               SqlDataReader reader8;
               SqlCommand cmd8 = new SqlCommand();
               cmd8.CommandText = "select FORMAT(redni_broj, '00000') from mp_faktura_lista where id=" + id_fakture;
               cmd8.Connection = openCon8;
               openCon8.Open();
               reader8 = cmd8.ExecuteReader();
               if (reader8.HasRows)
               {
                   while (reader8.Read())
                   {
                       redni_broj = reader8[0].ToString();
                   }
               }
               else
               {
                   redni_broj = "-----";
               }
               openCon8.Close();
           }
Posted
Updated 2-Nov-19 3:56am
Comments
MadMyche 2-Nov-19 8:47am    
Please update your question using the Improve Question widget to add in:
1. What data type is redni_broj? What is the actual value?
2. What does this function currently return? Are there errors?
Goran Bibic 2-Nov-19 8:59am    
1. What data type is redni_broj? int
What is the actual value? acutal is null
2. What does this function currently return? curently if is null (empty), if have value exmp 1 (write 00001)
Are there errors? No have errors

First off, don't do it like that: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Second, you don't need a while - your code will just set it to the last value in the reader, so you are probably better off withj an if instead:
C#
reader8 = cmd8.ExecuteReader();
if (reader8.Read())
{
   redni_broj = reader8[0].ToString();
}
else
{
   redni_broj = "-----";
}
Or even just
C#
reader8 = cmd8.ExecuteReader();
redni_broj = reader8.Read() ? reader8[0].ToString() : "-----";
 
Share this answer
 
Comments
Goran Bibic 2-Nov-19 8:57am    
reader8 = cmd8.ExecuteReader();
redni_broj = reader8.Read() ? reader8[0].ToString() : "-----";

This dont work result is null
OriginalGriff 2-Nov-19 9:08am    
No it isn't: check in the debugger, and you'll see.

I'm guessing it's either throwing an exception somewhere in your code and you are missing that - but I can't run your code with your data to check.

What does the debugger show you? And did you fix the SQL Injection problem through the whole of your app?
Goran Bibic 2-Nov-19 9:23am    
If put value in db 1 result is 00001
If put value ind db null result is empty (need to be -----)
OriginalGriff 2-Nov-19 9:26am    
And what does the debugger show you is happening?
Goran Bibic 2-Nov-19 9:31am    
I am solve

reader8 = cmd8.ExecuteReader();
while (reader8.Read())
{
if (reader8[0] != null && reader8[0] != DBNull.Value)
{
redni_broj = reader8.GetString(0);
}
else
{
redni_broj = " -----";
}
}
openCon8.Close();

And work

First and Foremost
This code is susceptible to SQL Injection. You should NEVER EVER put together a query by splicing user input in.
The proper method to add in the values is to use Parameters. Here is how this would look for your command, with some code reorganization as well
C#
string qry = "SELECT Format(redni_broj, '00000') FROM mp_faktura_lista WHERE id= @id";
SqlCommand cmd8 = new SqlCommand(qry, openCon8);
cmd8.Parameters.AddWithValue("@id", id_fakture);
SqlDataReader and ExecuteReader
Your code is designed to return one and only one value; there is no reason to utilize this method, rather you should be using the ExecuteScalar() method
C#
openCon8.Open();
var result = cmd8.ExecuteScalar();
if (result != null) { redni_broj = result.ToString(); }
else { redni_broj = "-----"; }
openCon8.Close();
My personal preferences
I would recommend not to use the Format() function in SQL. The reason is that the actual format that is being shown is a Presentation Layer concern and should not be in the business logic. Another valid reason is that sting functions within SQL is not as efficient as doing in application code.
What I would do is just to return the value as is (or null) and use application code for the formatting of it. You are already calling the ToSting() method, so you may as well utilize its overload method.

References:
SqlCommand.ExecuteScalar Method (System.Data.SqlClient) | Microsoft Docs[^]
 
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