I'm having problems retrieving values from the SQL database in visual studio. I have columns(GuestName, GuestPassportNo, GuestCountry, RoomID, HotelPackageID) in the GUEST table where HotelPackageID contains a NULL value while the rest are with values. I'm always thrown into the else statement(no record found) when HotelPackageID is NULL. Anyone have a solution, please? Here is my code:
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "SELECT Guest.GuestPassportNo,
Guest.GuestName,
Guest.GuestCountry,
Guest.RoomID,
Room.RoomDescription,
Room.RoomPrice,
HotelPackages.PackageName,
HotelPackages.PackagePrice ";
strCommandText += " FROM Guest, Room, HotelPackages";
strCommandText +=" WHERE Guest.RoomID=Room.RoomID
AND Guest.GuestPassportNo=@guestpno
AND HotelPackages.PackageID=Guest.HotelPackageID
AND Guest.GuestCountry=@guestcountry;
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
cmd.Parameters.AddWithValue("@guestpno", txtPassportNo.Text);
cmd.Parameters.AddWithValue("@guestcountry", txtGuestCountry.Text);
try
{
myConnect.Open();
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
txtGuestName.Text = reader["GuestName"].ToString();
txtRoomDescription.Text = reader["RoomDescription"].ToString();
txtRoomPrice.Text = reader["RoomPrice"].ToString();
txtHotelPackage.Text = reader["PackageName"].ToString();
txtPackagePrice.Text = reader["PackagePrice"].ToString();
MessageBox.Show("Guest Record Found!");
}
else
{
Messagebox.Show("No record Found");
}
reader.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.message);
}
finally
{
myConnect.Close();
}