首页 文章

Asp.Net C#如何将null值参数传递给datasource

提问于
浏览
2

我有一个带参数的gridview . 它绑定到一个SQL查询的数据源 . 当参数的值为null时,查询在SQL Server中执行时返回行 . 当我尝试通过以下代码行传递参数null值时,我得不到任何结果 .

FormDS.SelectParameters["ServiceFormCompletedId"].DefaultValue = Request.QueryString["sfcId"];

查询字符串不存在,因此它将null值传递给FormDS数据源 . SelectParameters表示它是一个空值 .

所以我试着给它一个空字符串 .

FormDS.SelectParameters["ServiceFormCompletedId"].DefaultValue = "";

在ASP.NET的标记中有以下内容

<asp:Parameter Name="ServiceFormCompletedId" ConvertEmptyStringToNull="true" />

也没有结果 .

我在配置数据源的“测试查询”部分中使用以下值测试了数据源并获得了行 .

enter image Configure Data Source

关于将空值传递给SqlDataSource的SelectParameters,我错过了什么?

1 回答

  • 1

    试试这个:

    SqlCommand cmd = new SqlCommand("ProcedureName");
                cmd.Connection = new SqlConnection("yourConnString");
                cmd.CommandType = CommandType.StoredProcedure;
    
                cmd.Parameters[0] = new SqlParameter("YourParaameter", SqlDbType.NVarChar);
                cmd.Parameters[0].Value = DBNull.Value;
                cmd.Parameters[0].Direction = ParameterDirection.Input;           
    
                cmd.ExecuteNonQuery(); // or any other command that you want to execute
    

相关问题