locked
Error handling when a SQL datasource insert fails RRS feed

  • Question

  • User1089205532 posted

    Hello all,

    I do have the following code to execute an insert:

        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                    InsertCommand="INSERT INTO [PLS_ARCH] ([Checked], [Userid], [Done], [Entity], [Environment], [TO_DO], [Description], [HOW_TO], [link_to_proc], [overtime], [processing_date], [driver]) (select Checked,Userid,DONE,Entity,Environment,TO_DO,Description,HOW_TO,LINK_TO_PROC,overtime,processing_date,driver from PLS_DAILY where entity=@entity and environment=@environment)" >
            <InsertParameters>
                <asp:Parameter Name="Entity" Type="String" />
                <asp:Parameter Name="Environment" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>

    linked with the following VB.NET code:

     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            If DropDownList1.SelectedValue = "EB" And DropDownList2.SelectedValue = "PROD" Then
                SqlDataSource2.InsertParameters("entity").DefaultValue = DropDownList1.SelectedValue
                SqlDataSource2.InsertParameters("Environment").DefaultValue = DropDownList2.SelectedValue
                SqlDataSource2.Insert()

     

    From a table point of view I did put the fact that column 'userid' can not contain NULL values. As such when the insert is execute and when one row has anyway the NULL value the following erromessage is returned in your webbrowser:

    Cannot insert the value NULL into column 'Userid', table 'H:\PLS\APP_DATA\PLS.MDF.dbo.PLS_ARCH'; column does not allow nulls. INSERT fails.

    I want to catch this error and give a proper text to the end-user => 'please make sure your userid is filled in'.

    I was trying to do this via the SQLdatasource inserting event but no clue ... .

    Can somebody help me.

    Regards,

    Steven

    Wednesday, June 22, 2011 9:31 AM

Answers

All replies