空的GridView虽然具有的​​SqlDataSource值 [英] empty gridview although the sqldatasource has values

查看:195
本文介绍了空的GridView虽然具有的​​SqlDataSource值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很奇怪的情况。
我已经创造了新的aspx页面,而无需使用任何自定义逻辑对象(视觉工作室向导创建的一切)试图创建的SqlDataSource网格视图。

本数据来源于存储过程中,与有默认值单一参数。当我刷新模式,或点击测试查询,我看到的结果行并corectly创建GridView的领域。但是,当我运行的页面不存在网格视图(它只是一个空的 - 当我添加EmptyDataTemplate它显示)。我已经添加了自定义(空)函数的DataBind,DataBinded和RowCreted事件,只有数据绑定和datavound事件被解雇(虽然,因为我写的 - 使用它的默认参数返回行和.NET存储过程可以在设计模式阅读)

有没有什么神奇的过程中,我已经做了不止一次这更没有问题。我试着在我们的生产ENV另一个存储过程至极的作品,仍然得到了同样的emty GridView控件

这里是code

 <%@页面语言=C#AutoEventWireup =真codeBehind =TEST.aspx.cs继承=site.TEST%GT;!< D​​OCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
    <标题>无标题页< /标题>
< /头>
<身体GT;
    <表ID =form1的=服务器>
    < D​​IV>
        < ASP:GridView控件ID =GridView1=服务器的DataSourceID =SqlDataSource1
            AllowSorting =真OnDataBinding =GridView1_DataBindingOnDataBound =GridView1_DataBound
            OnRowCreated =GridView1_RowCreated>
            < EmptyDataTemplate>
                无可用数据
            < / EmptyDataTemplate>
        < / ASP:GridView的>
        < ASP:SqlDataSource的ID =SqlDataSource1=服务器的ConnectionString =<%$的ConnectionStrings:myConnectionString%>中
            的SelectCommand =myStoredProcedureSelectCommandType =StoredProcedure的>
            < SelectParameters>
                < ASP:参数默认值=VAL1NAME =PAR1类型=字符串/>
                < ASP:参数名称=val2的TYPE =的Int32/>
            < / SelectParameters>
        < / ASP:SqlDataSource的>
    < / DIV>
    < /表及GT;
< /身体GT;
< / HTML>

和codebehind

 使用系统;
System.Collections中使用;
使用System.Configuration;
使用System.Data这;
使用System.Linq的;
使用的System.Web;
使用System.Web.Security;
使用System.Web.UI程序;
使用System.Web.UI.HtmlControls;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Xml.Linq的;命名空间的网站
{
    公共部分类测试:System.Web.UI.Page
    {
        保护无效的Page_Load(对象发件人,EventArgs的发送)
        {//这里刹车        }        保护无效GridView1_RowCreated(对象发件人,GridViewRowEventArgs E)
        {//这里刹车        }        保护无效GridView1_DataBinding(对象发件人,EventArgs的发送)
        {//这里刹车        }        保护无效GridView1_DataBound(对象发件人,EventArgs的发送)
        {//这里刹车        }
    }
}


解决方案

从你最近的评论看起来好像SQL事件探查器没有显示时,的SelectCommand 是任何活动从的SqlDataSource 发出。这可能是由于到 ConvertEmptyStringToNull 设置为true,默认情况下,包含在 SelectParameters 集合中参数的事实。此外,默认情况下, CancelSelectOnNullParameter 在SqlDataSource的设置为true。这意味着,你的'val2的参数可能传递一个NULL值,从而取消数据检索操作。这就是为什么你没有看到SQL事件探查器中的任何活动。

尝试设置 CancelSelectOnNullParameter 来假的的SqlDataSource

< ASP:SqlDataSource的ID =SqlDataSource1=服务器的ConnectionString =<%$ 的ConnectionStrings:myConnectionString%GT; 的SelectCommand =myStoredProcedure SelectCommandType =StoredProcedure的CancelSelectOnNullParameter =FALSE>
< / ASP:SqlDataSource的>

I have really weird situation. I've created new aspx page, and without using ANY custom logic objects (everything created with visual studios wizards) tried to create grid view from sqldatasource.

The data comes from stored procedure, with single parameter which has default value. when I refresh the schema or click "test query", I see result rows and GridViews fields are corectly created. But when I run the page there is no grid view (it's simply empty - when I add EmptyDataTemplate it is shown) . I've added custom (empty) function and DataBind, DataBinded and RowCreted event, and only databind and datavound events are fired (although, as I wrote - the stored procedure with its default parameter return rows and .net can read them in design mode)

There isn't anything "fancy" in the procedure, I've done this more than once with no problem. I've tried another stored procedure wich works in our production env and still got the same emty gridview

here is the code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TEST.aspx.cs" Inherits="site.TEST" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
            AllowSorting="True" OnDataBinding="GridView1_DataBinding" OnDataBound="GridView1_DataBound"
            OnRowCreated="GridView1_RowCreated">
            <EmptyDataTemplate>
                No Data Available
            </EmptyDataTemplate>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
            SelectCommand="myStoredProcedure" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:Parameter DefaultValue="val1" Name="par1" Type="String" />
                <asp:Parameter Name="val2" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

and the codebehind

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace site
{
    public partial class TEST : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {//brake here

        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {//brake here

        }

        protected void GridView1_DataBinding(object sender, EventArgs e)
        {//brake here

        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {//brake here

        }
    }
}

解决方案

From your recent comments it looks as though SQL Profiler isn't showing any activity when the SelectCommand is issued from the SqlDataSource. This could be due to the fact that ConvertEmptyStringToNull is set to true by default on Parameters contained in the SelectParameters collection. Also, by default, the CancelSelectOnNullParameter on the SqlDataSource is set to true. This means that your 'val2' parameter is probably passing a NULL value, which in turn cancels the data retrieval operation. This is why you don't see any activity within SQL Profiler.

Try setting CancelSelectOnNullParameter to false on the SqlDataSource.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" SelectCommand="myStoredProcedure" SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="False"> </asp:SqlDataSource>

这篇关于空的GridView虽然具有的​​SqlDataSource值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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