日期:2014-05-18  浏览次数:20472 次

怎样用gridview显示存储过程的查询结果
我是这样写的,可是不显示查询结果,大家帮我看看 

sing 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; 
using System.Web.Configuration; 

public partial class Default3 : System.Web.UI.Page 

  protected void Page_Load(object sender, EventArgs e) 
  { 

  } 
  protected void Button1_Click(object sender, EventArgs e) 
  { 
  SqlConnection conn = new SqlConnection(); 
  ConnectionStringSettings connsetting = ConfigurationManager.ConnectionStrings["ConnectionString2"]; 
  conn.ConnectionString = connsetting.ConnectionString; 
  SqlCommand cmd = new SqlCommand("sp_EmployInfo",conn); 
  cmd.CommandType = CommandType.StoredProcedure; 
  conn.Open(); 
  cmd.Parameters.Add(new SqlParameter("@TitleOfCourtesy", SqlDbType.NVarChar, 25,"this.TextBox1.Text")); 
  cmd.Parameters.Add(new SqlParameter("@Country", SqlDbType.NVarChar, 15, "this.DropDownList1.DataTextField")); 
  cmd.Parameters["@TitleOfCourtesy"].Value = this.TextBox1.Text; 
  cmd.Parameters["@Country"].Value = this.DropDownList1.DataTextField; 
  GridView1.DataSource = cmd.ExecuteReader(); 
  GridView1.DataBind(); 
  
  conn.Close(); 
   
  } 

存储过程 
ALTER PROCEDURE sp_EmployInfo 
@TitleOfCourtesy nvarchar(25), 
@Country nvarchar(15) 
AS 
select *from Employees where TitleOfCourtesy=@TitleOfCourtesy and Country=@Country 


------解决方案--------------------
cmd.Parameters["@Country"].Value = this.DropDownList1.DataTextField; 
改成
cmd.Parameters["@Country"].Value = this.DropDownList1.SelectedItem.text; 

然后断点跟踪一下

this.DropDownList1.DataTextField 获取的是一个绑定字段 而不是值
------解决方案--------------------
C# code
        SqlConnection conn = new SqlConnection();
        ConnectionStringSettings connsetting = ConfigurationManager.ConnectionStrings["ConnectionString2"];
        conn.ConnectionString = connsetting.ConnectionString;
        SqlCommand cmd = new SqlCommand("sp_EmployInfo", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@TitleOfCourtesy", this.TextBox1.Text);
        cmd.Parameters.Add("@Country", this.DropDownList1.SelectedItem.Text);
        conn.Open();
        GridView1.DataSource = cmd.ExecuteReader();
        GridView1.DataBind();
        conn.Close();