Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an empty string as a default value for a DataSource parameter in ASP.NET?

Tags:

c#

asp.net

This doesn't work. I've got an exception from SQL databse that column does not allow nulls.

<asp:SqlDataSource ID="MyDataSOurcet" runat="server" 
        ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
        InsertCommand="INSERT INTO MyTable(Name) VALUES (@Name) WHERE NameID = 1" 
        <InsertParameters>
            <asp:Parameter Name="Name" Type="String" DefaultValue=""/>
        </InsertParameters>
    </asp:SqlDataSource>
like image 902
Wodzu Avatar asked Sep 30 '10 10:09

Wodzu


2 Answers

You could try

   <asp:Parameter Name="Name"  Type="string" DefaultValue="" 
    ConvertEmptyStringToNull="false" />

But I thought that was default.

like image 133
Henk Holterman Avatar answered Sep 21 '22 00:09

Henk Holterman


try this:

<asp:SqlDataSource ID="MyDataSOurcet" runat="server" OnInserting="MyDataSOurcetInserting" ... >

//cs
protected void MyDataSOurcetInserting(object sender, SqlDataSourceCommandEventArgs e)
{
   //check if nothing has been inserted then
   e.Command.Parameters["Name"].value = DBNull.Value;
} 

Good luck!

like image 25
Mouhannad Avatar answered Sep 20 '22 00:09

Mouhannad