宋兴征 发表于 2013-5-11 23:42
GridView1.DataSource = ds.Tables[0];
指定数据源后,要重新绑定改一下:
GridView1.DataBind(); ...
这是全部代码,运行空白- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Data.SqlClient;
- public partial class Default5 : System.Web.UI.Page
- {
- SqlConnection conn = Db.createCon();
- protected void Page_Load(object sender, EventArgs e)
- {
- shuaxin();
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- conn.Open();
- String str = "select * from NCP_info where NCP_name = '" + TextBox1.Text.ToString() + "'";
- try
- {
- SqlDataAdapter da = new SqlDataAdapter(str , conn);
- DataSet ds = new DataSet();
- da.Fill(ds, "NCP_info");
- GridView1.DataSource = ds.Tables[0];
- GridView1.DataBind();
-
- }
- catch (SystemException ee)
- {
- Response.Write("<script>alert('错误!"+ee.Message +"');</script>");
- }
- finally {
- conn.Close();
- conn.Dispose();
- }
- }
- public void shuaxin()
- {
- try
- {
- SqlDataAdapter da = new SqlDataAdapter("select * from NCP_info", conn);
- DataSet ds = new DataSet();
- da.Fill(ds, "NCP_info");
- GridView1.DataSource = ds.Tables[0];
- GridView1.DataBind();
- }
- catch (SystemException e)
- {
- Response.Write("<script>alert('错误!"+e.Message +"');</script>");
- }
- finally {
- if (conn.State == ConnectionState.Open) {
- conn.Close();
- conn.Dispose();
- }
- }
- }
- }
复制代码 |