1

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();
}

1

1 Answer 1

2

You need to change your query:

strCommandText +=" WHERE Guest.RoomID=Room.RoomID AND Guest.GuestPassportNo=@guestpno AND HotelPackages.PackageID=Guest.HotelPackageID AND Guest.GuestCountry=@guestcountry;

should be:

strCommandText +=" WHERE Guest.RoomID=Room.RoomID AND Guest.GuestPassportNo=@guestpno AND (HotelPackages.PackageID=Guest.HotelPackageID OR GUEST.HotelPackageID IS NULL) AND Guest.GuestCountry=@guestcountry;
Sign up to request clarification or add additional context in comments.

7 Comments

hey thanks for your fast response! It should be (HotelPackages.PackageID=Guest.HotelPackageID OR GUEST.PackageID IS NULL) as it is the packageID in the guest table that may or may not be null. But when i do this, i get the PackageID 1(PackageName, Packageprice) displayed in my txtPackageName and txtPackagePrice.
Thanks. Have fixed my answer. On the issue of what is displayed in your text boxes - if the output of the query is correct then they should be fine, so does the query return what you want? You might get more help if you post a link to some sample data on http://sqlfiddle.com/.
Not yet. Is it possible for me to display blank in txtHotelPackage.Text and txtPackagePrice.Text when Guest.HotelPackageID is null? Cause now i'm getting the first ID name and price from the hotel package table even when the HotelPackageID in the guest table is null.
Sure you can. txtHotelPackage.Text = reader.IsDBNull("PackageName") ? "" : reader["PackageName"].ToString();, although I personally I prefer to use reader.GetString("PackageName").
hmm..i'm getting an error at reader.IsDBNull("PackageName") saying it cannot convert from 'string' to 'int'?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.