Gabriel Takyie
Gabriel Takyie

Reputation: 47

asp.net c# checking error

I want to check a selected date out of txtFrom and compare if that specific date exists. It should give me an alert, but it doesn't check. As a beginner I don't know if I wrote the code well, please can somebody help me? These are my codes:

string conn = WebConfigurationManager.ConnectionStrings["DIVIHOTELConnectionString"].ConnectionString;
SqlConnection myconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("select * from MyBooking where FromDate='" + txtFrom.Text + "'", myconn);
myconn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (txtFrom.Text == dr.GetString(1))
{
    ScriptManager.RegisterStartupScript(this, GetType(), "showalert","alert('This particular date has been booked already, please select new date !');", true);
}

Upvotes: 3

Views: 110

Answers (3)

Ankit Jain
Ankit Jain

Reputation: 23

First, filter query with proper date format i.e. Parameter be in data format Second, while reading data from datareader, specify the proper column name.

Upvotes: 1

Jaydip Jadhav
Jaydip Jadhav

Reputation: 12309

It is very clear that if your query returns column in this sequence [ID] ,[ClientID] ,[RoomID] ,[Amount] ,[DateBooked] ,[IsInBook] ,[FromDate] ,[EndDate] and you accessing dr.getString(1) then this will return value for [ClientID] not for [FromDate].

Try to use exact sequence number of column so that to get work done.

if (txtFrom.Text == dr.GetString(7))
{                          ------^
     //your code here
}

Upvotes: 2

Bharat
Bharat

Reputation: 2464

My Suggestion

Check what you storing in db and what is your input date which you are comparing both are same or not, because your datatype is varchar

As per my understating you will get result when your time also match with date. likely i will say you will not get result with your current scenario.

Upvotes: 1

Related Questions