Last post Jan 13, 2020 08:35 PM by mgebhard
Jan 13, 2020 07:29 PM|rogersbr|LINK
Have a VB.net webforms app, am setting up the logic for the Gridview. But in the SQL table all columns have spaces so
Public Class ASTR9
Public Property Id As Integer ok
Public Property Plant Category as String fail
Public Property Plant Color as String fail
Public Property [Plant Color] as String fail
In other tables, the names would always have no space at all. It would be Plant_Category, etc. https://www.youtube.com/watch?v=ZUCsHxBEPeg&t=68s at 1:53 as a good example of whats being done
here. Does it matter that the name in the class isnt a literal match to whats in the SQL table's column name?
Jan 13, 2020 08:31 PM|Rion Williams|LINK
You can use a
ColumnAttribute to decorate your properties with names that corresponding to your database:
Public Class ASTR9
Public Property Id As Integer
Public Property PlantCategory as String
Public Property PlantColor as String
Properties cannot have spaces, so this attribute should help with binding the values appropriately if your populating query has spaces. Otherwise, you could just alias or rename your columns entirely (without spaces) to avoid the issue completely.
Jan 13, 2020 08:35 PM|mgebhard|LINK
Class properties names cannot contains spaces. See the VB.NET programming guide for property naming conventions.
If you are using EF and generating the classes then the code generator will use an underscore to represent space. There's also the column and table attributes which allow you to map a table column to a property.
Partial Public Class Enrollment
Public Property EnrollmentID As Integer
The EF 6 docs cover the details.
Does it matter that the name in the class isnt a literal match to whats in the SQL table's column name?
Ideally, column names match the property names but it is not required. Keep in mind, you can also alias the column names to match the property name.
SELECT [Column With a Space] AS ColumnWithASpace