【问题标题】:Unable to display item from microsoft sql server out on gridview via dataset无法通过数据集在gridview上显示来自microsoft sql server的项目
【发布时间】:2013-05-16 02:59:24
【问题描述】:

我无法显示已添加到 SQL 数据库中的值。这是我用过的代码。最初的想法是当用户进入特定页面时,默认情况下它将以网格视图的形式显示所有值。因此,我将我的 SQL 连接代码放在 page_load 中。我在服务器资源管理器中测试了我的连接,它说 ping 测试成功。

我的 SQL 连接

protected void Page_Load(object sender, EventArgs e)
    {
        if(Page.IsPostBack == false)
        {

        SqlConnection connSel = new SqlConnection("Data Source = localhost;" + "Initial Catalog = project; Integrated Security = SSPI");

        SqlDataAdapter adapSel;

        string mySQL = "Select * from Report";

        adapSel = new SqlDataAdapter(mySQL, connSel);

        connSel.Open();

        DataSet dsSel = new DataSet();
        adapSel.Fill(dsSel);
        GWCase.DataSource = dsSel;
        GWCase.DataBind();

        connSel.Close();

    }
    }

这是我的 gridview 的源代码。

 <asp:GridView ID="GWCase" runat="server" AutoGenerateColumns="False"  Width="100%" BackColor="#CCCCCC"  BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black"     Height="199px" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GWCase_SelectedIndexChanged">

    <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
    <RowStyle BackColor="White" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#808080" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
  </asp:GridView>

【问题讨论】:

    标签: c# asp.net sql-server visual-studio


    【解决方案1】:

    这是因为您的 gridview AutoGenerateColumns="False" 所以 gridview 不会自动生成列。在您的网格视图代码中,我没有看到任何 BoundFieldTemplateField 为其生成列,即您的网格视图没有任何列要显示。所以最好设置AutoGenerateColumns="true" 或定义一些BoundFieldTemplateField。我认为它会起作用。

    【讨论】:

      【解决方案2】:

      你可以试试这个吗?只需在使用前打开连接。

      protected void Page_Load(object sender, EventArgs e)
              {
                  if(Page.IsPostBack == false)
                  {
      
                  SqlConnection connSel = new SqlConnection("Data Source = localhost;" + "Initial Catalog = project; Integrated Security = SSPI");
      
                  connSel.Open();
      
                  SqlDataAdapter adapSel;
      
                  string mySQL = "Select * from Report";
      
                  adapSel = new SqlDataAdapter(mySQL, connSel);
      
                  DataSet dsSel = new DataSet();
                  adapSel.Fill(dsSel);
                  GWCase.DataSource = dsSel;
                  GWCase.DataBind();
      
                  connSel.Close();
      
              }
              }
      

      【讨论】:

      • 还是一样。我的 gridview 上没有显示任何值。
      • 尝试在 sql 中使用您的查询。看看会不会有返回值。
      【解决方案3】:

      在我处理表格和数据集之前很久。我不确定,但我认为每个数据集都包含许多表,当您尝试从数据库中选择特定表并填满时,它存储在数据集的第一个表中。尝试将数据源设置为:

      GWCase.DataSource = dsSel.Tables[0];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多