views:

85

answers:

1

I am developing mobile application in C#. I am using the SQLite database to store the data. In the SQLite database I am storing the information related to the customer. In that I am storing the date with mm/dd/yy 12:54:30 PM format. Now I want to retrive the customer data based on start date & end date. I am using the following code

ShowRegistrationKeyDetails ShowRegKeyDetailsObj = new ShowRegistrationKeyDetails();
// set properties
          ShowRegKeyDetailsObj.FromDate = FromdateTimePicker.Value.ToString();
        ShowRegKeyDetailsObj.ToDate = TodateTimePicker.Value.ToString();

When I fire the SQL Queries based on these values I am not getting the proper result. I need to convert the above values of FromdateTimePicker.Text & TodateTimePicker.Text into the mm/dd/yy 12:54:30 PM format. I am using the following code

public SQLiteDataReader getClient_And_RegistrationKeys(int Customer_ID, String FromDt, String ToDt)
    {
        SQLiteDataReader SQLLiteDrObj = null;
        DataManager myDatabase = new DataManager("CompNet.db");
        myDatabase.Init(false);

        string[] Parameter = new string[] { "@Val_Customer_ID", "@Val_FromDt", "@Val_ToDt" };
        Object[] Objval = new Object[] { Customer_ID, FromDt, ToDt };

        string sqlCommandText = "Select Activation_Date, Standard_ID, Client_Key, Registration_Key from Activation_Details where Customer_ID=@Val_Customer_ID And Activation_Date between @Val_FromDt And @Val_ToDt";
        SQLLiteDrObj = myDatabase.ExecuteReader(sqlCommandText, Parameter, Objval);

        return SQLLiteDrObj;
    }

I have tried this code. Now I am getting the date in the format in which I want but I am not getting the required rows as output. There is no row in the output. Where I am going wrong ? Can you please provide me any code?

+2  A: 

Don't use Text to start with - use DateTimePicker.Value to get the value as a DateTime.

Ideally, then don't convert it into text when using SQLite either - use a parameterized query instead of formatting it at all. See this article as one example of using parameterized queries from C# with SQLite.

As a general rule, try to avoid going through text wherever possible. It's great for displaying values to users, but lousy for being confident that you've got the right data without having to be careful about different formats etc.

Jon Skeet
I have edited my question. I got your first answer. I helps me a lot. But I am facing new problem as described above. Can you please provide me the solution ?
Shailesh Jaiswal
@Shailesh: You're still calling `ToString`. Don't do that. Keep everything as a `DateTime`.
Jon Skeet
Yes you are right. I got the answer. Now everything running fine. Thanks a lot
Shailesh Jaiswal