There are many blanks in table and I would like for those blanks (DBNull) in label values to be ignored and presented as blank values. Also with adding and changing table through web form it would be hard to control every single input (23 columns of data).
Is it possible to do that with FOR statement? I've tried by didn't had any success. I could do that with IF statement but I would have to do that for every single of 23 labels.
Here is the code I have so far (thanks to people on this site).
Protected Sub TextBox1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Load
Using sqlconn = New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=KLIJENTI;Integrated Security=True")
Using sqlcmd = New SqlCommand("Select NAZIV,PUN_NAZIV, ADRESA, GRAD, OPSTINA, PRAVNA_FORMA,DAT_REG, TRAJANJE, MATICNI, PIB, SIFRA_DELATNOSTI, NAZIV_DELATNOSTI, VELICINA, TEKUCI, RZZO, PIO From Baza Where SIFRE = @SIFRE", sqlconn)
sqlcmd.Parameters.AddWithValue("@SIFRE", TextBox1.Text)
sqlconn.Open()
Dim result = sqlcmd.ExecuteReader()
While (result.Read())
If result IsNot Nothing Then
Label1.Text = result("NAZIV")
Label2.Text = result("PUN_NAZIV")
Label3.Text = result("ADRESA")
Label4.Text = result("GRAD")
Label5.Text = result("OPSTINA")
Label6.Text = result("PRAVNA_FORMA")
Label7.Text = result("DAT_REG")
Label8.Text = result("TRAJANJE")
Label9.Text = result("MATICNI")
Label10.Text = result("PIB")
Label11.Text = result("SIFRA_DELATNOSTI")
Label12.Text = result("NAZIV_DELATNOSTI")
Label13.Text = result("VELICINA")
Label14.Text = result("TEKUCI")
Label15.Text = result("RZZO") ' PROBLEM DBNull.Value
Label16.Text = result("PIO") ' PROBLEM DBNull.Value There are more labels below but I am stuck here
Else
TextBox1.Focus()
End If
End While
End Using
End Using
TextBox1.Text = ""
End Sub
Also there are blanks all over just in this instance they are in 15th and 16th label.
Thanks.
Simply add this function to your class:
Private Shared Function GetTextOrEmpty(reader As IDataReader, fieldName As String)
Dim ordinal = reader.GetOrdinal(fieldName)
Return If(reader.IsDbNull(ordinal), String.Empty, reader.GetString(ordinal))
End Function
And then, in your For
loop:
'Other labels (...)
Label14.Text = GetTextOrEmpty(result, "TEKUCI")
Label15.Text = GetTextOrEmpty(result, "RZZO")
'Other labels (...)