试图将数据绑定到GridView但无法绑定

时间:2015-03-17 17:53:54

标签: c# asp.net datagridview

我正在尝试将数据绑定到gridView。基本上我想将特定人员记录绑定到gridview,所以为此我通过查询获取数据。我还设置了AutoGenerateColumns=True。我的代码执行正常但没有异常也没有错误。请检查并指导。谢谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class ViewRegisteredDonors : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection conec = new SqlConnection(ConfigurationManager.ConnectionStrings["BloodDonorRegistrationConnectionString"].ConnectionString);

        string query;
        query = "select D_FirstName,D_LastName,D_BloodGroup,D_City,D_Phone from Donor where D_City= @BG AND D_City=@cc";
        SqlCommand cmd = new SqlCommand(query, conec);
        cmd.Parameters.AddWithValue("@BG", dropbownBlood.SelectedItem.Text);
        cmd.Parameters.AddWithValue("cc", DropDownListCity.SelectedItem.Text);
        conec.Open();
        cmd.CommandType = CommandType.Text;
        //cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet d = new DataSet();
        da.Fill(d);
        GridView1.DataSource = d;
        GridView1.DataBind();
    }
}

1 个答案:

答案 0 :(得分:0)

检查您的参数名称@ cc。@是否缺失,因此您的数据集可能为空。

cmd.Parameters.AddWithValue("cc", DropDownListCity.SelectedItem.Text);

应该是

cmd.Parameters.AddWithValue("@cc", DropDownListCity.SelectedItem.Text);