❔ Select statement to view in html
Good morning just had a quick question about how to be able to select the correct view
Will I need to make a separate function or keep it all in getcars
public Carinformation[] GetCars()
{
using SqlConnection con = new SqlConnection(connectionString);
con.Open();
using SqlCommand command = con.CreateCommand();
command.CommandText =
"select * from Carinformation";
var reader = command.ExecuteReader();
var list = new List<Carinformation>();
while (reader.Read())
{
var carinformation = new Carinformation
{
ID = reader.GetInt32("ID"),
Title = reader.GetString("Title"),
Price = reader.GetDecimal("Price"),
Miles = reader.GetInt32("Miles"),
VinNumber = reader.GetString("VinNumber"),
MilesPerGallon = reader.GetDecimal("MilesPerGallon"),
Engine = reader.GetString("Engine"),
Color = reader.GetString("Color"),
DriveType = reader.GetString("DriveType"),
Transmission = reader.GetString("Transmission"),
Year = reader.GetInt32("Year")
};
if (reader["Image"] != DBNull.Value) // add this check for DBNull
{
carinformation.Image = (byte[])reader["Image"];
}
list.Add(carinformation);
}
return list.ToArray();
}
2 Replies
This is my models
public class Carinformation
{
public int ID { get; set; }
public string Title { get; set; }
public decimal? Price { get; set; }
public int? Miles { get; set; }
public string? VinNumber { get; set; }
public decimal? MilesPerGallon { get; set; }
public string? Engine { get; set; }
public string? Color { get; set; }
public string? DriveType { get; set; }
public string? Transmission { get; set; }
public byte[]? Image { get; set; }
public int? Year { get; set; }
}
public class CarImage
{
public int ID { get; set; }
public int CarInformationID { get; set; }
public byte[]? Image { get; set; }
}
My tables are getting inserted correctly
Ex: Car Id is 1 will have all the details
Car Id 1 in carimage would have 3 rows for the 3 uploaded images
Now when joining how could I show them all in 1 row?
or do i need to make separate queries because I can't figure out a way to show them in my html
Note: My select statement is obviously wrong just wanted to keep it simple and also I dont have image from carimage in this code as well.
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.