我收到一個錯誤,該錯誤會引發與標題一樣的異常。林不知道是什麼問題每當嘗試將數據插入到數據庫時拋出異常錯誤
連接:
public void connectToDatabase()
{
connStrBuilder = new SqlConnectionStringBuilder();
connStrBuilder.DataSource = @"DESK-KEGRC\SQLSERVER";
connStrBuilder.InitialCatalog = "RentalDatabase";
connStrBuilder.IntegratedSecurity = true;
connection = new SqlConnection(connStrBuilder.ToString());
}
public void Insert(Car car)
{
try
{
string commandText = "INSERT INTO dbo.Cars(Brand, Name, Year) VALUES (@Brand, @Name, @Year)";
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.AddWithValue("@Brand", car.newBrand);
command.Parameters.AddWithValue("@Name", car.newName);
command.Parameters.AddWithValue("@Year", car.newRegisteredYear);
connection.Open();
command.ExecuteNonQuery();
}
catch
{
throw;
}
}
這是事件按鈕將數據插入到數據庫
private void InsertData(object sender, EventArgs e)
{
car = new Car();
car.newBrand = txtBrand.Text;
car.newName = txtName.Text;
car.newRegisteredYear = txtYear.Text;
dataBaseConnection.Insert(car);
}
錯誤:
System.NullReferenceException: Object reference not set to an instance of an object. at ConnectToDatabase.DatabaseConnection.Insert(Car car) in C:\Users\Kopalnia\Desktop\C#\Program\VehicleRental\VehicleRental\Vehicle Rental\ConnectToDatabase\DatabaseConnection.cs:line 46 at Vehicl_Rental.AddACar.InsertData(Object sender, EventArgs e) in C:\Users\Kopalnia\Desktop\C#\Program\VehicleRental\VehicleRental\Vehicle Rental\Vehicl Rental\AddACar.cs:line 31
當你追蹤它時 - 確切地說哪一行是錯誤的,那個時候所有項目的值是多少? – BugFinder