'Getting somewhat confused at this point folks
Am currently looking to use this code to display vehicle information in a data grid view in visual studio 2022 using c# :-
connStr = @"Data Source = .\sqlexpress; Initial Catalog = TDsSalesnService; Integrated Security = true";
// Setting up data adapter for datagridview display
sqlVehicleDetails = @"SELECT M.MakeName AS Make, ML.ModelName AS Model, V.Registration AS Reg, V.VINNum, V.PriceCost, V.PriceSell, V.Colour, VT.VehicleTypeDesc AS Type, V.Transmission, V.EngineSize, V.FuelType, V.Mileage, V.Condition, V.VehicleYear AS Year, V.Availableness AS Availability
FROM Vehicle V
JOIN VehicleType VT ON V.VehicleTypeID = VT.VehicleTypeID
JOIN Model ML on V.ModelID = ML.ModelID
JOIN Make M on ML.MakeID = M.MakeID
ORDER BY M.MakeName, ML.ModelName, VT.VehicleTypeDesc, V.VehicleYear ASC";
conn = new SqlConnection(connStr);
cmdVehicleDetails = new SqlCommand(sqlVehicleDetails, conn);
daVehicleDetails = new SqlDataAdapter(sqlVehicleDetails, conn);
cmdBVehicleDetails = new SqlCommandBuilder(daVehicleDetails);
daVehicleDetails.FillSchema(dsTDsSalesnService, SchemaType.Source, "Vehicle");
daVehicleDetails.Fill(dsTDsSalesnService, "Vehicle");
This works fine. The issue comes when I go to write records to the database and it's saying it doesn't accept null values for the first field m.makename. I was hoping to use and call the vehicle table using this code so i can write new records in the correct format :-
sqlVehicle = @"SELECT * from Vehicle";
daVehicle = new SqlDataAdapter(sqlVehicle, connStr);
cmdBVehicle = new SqlCommandBuilder(daVehicle);
daVehicle.FillSchema(dsTDsSalesnService, SchemaType.Source, "Vehicle");
daVehicle.Fill(dsTDsSalesnService, "Vehicle");
But keeps throwing error about the tables used not being unique. I truth I would need each line explained as to what is going on so i can map in my head how the process is functioning as I don't think an just understanding it tbh. Any help would be appreciated.
So the error reads System.ArgumentException:
'These columns don't currently have unique values.'
and it points to the daVehicle.FillSchema(dsTDsSalesnService, SchemaType.Source, "Vehicle"); line.
What am basically trying to do is have the datadgrid view display the vehicle details formatted using the first block of code to make it more legible to the user, but when I go to add records to the database I want to use it's raw format using the second block of code without utilizing the extra naming conventions and there's a extra field there that cause additional problems writing to the db even if i don't use the 2nd block of code.
I can use the second block of code as it for display and writing to the db but it's now useful to the user, looks awful and very hard to reads as it's codes in place of the fields instead of proper names.
datagridview using 1st block of code only
datagridview using 2nd block of code only
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
