Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, i visited another topic but i couldnt to fix my problem.
i have some fields in my reserved table (reservedID,roomID,UserId,strat_date,end_date).
i reserved some room in this table.
when i want to reserve another room , i should check start_date and end_date with two date which user inserted in datepicker.
i get this (two)dates from querystring(string type) and i cant convert it to datetime for convert.
my start_date and end_date fields are string.
i get them from db ...



C#
protected void Button1_Click(object sender, EventArgs e)
{
    string room_id = Request.QueryString["ri"];
    int room_id2 = Convert.ToInt32(room_id);
    string from = Request.QueryString["from"];
    string to = Request.QueryString["to"];
    string name = TextBox1.Text;
    string email = TextBox2.Text;
    if (!String.IsNullOrEmpty(room_id))
    {
        if (!String.IsNullOrEmpty(from))
        {
            if (!String.IsNullOrEmpty(to))
            {
                if (!String.IsNullOrEmpty(name))
                {
                    if (!String.IsNullOrEmpty(email))
                    {
                        if (new EmailAddressAttribute().IsValid(email))
                        {
                            List<DAL.project.RESERVED> reserved = BLL.project.DataProvider.reserved_room(room_id2);
                            int status = 0;
                            foreach(DAL.project.RESERVED rsd in reserved)
                            {
                                DateTime sd = Convert.ToDateTime(rsd.START_DATE);
                                DateTime ed = Convert.ToDateTime(rsd.END_DATE);
                                if ((sd <= from2 && ed >= from2) || (sd <= to2 && ed >= to2))
                                {
                                    status = 1;
                                    break;
                                }
                            }
                            Label3.Text = Convert.ToString(status);
                        }
                        else
                        {
                            Label3.Text = "ایمیل وارد شده اشتباه است.";
                        }
                    }
                    else
                    {
                        Label3.Text = "پست الکترونیکی را وارد نکرده اید.";
                    }
                }
                else
                {
                    Label3.Text = "نام را وارد نکرده اید.";
                }
            }
            else
            {
                Label3.Text = "تاریخ را وارد نکرده اید.";
                Response.AddHeader("REFRESH", "3;search.aspx");
            }
        }
        else
        {
            Label3.Text = "تاریخ را وارد نکرده اید.";
            Response.AddHeader("REFRESH", "3;search.aspx");
        }
    }
    else
    {
        Label3.Text = "اجازه ی دسترسی به این صفحه را ندارید. بعد از چند ثانیه به صفحه ی جستجو هدایت می شوید.";
        Response.AddHeader("REFRESH", "3;search.aspx");
    }
}


What I have tried:

i tried so much but i couldnt convert :
string from = Request.QueryString["from"];
string to = Request.QueryString["to"];

and

rsd.strat_date , rsd.end_date



to datetime for provide compare
Posted
Updated 13-Jul-16 17:04pm
Comments
Patrice T 13-Jul-16 16:46pm    
Show the strings you try to convert to date.
saeed rajabi 13-Jul-16 16:50pm    
wait
saeed rajabi 13-Jul-16 16:50pm    
string from = Request.QueryString["from"];
string to = Request.QueryString["to"];
Patrice T 13-Jul-16 16:55pm    
NoI want something like
from= "20160713"
saeed rajabi 13-Jul-16 16:57pm    
from = "۱۳۹۵/۰۴/۰۱";

I think
C#
Convert.ToDateTime
will not be an efficient method to convert string into DateTime. Since it Throws FormatException if the Conversion failed, So i Strongly Recommend you to use
C#
DateTime.TryParseExact
which has internal Exception handling mechanism So that it will Give a Return value To indicate the Conversion status and the output to the out variable. Consider the following code:

C#
DateTime inDate;
string formatString="MM/dd/yyyy hh:mm:ss";
bool conversionResult = DateTime.TryParseExact(rsd.START_DATE, formatString, CultureInfo.InvariantCulture, DateTimeStyles.None, out inDate)
if(!conversionResult) 
{
  Console.WriteLine("Invalid Input");
}


Please note : The formatString and the format of the Input should match.
 
Share this answer
 
Comments
Karthik_Mahalingam 16-Jul-16 3:20am    
5
Sujith Karivelil 18-Jul-16 1:06am    
coool
Karthik_Mahalingam 18-Jul-16 1:11am    
:)
Hi wherever you get date, first date convert to string then string to date you can convert.
Some time you stored date in object then from object data can not convert directly into date so first convert string then convert to data format.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900