0

I have a gridview and sqldatasource. In tabledefinition the default format for date is mm:dd:yyyy , is there any way of changing the format to dd:mm:yyyy from sql settings or something like that.

I have globalization in form load(for dd/mm/.yyyy) but when I'm selecting from datasource some values between some dates :

 string d1 = Convert.ToDateTime(date1.Text).ToString("dd.MM.yyyy");
 string d2 = Convert.ToDateTime(date2.Text).ToString("dd.MM.yyyy");
 SqlDataSource1.SelectCommand = "Select * FROM test WHERE Name = '"+name.Text+"' AND Date between '"+d1+"' AND '"+d2+"'";

It works only for mm/dd/yyyy.

Is there any way to change it?

7
  • What is the datatype of column Date in SQL Server ? is it DateTime ? Commented Jun 15, 2012 at 7:07
  • A good habit is to use ISO date format in your SQL queries, i.e. YYYYMMDD Commented Jun 15, 2012 at 7:07
  • 4
    Is this an invitation of SQL-Injection? Use parameters. Commented Jun 15, 2012 at 7:10
  • 3
    Use Parameters and, having converted the text into dates, keep them as dates. Let ADO.Net translate .NET dates into SQL dates, and avoid dealing with formatting at all Commented Jun 15, 2012 at 7:11
  • 1
    @TimSchmelter is absolutly right. USE PARAMETERS! Commented Jun 15, 2012 at 7:11

5 Answers 5

4

Avoid the unnecessary conversions back to string and use parameters:

DateTime d1 = Convert.ToDateTime(date1.Text);
DateTime d2 = Convert.ToDateTime(date2.Text);
SqlDataSource1.SelectCommand = "Select * FROM test WHERE Name = @name AND Date between @d1 AND @d2";
SqlDataSource1.SelectCommand.Parameters.AddParameterWithValue("@name", name.Text);
SqlDataSource1.SelectCommand.Parameters.AddParameterWithValue("@d1", d1);
SqlDataSource1.SelectCommand.Parameters.AddParameterWithValue("@d2", d2);
Sign up to request clarification or add additional context in comments.

Comments

3

Its better if you can use Parameters with the query. Also if your column type is Date, then its better if you don't convert them to string for the comparison. Try the following

SqlDataSource1.SelectParameters.Add("@d1", date1);
SqlDataSource1.SelectParameters.Add("@d2", date2);
SqlDataSource1.SelectCommand = "Select * FROM test WHERE Name = '"+name.Text+"' AND Date between @d1 AND @d2";

Also use parameters for Name

Comments

1

Use a parametrized query, and all your problems will disappear. :)

SqlDataSource1.SelectCommand = "Select * FROM test WHERE Name = @p_Name AND Date between @p_from AND @p_to";
SqlDataSource1.SelectCommand.Parameters.Add ("@p_Name", SqlDbType.String).Value = name.Text;
SqlDataSource1.SelectCommand.Parameters.Add ("@p_from", SqlDbType.DateTime).Value = d1;
SqlDataSource1.SelectCommand.Parameters.Add ("@p_to", SqlDbType.DateTime).Value = d2;

1 Comment

It is what it is; you define a query which has parameters. These act as 'placeholders'. You define what the parameters are, what datatype they should contain, and assign a value to it. After that, all will be taken care of for you (data-formats, quoting, etc... )
1

Try to use parameters:

SqlDataSource1.SelectCommand = "SELECT * FROM test WHERE Name = @Name AND Date between @DateLow AND @DateHigh";
SqlDataSource1.SelectParameters.Add("Name", name.Text);
SqlDataSource1.SelectParameters.Add("DateLow", DbType.DateTime, d1);
SqlDataSource1.SelectParameters.Add("DateHigh", DbType.DateTime, d2);

Comments

0
SqlDataSource1.SelectCommand= " SELECT [columns] FROM [table] WHERE Name =   
'"+name.Text+"' AND Date BETWEEN  
CONVERT(Date,'"+d1+"' , 105)  AND CONVERT(Date,'"+d2+"', 105)"

try this....

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.