Gridview没有在asp.net中绑定 [英] Gridview doesn't Bind in asp.net

查看:63
本文介绍了Gridview没有在asp.net中绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



我为基于文本框的Gridview值尝试了以下代码。使用搜索按钮显示以下代码。请帮帮我..



Dear Frnds,

I have tried the following code for Gridview values based on textbox. the following code is shown using search button. please help me..

SqlConnection conn;
       conn = new SqlConnection();
       conn.ConnectionString = @"Data Source=Param-PC;Initial Catalog=HMS;Persist Security Info=True;User ID=sa;Password=123";
       conn.Open();
       string query;
       SqlCommand SqlCommand;
       SqlDataReader reader;
       SqlDataAdapter adapter = new SqlDataAdapter();
       query = "SELECT Reg_No,convert(nvarchar(10),Reg_Date,103) as Reg_Date, Patient_Name, Contact_No, Department_Name, convert(nvarchar(10),Date,103) as Date, Advance_Amt FROM In_Patients where Patient_Name='" + TextBox1.Text + "'";

       SqlCommand = new SqlCommand(query, conn);
       adapter.SelectCommand = new SqlCommand(query, conn);
       reader = SqlCommand.ExecuteReader();
       GridView1.DataSource = reader;
       GridView1.DataBind();
       reader.Close();

推荐答案

试试这个:



Try this:

SqlCommand = new SqlCommand(query, conn);
SqlDataAdapter da=new sqldataadapter(SqlCommand );
Dataset ds=new Dataset();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
reader.Close();


如果要绑定数据,请使用数据集而不是datareader
Use dataset rather then datareader


GridView使用SqlDataReader



SqlDataReader将返回数据行



所以你想要逐行添加到表(创建一个新表)

然后最终将表数据绑定到你的GridView。



否则你可以使用



SqlDataAdapter da = new sqldataadapter(query,conn);

数据集ds = new Dataset();

da。填充(ds);

GridView1.DataSource = ds;

GridView1.DataBind();
if you want bind data to GridView By using SqlDataReader

SqlDataReader will returns the Row of data

So you want add row by row to table(create one new table)
then finally bind the Table data into your GridView.

or else u can use

SqlDataAdapter da=new sqldataadapter(query, conn);
Dataset ds=new Dataset();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();


这篇关于Gridview没有在asp.net中绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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