如何在ASP.NET中将空字符串设置为DataSource参数的默认值?

时间:2021-03-08 03:26:41

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

这不起作用。我从SQL数据库得到一个例外,该列不允许空值。

<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>

2 个解决方案

#1


9  

You could try

你可以试试

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

But I thought that was default.

但我认为那是默认的。

#2


0  

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!

#1


9  

You could try

你可以试试

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

But I thought that was default.

但我认为那是默认的。

#2


0  

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!