根据日期检索Gridview的数据 [英] Retrieve data for Gridview according to date
本文介绍了根据日期检索Gridview的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< asp:ScriptManager ID ScriptManager1 " runat 服务器" < /asp:ScriptManager >
< div >
< asp:TextBox ID =" runat 服务器" AutoPostBack True" < /asp:TextBox >
< asp:CalendarExtender ID =" runat 服务器" TargetControlID TextBox1" < /asp:CalendarExtender >
< /div >
使用系统;
使用 System.Collections.Generic;
使用 System.Configuration;
使用 System.Data;
使用 System.Data.OleDb;
使用 System.Data.SqlClient;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用使用System.Web.UI.WebControls;
使用 System.Globalization;
公共 部分 类视图:System.Web .UI.页面
{
字符串 strcon = ConfigurationManager.ConnectionStrings [" span>].ToString();
受保护的 无效 Page_Load(对象发件人,EventArgs e)
{
字符串 str = TextBox1.Text;
CultureInfo [] cultures = {CultureInfo.CreateSpecificCulture(" )};
foreach (文化文化中的 文化中的)
{
DateTime日期;
日期= DateTime.Parse(str,文化);
}
// 字符串str = TextBox1.Text;
// DateTime日期= 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();
}
无效 Load_GridData()
{
SqlConnection conn = 新 SqlConnection(strcon);
conn.Open(); // 打开连接
SqlDataAdapter Sqa = 新 SqlDataAdapter(" ,conn);
DataSet ds = 新 DataSet();
Sqa.Fill(ds); // 填充数据集
GridView1.DataSource = ds; // 将数据提供给GridView
GridView1.DataBind();
conn.Close();
}
}
错误:-日期有效时间未识别为字符串.
错误:无法将字符串识别为有效的日期时间.
日期保存在数据库的YYYY-MM-DD格式中.请帮助...
解决方案
将参数传递为
SqlConnection conn = 新 SqlConnection(strcon); conn.Open(); // 打开连接 SqlCommand cmd = 新 SqlCommand(" ,conn); cmd.Parameters.AddWithValue(" ,datevalue); SqlDataAdapter Sqa = 新 SqlDataAdapter(cmd); DataSet ds = 新 DataSet(); Sqa.Fill(ds); // 填充数据集 GridView1.DataSource = ds; // 将数据提供给GridView GridView1.DataBind(); conn.Close();
datevalue将是您要在查询中传递的日期的值
您可以使用AddWithValue提供@date参数的值:
Sqa.SelectCommand.Parameters.AddWithValue("@ date",YourDateParameter);
全局声明日期,以便您可以在整个代码页中访问它. yyyy-mm-dd是通用日期格式.
问题是您没有将任何输入传递给该查询,另一个问题是,如果您随时间保存日期,则="="不会给出结果,因此我进行了转换,它将获得该日期的结果.
我已转换为101,因为您是美国人
用于en-IN,您可以转换为105
将行更改为:
SqlDataAdapter Sqa =新的SqlDataAdapter(string.Format(从Img选择图像,ImageName,其中(convert(varchar(20),[Date],101)=``{0}'')",date.ToShortDateString()), conn);
希望它能起作用.
<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>
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...解决方案Pass parameter as
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
Hi,
You can use AddWithValue to supply the @date parameter''s value:
Sqa.SelectCommand.Parameters.AddWithValue("@date", YourDateParameter);
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.
这篇关于根据日期检索Gridview的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文