Skip to content Skip to sidebar Skip to footer

How To Put A Concatenated Data From Database To A Label

My table goes like this: | ID | FNAME | LNAME | My code goes like this: cmd.CommandText = 'SELECT * FROM members WHERE ID = '' & Label18.Text & ''' dreader = cmd

Solution 1:

cmd.CommandText = "SELECT CONCAT(fname,' ',lname) FROM members WHERE ID = '" & Label18.Text & "';"Label3.Text = cmd.ExecuteScalar

Note : This makes sense when the select returns a single Cell value

ExecuteScalar() in SqlCommand Object is used for get a single value from Database after its execution. It executes SQL statements or Stored Procedure and returned a scalar value on first column of first row in the Result Set. If the Result Set contains more than one columns or rows , it takes only the first column of first row, all other values will ignore. If the Result Set is empty it will return a Null reference.

Solution 2:

The easiest way is to just return an additional column from the database itself.

cmd.CommandText = "SELECT *, FullName = fname + ' ' + lname FROM members WHERE ID = '" & Label18.Text & "'"
dreader = cmd.ExecuteReader()
dreader.Read()
Label3.Text = dreader("FullName").ToString()

Post a Comment for "How To Put A Concatenated Data From Database To A Label"