在 Visual Studio 2019 中,类的 C# 实例返回空值,我不知道为什么
我正在开发一个 C# 表单应用程序。当我尝试使用从文本框中引入的值修改/更新现有对象的值时发生错误。我确信在构造函数中传递的数据类型是正确的。
在第二个屏幕截图中可以看到一条错误消息:
输入的字符串格式不正确
我在下面粘贴了 Holiday 对象的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AnthonyQuinnAssignmentObjectOrientatedProgram
{
public class Holiday
{
public int HolidayNo { get; set; }
public string Destination { get; set; }
public decimal Cost { get; set; }
public DateTime DepartureDate { get; set; }
public int NoOfDays { get; set; }
public int Available { get; set; }
public Holiday() { }
public Holiday(int holidayNo,string destination,decimal cost,DateTime departureDate,int noOfDays,int available)
{
HolidayNo = holidayNo;
Destination = destination;
Cost = cost;
DepartureDate = departureDate;
NoOfDays = noOfDays;
Available = available;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace AnthonyQuinnAssignmentObjectOrientatedProgram
{
public class HolidayDB
{
public HolidayDB()
{
}
public static List<Holiday> GetHoliday()
{
SqlConnection connection = TravelDB.getconnection();
List<Holiday> holidays = new List<Holiday>();
try
{
connection.Open();
string select = "SELECT *" + " FROM tblHoliday";
SqlCommand selectCommand = new SqlCommand(select,connection);
SqlDataReader holidayReader = selectCommand.ExecuteReader();
while (holidayReader.Read())
{
Holiday holiday = new Holiday();
holiday.HolidayNo = Convert.ToInt32(holidayReader["HolidayNo"]);
holiday.Destination = holidayReader["Destination"].ToString();
holiday.Cost = decimal.Parse(holidayReader["Cost"].ToString());
holiday.DepartureDate = DateTime.Parse(holidayReader["DepartureDate"].ToString());
holiday.NoOfDays = Convert.ToInt32(holidayReader["NoOfDays"]);
holiday.Available = Convert.ToInt32(holidayReader["Available"]);
holidays.Add(holiday);
}
return holidays;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message + "\n\n" + ex.GetType().ToString() + "\n\n" + ex.StackTrace,"Exception");
}
finally
{
connection.Close();
}
return null;
}
public static bool UpdateHoliday(Holiday oldHoliday,Holiday newHoliday)
{
SqlConnection connection = TravelDB.getconnection();
string updateStatment =
"UPDATE tblHoliday SET " +
"Destination = @NewDestination," +
"Cost = @NewCost" +
"DepartureDate = @NewDepartureDate" +
"NoOfDays = @NoOfDays" +
"Availability = @Availability" +
"WHERE Destination = @OldDestination" +
"AND Destination = @OldDestination" +
"AND Cost = @OldCost" +
"AND DepartureDate = @OldDepartureDate" +
"AND NoOfDays = @OldNoOfDays " +
"AND Availability = @OldAvailability";
SqlCommand updateCommand = new SqlCommand(updateStatment,connection);
updateCommand.Parameters.AddWithValue("@NewDestination",newHoliday.Destination);
updateCommand.Parameters.AddWithValue("@NewCost",newHoliday.Cost);
updateCommand.Parameters.AddWithValue("@NewDepartureDate",newHoliday.DepartureDate);
updateCommand.Parameters.AddWithValue("@NewNoOfDays",newHoliday.NoOfDays);
updateCommand.Parameters.AddWithValue("@NewAvailability",newHoliday.Available);
try
{
connection.Open();
int count = updateCommand.ExecuteNonQuery();
if (count > 0)
return true;
else
return false;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message + "\n\n" + ex.GetType().ToString() + "\n\n" + ex.StackTrace,"Exception");
}
finally
{
connection.Close();
}
return false;
}
public static bool AddHoliday(Holiday holiday)
{
SqlConnection connection = TravelDB.getconnection();
string insertStatment =
"INSERT tblHoliday" +
"(HolidayNo,Destination,Cost,DepartureDate,NoOfDays,Available)" +
"VALUES (@HolidayNo,@Destination,@Cost,@DepartureDate,@NoOfDays,@Available)";
SqlCommand insertCommand = new SqlCommand(insertStatment,connection);
insertCommand.Parameters.AddWithValue("@HolidayNo",holiday.HolidayNo);
insertCommand.Parameters.AddWithValue("@Destination",holiday.Destination);
insertCommand.Parameters.AddWithValue("@Cost",holiday.Cost);
insertCommand.Parameters.AddWithValue("@DepartureDate",holiday.DepartureDate);
insertCommand.Parameters.AddWithValue("@NoOfDays",holiday.NoOfDays);
insertCommand.Parameters.AddWithValue("@Available",holiday.Available);
try
{
connection.Open();
int count = insertCommand.ExecuteNonQuery();
if (count > 0)
return true;
else
return false;
}
catch (SqlException ex)
{
throw ex;
// MessageBox.Show(ex.Message + "\n\n" + ex.GetType().ToString() + "\n\n" + ex.StackTrace,"Exception");
}
finally
{
connection.Close();
}
}
public static bool DeleteHoliday(Holiday holiday)
{
SqlConnection connection = TravelDB.getconnection();
string deleteStatement =
"DELETE FROM tblHoliday" +
"WHERE HolidayNo = @HolidayNo" +
"AND Destination = @Destination" +
"AND Cost = @Cost" +
"AND DepartureDate = @DepartureDate " +
"AND NoOfDays = @NoOfDays" +
"AND Available = @Available";
SqlCommand deleteCommand = new SqlCommand(deleteStatement,connection);
deleteCommand.Parameters.AddWithValue("@HolidayNo",holiday.HolidayNo);
deleteCommand.Parameters.AddWithValue("@Destination",holiday.Destination);
deleteCommand.Parameters.AddWithValue("@Cost",holiday.Cost);
deleteCommand.Parameters.AddWithValue("@DepartureDate",holiday.DepartureDate);
deleteCommand.Parameters.AddWithValue("@NoOfDays",holiday.NoOfDays);
deleteCommand.Parameters.AddWithValue("@Available",holiday.Available);
try
{
connection.Open();
int count = deleteCommand.ExecuteNonQuery();
if (count > 0)
return true;
else
return false;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message + "\n\n" + ex.GetType().ToString() + "\n\n" + ex.StackTrace,"Exception");
return false;
}
finally
{
connection.Close();
}
}
}
}