Le problème que j'ai est que lorsque j'exécute le code, je rencontre ceci erreur :
"Impossible d'insérer la valeur NULL dans la colonne 'OnHand', tableau ' C:\USERS\UNKNOWN\DESKTOP\XEX14PRODUCTRECEIPT\XEX14PRODUCTRECEIPT\APP_DATA\HALLOWEEN.MDF.dbo.Products ' ; n'autorise pas les valeurs nulles. UPDATE échoue"
Cela se produit lorsque je modifie la valeur de OnHand en rien ou en null, mais je suis censé obtenir l'erreur que j'ai créée dans le CodeBehind "A database error has occurred".
Quelle en est la cause ?
Merci !
CodeBehind pour GridView
public partial class Default : System.Web.UI.Page
{
protected void grdProducts_SelectedIndexChanged(object sender, EventArgs e)
{
grdProducts.HeaderRow.TableSection = TableRowSection.TableHeader;
}
protected void grdProducts_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
if (e.Exception == null)
{
lblError.Text = "A databse error has occured. " + "Message: " + e.Exception.Message;
e.ExceptionHandled = true;
e.KeepInEditMode = true;
}
else if (e.AffectedRows == 0)
{
lblError.Text = "Another user may have updated that category. " + "Please try again ";
}
}
}
}
Default.aspx
div class="container">
<header class="jumbotron"><%-- image set in site.css --%></header>
<main>
<form id="form1" runat="server" class="form-horizontal">
<div class="row">
<div class="col-xs-12">
<asp:GridView ID="grdProducts" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="SqlDataSource1"
CssClass="table table-bordered table-condensed" OnSelectedIndexChanged="grdProducts_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ID" SortExpression="ProductID"
ReadOnly="True">
<HeaderStyle CssClass="col-sm-2" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ReadOnly="True">
<HeaderStyle CssClass="col-sm-6" />
</asp:BoundField>
<asp:BoundField DataField="OnHand" HeaderText="On Hand" SortExpression="OnHand">
<HeaderStyle CssClass="col-sm-2 text-right" />
<ItemStyle CssClass="text-right" />
</asp:BoundField>
<asp:CommandField ShowEditButton="True" />
</Columns>
<HeaderStyle CssClass="bg-halloween" />
<AlternatingRowStyle CssClass="altRow" />
<EditRowStyle CssClass="warning" />
<PagerStyle CssClass="bg-halloween" HorizontalAlign="Center" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:HalloweenConnection %>"
SelectCommand="SELECT ProductID, Name, OnHand FROM Products"
UpdateCommand="UPDATE Products SET OnHand = @OnHand WHERE (ProductID = @ProductID)">
<UpdateParameters>
<asp:Parameter Name="OnHand" />
<asp:Parameter Name="ProductID" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<p><asp:Label ID="lblError" runat="server"
CssClass="text-danger" EnableViewState="false"></asp:Label></p>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
HeaderText="Please correct the following errors:" CssClass="text-danger" />
</div>
</div>
</form>
</main>