3

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);
}
12
  • any runtime error? Commented Oct 25, 2016 at 12:11
  • 2
    Learn how to use parameters. Concatenating strings (in particular with dates) is guaranteed to give wrong results at its best. And in worst case you could easily loose your whole database because you allow your user to type anything in that textbox (Sql Injection) Commented Oct 25, 2016 at 12:12
  • What is the data type of FromDate? Assuming datetime. You are doing a Select *, so what is the field order you are reading in your data reader? You should select the column names in case the schema changes. Commented Oct 25, 2016 at 12:13
  • And also if you are comparing with date then convert you date in C# from string to Date. Commented Oct 25, 2016 at 12:14
  • FromDate is varchar , shall i change to datetime and remove the*? Commented Oct 25, 2016 at 12:17

3 Answers 3

2

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

but now when i write a date that doesn't exist, it gives me error
An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code Additional information: Invalid attempt to read when no data is present.
Try to use dr.GetString(6). I just given sample number 7 for sequence
it worked with the 6 for an existing date but not a new date
Put another if statement on the outside of this one to first check if dr.GetString(6) != null
1

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.

2 Comments

but now when i write a date that doesn't exist, it gives me error
An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code Additional information: Invalid attempt to read when no data is present.
1

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.

Comments

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.