如何搜索特定日期数据,数据在asp.net中使用c#在gridview中显示 [英] how to search particular date data and the data is display at gridview in asp.net using c#

查看:73
本文介绍了如何搜索特定日期数据,数据在asp.net中使用c#在gridview中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,





我是使用c#和asp.net以及使用sqlserver2008开发的web应用程序

这些web应用程序有一个文本框一个命令按钮和一个gridview

sql server2008表也有一个表即svf svf有coloumns sno

rdate和amount .i我在文本框中输入了gridview显示的日期

rdate数据所以请给出代码并且我写了一些代码



SqlConnection con = new SqlConnection (Data Source = NSYS1 \\SQLEXPRESS; Initial Catalog = agfl; connect timeout = 30; Integrated Security = True);

con.Open();

string strQuery =select * from stv where rdate =@Textbox6.Text;

SqlCommand cmd = new SqlCommand(strQuery);

cmd.CommandType = CommandType.Text ;

SqlDataAdapter sda = new SqlDataAdapter();

sda.SelectCommand = cmd;

cmd.Parameters.AddWithValue(@ rdate,TextBox6.Text);

DataSet ds = new DataSet();

sda.Fill(ds);

DataTable dt = new DataTable();

dt = ds.Tables [0];

gvuser.DataSource = dt;

gvuser.DataBind();

con.Close();

但是我在网上运行命令时遇到错误点击事件



填充:SelectCommand.Connection属性尚未初始化。

Hello,


I am develop on web application using c#and asp.net and using sqlserver2008
these web application has one textbox one command button and one gridview
sql server2008 table also one table i.e. svf the svf has the coloumns sno
rdate and amount .i am entered the date in textbox the gridview display
rdate data so please give the code and i writed some code

SqlConnection con = new SqlConnection("Data Source=NSYS1\\SQLEXPRESS;Initial Catalog=agfl;connect timeout=30;Integrated Security=True");
con.Open();
string strQuery = "select * from stv where rdate =@Textbox6.Text";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
cmd.Parameters.AddWithValue("@rdate", TextBox6.Text);
DataSet ds = new DataSet();
sda.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
gvuser.DataSource = dt;
gvuser.DataBind();
con.Close();
but i got error while on web run at command buttom click event

Fill: SelectCommand.Connection property has not been initialized.

推荐答案

您的代码应如下所示:

Your code should be like this:
//connections
string strQuery = "select * from stv where rdate =@something";

SqlCommand cmd = new SqlCommand(strQuery,con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@something", TextBox6.Text);

SqlDataAdapter sda = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();
sda.Fill(dt);
gvuser.DataSource = dt;
gvuser.DataBind();
con.Close();



问候..


Regards..


使用下面提到的代码片段



Use below mentioned code snippet

SqlConnection con = new SqlConnection("Data Source=NSYS1\\SQLEXPRESS;Initial Catalog=agfl;connect timeout=30;Integrated Security=True");
string strQuery = "select * from stv where rdate="+"'"+Textbox6.Text+"'";
SqlCommand cmd = new SqlCommand(strQuery);
SqlDataAdapter sda = new SqlDataAdapter(cmd ,con);
DataSet ds = new DataSet();
ds.clear();
sda.Fill(ds);
gvuser.DataSource = ds;
gvuser.DataBind();





使用数据适配器时的信息无需提供con.open或关闭它将自我管理。



希望这有助于!!!



For your information when using data adapter no need to provide con.open or close it will manage itself.

Hope This Helps!!!


这篇关于如何搜索特定日期数据,数据在asp.net中使用c#在gridview中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆