Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>

<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="TextBox1">
</asp:CalendarExtender>

</div>

C#
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
public partial class View : System.Web.UI.Page
{
string strcon = ConfigurationManager.ConnectionStrings["con"].ToString();

protected void Page_Load(object sender, EventArgs e)
{
string str = TextBox1.Text;

CultureInfo[] cultures = { CultureInfo.CreateSpecificCulture("en-US") };
foreach (CultureInfo culture in cultures)
{
DateTime date;
date = DateTime.Parse(str, culture);
}
//string str = TextBox1.Text;
// DateTime date = DateTime.ParseExact(str, "yyyy/MM/DD", null);
//System.DateTime str_date = DateTime.Parse(TextBox1.Text, System.Globalization.CultureInfo.CreateSpecificCulture("en-AU").DateTimeFormat);
//var userdateformat = DateTime.ParseExact(" ' " + TextBox1.Text.ToString() + " ' ", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);

Load_GridData();

}
void Load_GridData()
{
SqlConnection conn = new SqlConnection(strcon);
conn.Open(); // open the connection
SqlDataAdapter Sqa = new SqlDataAdapter("select Image, ImageName from Img where ([Date] = @date) ", conn);
DataSet ds = new DataSet();
Sqa.Fill(ds); // fill the dataset
GridView1.DataSource = ds; // give data to GridView
GridView1.DataBind();
conn.Close();
}
}

ERROR:- STRING IS NOT RECOGNIZED AS VALID DATETIME.
Error:- String is not recognized as valid datetime.


Date is save in YYYY-MM-DD formate in database. Please Help...
Posted
Updated 3-Apr-12 21:27pm
v3
Comments
Mohamed Mitwalli 4-Apr-12 3:29am    
Hi,
SqlDataAdapter Sqa = new SqlDataAdapter("select Image, ImageName from Img where ([Date] = @date) ", conn);
for @date where are you supply the value ??

Pass parameter as
C#
SqlConnection conn = new SqlConnection(strcon);
conn.Open(); // open the connection
SqlCommand cmd=new SqlCommand("select Image, ImageName from Img where ([Date] = @date) ", conn);
cmd.Parameters.AddWithValue("date",datevalue);
SqlDataAdapter Sqa = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
Sqa.Fill(ds); // fill the dataset
GridView1.DataSource = ds; // give data to GridView
GridView1.DataBind();
conn.Close();

datevalue will be value of date that you want to pass in query
 
Share this answer
 
Comments
Mohamed Mitwalli 4-Apr-12 7:27am    
5
uspatel 4-Apr-12 8:05am    
Thanks Mitwalli.......
Mohamed Mitwalli 4-Apr-12 8:27am    
your welcome :)
Hi,
You can use AddWithValue to supply the @date parameter's value:
Sqa.SelectCommand.Parameters.AddWithValue("@date", YourDateParameter);
 
Share this answer
 
declare date globally,so that u can access it through out the code page.
yyyy-mm-dd is universal date format.

Problem is u didn't pass any input to that query and another problem is if u r saving the date with time then '=' doesn't give results so I made that conversion and it will get the results on that date.
I've converted to 101 as ur cilture is en-US
for en-IN u can convert to 105

change the line as:
SqlDataAdapter Sqa = new SqlDataAdapter(string.Format("select Image, ImageName from Img where (convert(varchar(20),[Date],101) = '{0}') ",date.ToShortDateString()), conn);


Hope it works.
 
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