5

I have a gridview with a parameter. It is bound to a datasource which is a SQL query. The query returns rows when executed in SQL Server when the value of the parameter is null. When I try to pass the parameter a null value via the following line of code I get no results.

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

The querystring does not exist so it passes a null value to the FormDS Datasource. The SelectParameters said it was a null value.

So I tried giving it an empty string.

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

with the following in the markup of ASP.NET

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

There was also no results.

I tested the Datasource with the following values in the Test Query section of the Configure Data Source and got rows back.

enter image Configure Data Source

What am I missing about passing null values to the SelectParameters of a SqlDataSource?

5
  • 2
    Have you tried DbNull.Value ? Commented Apr 4, 2017 at 4:31
  • How to I use DbNull ? I just tried it. Can not implicity convert type 'System.DBNull' to a 'string'. I tried DbNull.ToString and that does not work as it is an empty string and that is not working. Commented Apr 4, 2017 at 4:41
  • 1
    The answer can be found here : Stack Overflow : pass null value to DB via sqldatasource. Commented Apr 4, 2017 at 6:28
  • Maybe You can use ternary if the string is empty / the length is 0, pass DBNull.Value, otherwise, pass your string. Commented Apr 4, 2017 at 6:39
  • @WazeAndroid is correct. The previous solution worked well. I initially ignored it as I could not see it working. Do you want to make it an answer for this question? Commented Apr 4, 2017 at 21:28

2 Answers 2

5

Try this:

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
Sign up to request clarification or add additional context in comments.

1 Comment

Although I am sure your solution works, it was not the exact solution I was looking for. CancelSelectOnNullParameter="false" in the SqlDataSource markup of ASP.NET works.
2

The solution is to put CancelSelectOnNullParameter="false" in the SqlDataSource markup like the following.

<asp:SqlDataSource ID="IDxxx" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringxxx %>" CancelSelectOnNullParameter="false" SelectCommand="SELECT * FROM Table Where @Parameter1 = Table.Column1" >
    <SelectParameters>
        <asp:Parameter Name="Parameter1" />                                        
    </SelectParameters>
</asp:SqlDataSource>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.